Terraform:如何将多个子网关联到路由表?

时间:2018-08-08 05:56:16

标签: terraform terraform-provider-aws

我正在使用terraform v.0.11.7。

我想创建4个子网(2个公共子网,2个私有子网)

这是vars.tf的内容

variable "region" {
  default = "ap-south-1"
}

variable "ami_id" {
  type = "map"
  default = "ami-d783a9b8"
}

variable "credentials" {
  default = "/root/.aws/credentials"
}

variable "vpc_cidr" {
  default = "10.0.0.0/16"
}

variable "pub_subnet_aza_cidr" {
  default = "10.0.10.0/24"
}

variable "pub_subnet_azc_cidr" {
  default = "10.0.20.0/24"
}

variable "pri_subnet_aza_cidr" {
  default = "10.0.30.0/24"
}

variable "pri_subnet_azc_cidr" {
  default = "10.0.40.0/24"
}

现在在main.tf内部,我想将前2个公共子网关联到公共路由表,该怎么做?

resource "aws_subnet" "pub_subnet_aza" {
  vpc_cidr = "{aws_vpc.vpc.id}"
  cidr_block = "${var.pub_subnet_aza_cidr}"
  tags {
    Name = "Pub-Sunet-A"
 }
  availability_zone = "${data.aws_availability_zone.available.name[0]}"
}

resource "aws_subnet" "pub_subnet_azc" {
  vpc_cidr = "{aws_vpc.vpc.id}"
  cidr_block = "${var.pub_subnet_azc_cidr}"
  tags {
   Name = "Pub-Subnet-C"
}
  availability_zone = "${data.aws_availability_zone.available.name[2]}"
}

resource "aws_route_table_association" "public" {
  subnet_id = "${aws_subnet.pub_subnet_aza.id}"  # How to put pub_subnet_azc.id into here?
  route_table_id = "${aws_route_table.public.id}"
}

1 个答案:

答案 0 :(得分:3)

最好使用子网列表来减少变量数量。然后,您还可以使用count = length(var.subnets)获取2个路由表关联资源实例,并从子网列表中选择正确的实例。

variable "subnet_cidrs_public" {
  description = "Subnet CIDRs for public subnets (length must match configured availability_zones)"
  # this could be further simplified / computed using cidrsubnet() etc.
  # https://www.terraform.io/docs/configuration/interpolation.html#cidrsubnet-iprange-newbits-netnum-
  default = ["10.0.10.0/24", "10.0.20.0/24"]
  type = "list"
}

resource "aws_subnet" "public" {
  count = "${length(var.subnet_cidrs_public)}"

  vpc_id = "${aws_vpc.main.id}"
  cidr_block = "${var.subnet_cidrs_public[count.index]}"
  availability_zone = "${var.availability_zones[count.index]}"
}

resource "aws_route_table_association" "public" {
  count = "${length(var.subnet_cidrs_public)}"

  subnet_id      = "${element(aws_subnet.public.*.id, count.index)}"
  route_table_id = "${aws_route_table.public.id}"
}

我看到您一直在通过data阅读可用区,这很好,您仍然可以这样做。您只需要以某种方式设置子网和AZ之间的关联。我留给你。

当然,更优雅的做法是在该地区的每个可用区中配置一个子网。一旦使用cidrsubnet()计算子网的地址空间,就可以使用length(data.availability_zones)作为其余所有地址的驱动程序。应该不太复杂。

这是完整的代码:

provider "aws" {
  region = "eu-west-1"
}

variable "availability_zones" {
  description = "AZs in this region to use"
  default = ["eu-west-1a", "eu-west-1c"]
  type = "list"
}

variable "vpc_cidr" {
  default = "10.0.0.0/16"
}

variable "subnet_cidrs_public" {
  description = "Subnet CIDRs for public subnets (length must match configured availability_zones)"
  # this could be further simplified / computed using cidrsubnet() etc.
  # https://www.terraform.io/docs/configuration/interpolation.html#cidrsubnet-iprange-newbits-netnum-
  default = ["10.0.10.0/24", "10.0.20.0/24"]
  type = "list"
}

resource "aws_vpc" "main" {
  cidr_block = "${var.vpc_cidr}"
  tags {
    Name = "stackoverflow-51739482"
  }
}

resource "aws_subnet" "public" {
  count = "${length(var.subnet_cidrs_public)}"

  vpc_id = "${aws_vpc.main.id}"
  cidr_block = "${var.subnet_cidrs_public[count.index]}"
  availability_zone = "${var.availability_zones[count.index]}"
}

resource "aws_route_table" "public" {
  vpc_id = "${aws_vpc.main.id}"

  tags {
    Name = "public"
  }
}

resource "aws_route_table_association" "public" {
  count = "${length(var.subnet_cidrs_public)}"

  subnet_id      = "${element(aws_subnet.public.*.id, count.index)}"
  route_table_id = "${aws_route_table.public.id}"
}