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

前端 未结 2 628
借酒劲吻你
借酒劲吻你 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条回答
  •  梦毁少年i
    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.

提交回复
热议问题