在列表中提供子网并使用terraform访问它们

问题描述

我正在逐个分配子网,我想在列表中列出它,但是如果我这样做了,我将如何从列表中访问特定子网感到困惑。我想对可用区域做同样的事情。就像如果我要给public_subnet_cidr_2,如果它在列表中一样,我将如何给它。

main.tf

module "my_vpc" {
  source   = "./modules/vpc"
  vpc_cidr = "13.0.0.0/16"
  #tenancy               = "default"
  vpc_id                = "${module.my_vpc.vpc_id}"
  public_subnet_cidr_1  = "13.0.1.0/24"
  public_subnet_cidr_2  = "13.0.2.0/24"
  private_subnet_cidr_1 = "13.0.3.0/24"
  private_subnet_cidr_2 = "13.0.4.0/24"
  availability_zone_1   = "us-east-2a"
  availability_zone_2   = "us-east-2b"
}

vpc-main.tf

resource "aws_vpc" "main" {
  cidr_block = var.vpc_cidr
  tags = {
    Name = "wahaj-vpc"
  }
}

# Define the public subnet
resource "aws_subnet" "public_subnet_1" {
  vpc_id     = var.vpc_id
  cidr_block = var.public_subnet_cidr_1

  availability_zone = var.availability_zone_1

  tags = {
    Name = "Public Subnet 1"
  }
}

resource "aws_subnet" "public_subnet_2" {
  vpc_id     = var.vpc_id
  cidr_block = var.public_subnet_cidr_2

  availability_zone = var.availability_zone_2

  tags = {
    Name = "Public Subnet 2"
  }
}

# Define the private subnet
resource "aws_subnet" "private_subnet_1" {
  vpc_id            = var.vpc_id
  cidr_block        = var.private_subnet_cidr_1
  availability_zone = var.availability_zone_1

  tags = {
    Name = "Private Subnet 1"
  }
}

resource "aws_subnet" "private_subnet_2" {
  vpc_id            = var.vpc_id
  cidr_block        = var.private_subnet_cidr_2
  availability_zone = var.availability_zone_2

  tags = {
    Name = "Private Subnet 2"
  }
}
# Define the internet gateway
resource "aws_internet_gateway" "gw" {
  vpc_id = var.vpc_id

  tags = {
    Name = "VPC IGW"
  }
}

# Define the route table
resource "aws_route_table" "web_public_rt" {
  vpc_id = var.vpc_id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.gw.id
  }

  tags = {
    Name = "Public Subnet RT"
  }
}

# Assign the route table to the public Subnet
resource "aws_route_table_association" "web-public-rt" {
  subnet_id      = aws_subnet.public_subnet_1.id
  route_table_id = aws_route_table.web_public_rt.id
}

解决方法

您的代码中存在几个明显的问题

  • 您不能拥有CIDR为13.0.0.0/16的VPC。我猜应该是10.0.0.0/16
  • 这也是创建aws_vpc在模块外部的VPC的非常不同的方式。
  • 该模块也缺少public_subnet_1
  • 最好将list用作变量,而不要传递单独的public_subnet_cidr_1public_subnet_cidr_2等。
  • 我看不出为什么需要对availability_zone_1availability_zone_2进行硬核化的原因。最好使用data源。

无论如何,我修改了您的文件,以便它们进行部署。我验证了我在沙盒帐户中us-east-1区域中的情况。请注意,我仅在对代码进行最小更改的情况下进行部署。我尚未验证,该VPC是否正常运行,或者是否按预期运行。

main.tf

provider "aws" {
  # your data
}

resource "aws_vpc" "main" {
  cidr_block       = "10.0.0.0/16"

  tags = {
    Name = "main"
  }
}


module "my_vpc" {
  source   = "./modules/vpc"
  vpc_cidr = "10.0.0.0/16"
  vpc_id                = aws_vpc.main.id
  public_subnet_cidr_1  = "10.0.1.0/24"
  public_subnet_cidr_2  = "10.0.2.0/24"
  private_subnet_cidr_1 = "10.0.3.0/24"
  private_subnet_cidr_2 = "10.0.4.0/24"
  availability_zone_1   = "us-east-1a"
  availability_zone_2   = "us-east-1b"
}


module / vpc / vpc.tf

variable "vpc_id" {}
variable "vpc_cidr" {}
variable "public_subnet_cidr_1" {}
variable "public_subnet_cidr_2" {}
variable "private_subnet_cidr_1" {}
variable "private_subnet_cidr_2" {}
variable "availability_zone_1" {}
variable "availability_zone_2" {}


resource "aws_subnet" "public_subnet_1" {
  vpc_id     = var.vpc_id
  cidr_block = var.public_subnet_cidr_1

  availability_zone = var.availability_zone_1

  tags = {
    Name = "Public Subnet 1"
  }
}


resource "aws_subnet" "public_subnet_2" {
  vpc_id     = var.vpc_id
  cidr_block = var.public_subnet_cidr_2

  availability_zone = var.availability_zone_2

  tags = {
    Name = "Public Subnet 2"
  }
}

# Define the private subnet
resource "aws_subnet" "private_subnet_1" {
  vpc_id            = var.vpc_id
  cidr_block        = var.private_subnet_cidr_1
  availability_zone = var.availability_zone_1

  tags = {
    Name = "Private Subnet 1"
  }
}

resource "aws_subnet" "private_subnet_2" {
  vpc_id            = var.vpc_id
  cidr_block        = var.private_subnet_cidr_2
  availability_zone = var.availability_zone_2

  tags = {
    Name = "Private Subnet 2"
  }
}
# Define the internet gateway
resource "aws_internet_gateway" "gw" {
  vpc_id = var.vpc_id

  tags = {
    Name = "VPC IGW"
  }
}

# Define the route table
resource "aws_route_table" "web_public_rt" {
  vpc_id = var.vpc_id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.gw.id
  }

  tags = {
    Name = "Public Subnet RT"
  }
}

# Assign the route table to the public Subnet
resource "aws_route_table_association" "web-public-rt" {
  subnet_id      = aws_subnet.public_subnet_1.id
  route_table_id = aws_route_table.web_public_rt.id
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...