Terraform-如何在对象列表上使用for_each循环创建资源

时间:2019-08-20 09:24:40

标签: amazon-web-services for-loop foreach terraform

我有一个包含要创建的子网列表的对象。

variable "subnet-map" {
  default = {
    ec2 = [
      {
        cidr_block        = "10.0.1.0/24"
        availability_zone = "eu-west-1a"
      }
    ],
    lambda = [
      {
        cidr_block        = "10.0.5.0/24"
        availability_zone = "eu-west-1a"
      },
      {
        cidr_block        = "10.0.6.0/24"
        availability_zone = "eu-west-1b"
      },
      {
        cidr_block        = "10.0.7.0/24"
        availability_zone = "eu-west-1c"
      }
    ],
    secrets = [
      {
        cidr_block        = "10.0.8.0/24"
        availability_zone = "eu-west-1a"
      },
      {
        cidr_block        = "10.0.9.0/24"
        availability_zone = "eu-west-1b"
      },
      {
        cidr_block        = "10.0.10.0/24"
        availability_zone = "eu-west-1c"
      }
    ],
    rds = [
      {
        cidr_block        = "10.0.11.0/24"
        availability_zone = "eu-west-1a"
      },
      {
        cidr_block        = "10.0.12.0/24"
        availability_zone = "eu-west-1b"
      },
      {
        cidr_block        = "10.0.13.0/24"
        availability_zone = "eu-west-1c"
      }
    ]
  }
}

我之前使用的是count循环结构。所以我曾经将上述结构扁平化为对象列表

locals {
  subnets = flatten([
    for resource in keys(var.subnet-map) : [
      for subnet in var.subnet-map[resource] : {
        resource          = resource
        cidr_block        = subnet.cidr_block
        availability_zone = subnet.availability_zone
      }
    ]
  ])
}

然后我将通过创建资源

resource "aws_subnet" "aws-subnets" {
  count             = length(local.subnets)
  vpc_id            = aws_vpc.aws-vpc.id
  cidr_block        = local.subnets[count.index].cidr_block
  availability_zone = local.subnets[count.index].availability_zone

  tags = {
    Name = "subnet-${local.subnets[count.index].resource}-${local.subnets[count.index].availability_zone}"
  }
}

现在,我想使用for_each循环。但是我不知道该怎么做。这是我到目前为止所做的。

resource "aws_subnet" "subnets-dev" {
  for_each          = var.subnet-map
  vpc_id            = aws_vpc.vpc-dev.id
  cidr_block        = each.value.cidr_block
  availability_zone = each.value.availability_zone

  tags = {
    Name        = "subnet-dev-${each.value.resource}-${each.value.availability_zone}"
    environment = "dev"
  }
}

但是它总是显示错误信息

Error: Unsupported attribute

  on vpc/main.tf line 93, in resource "aws_subnet" "subnets-dev":
  93:     Name        = "subnet-dev-${each.value.resource}-${each.value.availability_zone}"
    |----------------
    | each.value is tuple with 3 elements

This value does not have any attributes.

我该如何解决?

1 个答案:

答案 0 :(得分:5)

我不确定我是否完全遵循您在此处尝试的所有内容,因为您最初的代码段var.subnet-map表示它是对象列表列表的映射,但是后来您使用for_each = var.subnet-map它似乎已将其视为列表的映射。在这里尝试for_each之前,您是否删除了多余的地图级别(“默认”键)?

使用variable "subnet-map"的原始定义,您使用for_each的第一步将与使用count的第一步相似:您需要将结构展平,这一次是一张地图对象而不是对象列表。最简单的方法是从现有的拼合列表中导出地图:

locals {
  subnets = flatten([
    for resource in keys(var.subnet-map) : [
      for subnet in var.subnet-map[resource] : {
        resource          = resource
        cidr_block        = subnet.cidr_block
        availability_zone = subnet.availability_zone
      }
    ]
  ])

  subnets_map = {
    for s in local.subnets: "${s.resource}:${s.availability_zone}" => s
  }
}

这里,我假设您的“资源”字符串和可用性区域一起是子网的合适唯一标识符。如果没有,您可以将"${s.resource}:${s.availability_zone}"部分调整为您要用于它们的任何唯一键。

现在,您可以将展平的地图用作for_each地图:

resource "aws_subnet" "subnets-dev" {
  for_each          = local.subnets_map
  vpc_id            = aws_vpc.vpc-dev.id
  cidr_block        = each.value.cidr_block
  availability_zone = each.value.availability_zone

  tags = {
    Name        = "subnet-dev-${each.value.resource}-${each.value.availability_zone}"
    environment = "dev"
  }
}

这将为您提供实例,其地址为aws_subnet.subnets-dev["ec2:eu-west-1a"]


请注意,如果要从count迁移到要保留的现有子网,则还需要执行一次迁移步骤,以告诉Terraform现有状态中的哪些索引对应于哪些键在新配置中。

例如,如果(并且仅当)索引0先前是ec2eu-west-1a的索引时,该索引的迁移命令为:

terraform state mv 'aws_subnet.subnets-dev[0]' 'aws_subnet.subnets-dev["ec2:eu-west-1a"]'

如果不确定它们之间的相关性,可以在添加terraform plan之后运行for_each,然后查看Terraform计划销毁的实例。如果依次处理其中的每一个,使用Terraform当前知道的地址以及Name标签中显示的资源和可用性区域名称,则可以将它们迁移到新地址,这样Terraform就不会不再认为您是在要求销毁编号的实例并将其替换为命名实例。