本文介绍了Terraform:如何从一个资源块创建多个AWS子网?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从一个资源块创建多个子网,但出现以下错误

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

main.tf

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

  tags {
    Name        = "${var.private}"
    Environment = "${terraform.workspace}"
  }
}

variable.tf

variable "private_subnet" {
  type    = "list"
  default = []
}

dev.tfvars

private_subnet = ["10.0.2.0/24", "10.0.3.0/24"]

推荐答案

您必须通过使用count参数为var.private_subnet列表中的每个条目创建一个资源来创建多个aws_subnet资源:

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
}

这会将单个aws_subnet资源扩展为两个,当每个资源块由terraform求值时,基于count的枚举,每个资源的值会略有不同.

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.

这篇关于Terraform:如何从一个资源块创建多个AWS子网?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 18:30