在Terraform中循环遍历一个地图内的地图,获取键名

huangapple go评论67阅读模式
英文:

Getting the key name in a loop through a map of maps in terraform

问题

在给定的Terraform变量中:

变量 "db_alarm_metrics" 包含如下内容:
```plaintext
{
  "ReadIOPS" = {
    "threshold" = "20",
    "period"    = "60000"
  },
  "WriteIOPS" = {
    "threshold" = "20",
    "period"    = "60000"
  }
}

变量 "aggregation_db_primary_db_instance_id" 包含如下内容:

mysql-aggregation

我想对它们进行一种嵌套的循环处理,因此我创建了以下本地资源,以便为我创建一个包含两者的列表进行循环处理。

locals {
  instance_metrics = flatten([
    for instance_type in [ var.aggregation_db_primary_db_instance_id, "${var.aggregation_db_primary_db_instance_id}-replica" ] : [
      for metric_name, metric in var.db_alarm_metrics : {
        instance_type = instance_type
        metric_name   = metric_name
        metric        = metric
      }
    ]
  ])
}

terraform console 中,如果我运行 local.instance_metrics 来测试输出结果,我会得到如下:

{
  "instance_type" = "mysql-aggregation",
  "metric_name"   = "ReadIOPS",
  "metric"        = {
    "period"    = "60000",
    "threshold" = "20"
  }
},
{
  "instance_type" = "mysql-aggregation",
  "metric_name"   = "WriteIOPS",
  "metric"        = {
    "period"    = "60000",
    "threshold" = "20"
  }
},
{
  "instance_type" = "mysql-aggregation-replica",
  "metric_name"   = "ReadIOPS",
  "metric"        = {
    "period"    = "60000",
    "threshold" = "20"
  }
},
{
  "instance_type" = "mysql-aggregation-replica",
  "metric_name"   = "WriteIOPS",
  "metric"        = {
    "period"    = "60000",
    "threshold" = "20"
  }
}

与内部映射的 "metric" 作为键不同,我想要来自 var.db_alarm_metrics 的键名作为键。期望输出如下:

{
  "instance_type" = "mysql-aggregation",
  "ReadIOPS" = {
    "period"    = "60000",
    "threshold" = "20"
  }
},
{
  "instance_type" = "mysql-aggregation",
  "WriteIOPS" = {
    "period"    = "60000",
    "threshold" = "20"
  }
},
{
  "instance_type" = "mysql-aggregation-replica",
  "ReadIOPS" = {
    "period"    = "60000",
    "threshold" = "20"
  }
},
{
  "instance_type" = "mysql-aggregation-replica",
  "WriteIOPS" = {
    "period"    = "60000",
    "threshold" = "20"
  }
}

我做错了什么?谢谢!


<details>
<summary>英文:</summary>

Given the following two variables in terraform:

variable "db_alarm_metrics" {
type = map(any)
description = "Database metrics to create alarms for"
default = {
"ReadIOPS" = {
"threshold" = "20",
"period" = "60000"
},
"WriteIOPS" = {
"threshold" = "20",
"period" = "60000"
}
}
}

variable "aggregation_db_primary_db_instance_id" {
type = string
description = ""
default = "mysql-aggregation"
}

I want to loop through a kind of nested for_each for the two of them, so I made the following local resource to create a list of the two combined for me to loop through.

locals {
instance_metrics = flatten([
for instance_type in [ var.aggregation_db_primary_db_instance_id, "${var.aggregation_db_primary_db_instance_id}-replica" ] : [
for metric in var.db_alarm_metrics : {
instance_type = instance_type
metric = metric
}
]
])
}

In `terraform console` if I run `local.instance_metrics` to test the output of this, I get

{
"instance_type" = "mysql-aggregation"
"metric" = {
"period" = "60000"
"threshold" = "20"
}
},
{
"instance_type" = "mysql-aggregation"
"metric" = {
"period" = "60000"
"threshold" = "20"
}
},
{
"instance_type" = "mysql-aggregation-replica"
"metric" = {
"period" = "60000"
"threshold" = "20"
}
},
{
"instance_type" = "mysql-aggregation-replica"
"metric" = {
"period" = "60000"
"threshold" = "20"
}
},


Instead of &quot;metric&quot; as the key for those inner maps, I want the key name from `var.db_alarm_metrics`. Expected output:

{
"instance_type" = "mysql-aggregation"
"ReadIOPS" = {
"period" = "60000"
"threshold" = "20"
}
},
{
"instance_type" = "mysql-aggregation"
"WriteIOPS" = {
"period" = "60000"
"threshold" = "20"
}
},
{
"instance_type" = "mysql-aggregation-replica"
"ReadIOPS" = {
"period" = "60000"
"threshold" = "20"
}
},
{
"instance_type" = "mysql-aggregation-replica"
"WriteIOPS" = {
"period" = "60000"
"threshold" = "20"
}
},

What am I doing wrong? TIA!

</details>


# 答案1
**得分**: 3

```hcl
  instance_metrics = flatten([
    for instance_type in [var.aggregation_db_primary_db_instance_id, "${var.aggregation_db_primary_db_instance_id}-replica"] : [
      for type, metric in var.db_alarm_metrics : {
        instance_type = instance_type
        "${type}"     = metric
      }
    ]
  ])

