Terraform: How to create multiple aws subnets from one resource block?

前端 未结 2 625
借酒劲吻你
借酒劲吻你 2021-01-16 02:23

I\'m trying to create multiple subnets from one resource block and I get the following error

Error: aws_subnet.private: cidr_block must be a single v

相关标签:
2条回答
  • 2021-01-16 02:58

    private_subnet is a list, so you should pick a single element, e.g.

    cidr_block = "${element(var.private_subnet,count.index)}"

    0 讨论(0)
  • 2021-01-16 02:59

    You have to create multiple aws_subnet resources by utilitizing the count argument to create one resource for each entry in your var.private_subnet list:

    resource "aws_subnet" "private" {
      count                   = "${length(var.private_subnet)}"
      vpc_id                  = "${aws_vpc.vpcname.id}"
      cidr_block              = "${var.private_subnet[count.index]}"
      availability_zone       = "${data.aws_availability_zones.available.names[count.index]}"
      map_public_ip_on_launch = false
    }
    

    This expands the single aws_subnet resource into two, each with slightly different values based on the enumeration of count when each resource block is evaluated by terraform.

    0 讨论(0)
提交回复
热议问题