【问题标题】:How to iterate through list of maps in terraform while creating locals?如何在创建本地人时遍历 terraform 中的地图列表?
【发布时间】:2021-11-09 03:37:55
【问题描述】:

我有一个像下面这样的变量

subnets = [
{      
      app_name = "app1" 
      subnets = {
       "us-west-1a" = ["10.85.1.128/26", "10.85.1.192/26", "10.85.2.128/26", "10.85.2.192/26", "10.85.3.64/26", "10.85.3.128/26", "10.85.3.192/26", "10.85.4.0/26", "10.85.4.64/26", "10.85.4.128/26", "10.85.4.192/26", "10.85.5.0/26", "10.85.5.64/26", "10.85.5.128/26", "10.85.6.64/26", "10.85.6.128/26", "10.85.6.192/26"]
       "us-west-1b" = ["10.85.1.128/26", "10.85.1.192/26", "10.85.2.128/26", "10.85.2.192/26", "10.85.3.64/26", "10.85.3.128/26", "10.85.3.192/26", "10.85.4.0/26", "10.85.4.64/26", "10.85.4.128/26", "10.85.4.192/26", "10.85.5.0/26", "10.85.5.64/26", "10.85.5.128/26", "10.85.6.64/26", "10.85.6.128/26", "10.85.6.192/26"]
       "us-west-1c" = ["10.85.1.128/26", "10.85.1.192/26", "10.85.2.128/26", "10.85.2.192/26", "10.85.3.64/26", "10.85.3.128/26", "10.85.3.192/26", "10.85.4.0/26", "10.85.4.64/26", "10.85.4.128/26", "10.85.4.192/26", "10.85.5.0/26", "10.85.5.64/26", "10.85.5.128/26", "10.85.6.64/26", "10.85.6.128/26", "10.85.6.192/26"]
      }
},

     {
      app_name = "app2"
      subnets = {
       "us-west-1a" = ["10.85.1.128/26", "10.85.1.192/26", "10.85.2.128/26", "10.85.2.192/26", "10.85.3.64/26", "10.85.3.128/26", "10.85.3.192/26", "10.85.4.0/26", "10.85.4.64/26", "10.85.4.128/26", "10.85.4.192/26", "10.85.5.0/26", "10.85.5.64/26", "10.85.5.128/26", "10.85.6.64/26", "10.85.6.128/26", "10.85.6.192/26"]
       "us-west-1b" = ["10.86.1.128/26", "10.86.1.192/26", "10.86.2.128/26", "10.86.2.192/26", "10.86.3.64/26", "10.86.3.128/26", "10.86.3.192/26", "10.86.4.0/26", "10.86.4.64/26", "10.86.4.128/26", "10.86.4.192/26", "10.86.5.0/26", "10.86.5.64/26", "10.86.5.128/26", "10.86.6.64/26", "10.86.6.128/26", "10.86.6.192/26"]
      }
    }
  ]

我想创建一个应该给出如下值的本地

{app_name = app1
 cidr_block = "10.85.1.128/26"
 az = us-west-1a}
{app_name = app1
 cidr_block = "10.85.1.192/26"
 az = us-west-1a}
.............
{app_name = app2
 cidr_block = "10.85.1.128/26"
 az = us-west-1a}
{app_name = app2
     cidr_block = "10.86.1.128/26"
     az = us-west-1b}

我尝试如下。但我无法拆分列表并再进行 1 次迭代

locals {
int_subnets = toset(flatten([
for app in var.subnets: [
 for az,cidr in app.subnets : {
 app_name = app.app_name
 cidr_block = cidr
 az = az
 }
 ]]))

resource "aws_subnet" "example" {
  for_each = {
    for s in local.int_subnets : "${s.app_name} ${s.cidr_block}" => s
  }

  vpc_id            = var.vpc_id
  cidr_block        = each.value.cidr_block
  availability_zone = each.value.az

  tags = {
    Name = "${each.value.app_name}-Int-${split("-", each.value.az)[2]}"
  }
}
     }

遇到错误

on main.tf line 49, in resource "aws_subnet" "example":
│   49:     for s in local.int_subnets : "${s.app_name} ${s.cidr_block}" => s
│     ├────────────────
│     │ s.cidr_block is tuple with 17 elements
│ 
│ Cannot include the given value in a string template: string required.

请让我知道是否可以再进行 1 次迭代并将 cidr 阻止列表拆分为字符串,或者我们是否有其他更好的方法

【问题讨论】:

  • 您的“一个应该给出如下价值的本地人”的例子并不完全清楚。您想为每个 AZ 或所有 AZ 拥有第一个 CIDR?即您的示例在 app1 和 app2 中具有不同的值

标签: terraform


【解决方案1】:

我相信你可能想要这样的东西:

locals {
  int_subnets = toset(flatten([
    for app in var.subnets : [
      for az in keys(app.subnets) : [
        for subnet in flatten(app.subnets[az]) : [
          { "app_name" : app.app_name,
            "az" : az,
            "cidr_block " : subnet
          }
        ]
      ]
    ]
  ]))
}

这将产生这种格式的输出:

int_subnet = ([
  {
    "app_name" = "app1"
    "az" = "us-west-1a"
    "cidr_block " = "10.85.1.128/26"
  },
  {
    "app_name" = "app1"
    "az" = "us-west-1a"
    "cidr_block " = "10.85.1.192/26"
  },
...
  {
    "app_name" = "app2"
    "az" = "us-west-1a"
    "cidr_block " = "10.85.1.128/26"
  },
  {
    "app_name" = "app2"
    "az" = "us-west-1a"
    "cidr_block " = "10.85.1.192/26"
  },
...

【讨论】:

    猜你喜欢
    • 2017-07-23
    • 2019-08-24
    • 2019-07-23
    • 2021-09-08
    • 2022-07-06
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    相关资源
    最近更新 更多