请注意,您可以引用指标类型,因为变量 db_alarm_metrics 也是一个映射。这将在 terraform 控制台中产生以下输出:

> local.instance_metrics
[
  {
    "ReadIOPS" = {
      "period" = "60000"
      "threshold" = "20"
    }
    "instance_type" = "mysql-aggregation"
  },
  {
    "WriteIOPS" = {
      "period" = "60000"
      "threshold" = "20"
    }
    "instance_type" = "mysql-aggregation"
  },
  {
    "ReadIOPS" = {
      "period" = "60000"
      "threshold" = "20"
    }
    "instance_type" = "mysql-aggregation-replica"
  },
  {
    "WriteIOPS" = {
      "period" = "60000"
      "threshold" = "20"
    }
    "instance_type" = "mysql-aggregation-replica"
  },
]
英文:

You can achieve this with a slight adjustment:

  instance_metrics = flatten([
    for instance_type in [var.aggregation_db_primary_db_instance_id, &quot;${var.aggregation_db_primary_db_instance_id}-replica&quot;] : [
      for type, metric in var.db_alarm_metrics : {
        instance_type = instance_type
        &quot;${type}&quot;     = metric
      }
    ]
  ])

Note that you can reference the metric type since the variable db_alarm_metrics is a map as well. This gives the following output in the terraform console:

&gt; local.instance_metrics
[
  {
    &quot;ReadIOPS&quot; = {
      &quot;period&quot; = &quot;60000&quot;
      &quot;threshold&quot; = &quot;20&quot;
    }
    &quot;instance_type&quot; = &quot;mysql-aggregation&quot;
  },
  {
    &quot;WriteIOPS&quot; = {
      &quot;period&quot; = &quot;60000&quot;
      &quot;threshold&quot; = &quot;20&quot;
    }
    &quot;instance_type&quot; = &quot;mysql-aggregation&quot;
  },
  {
    &quot;ReadIOPS&quot; = {
      &quot;period&quot; = &quot;60000&quot;
      &quot;threshold&quot; = &quot;20&quot;
    }
    &quot;instance_type&quot; = &quot;mysql-aggregation-replica&quot;
  },
  {
    &quot;WriteIOPS&quot; = {
      &quot;period&quot; = &quot;60000&quot;
      &quot;threshold&quot; = &quot;20&quot;
    }
    &quot;instance_type&quot; = &quot;mysql-aggregation-replica&quot;
  },
]

huangapple
  • 本文由 发表于 2023年4月7日 02:05:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定