英文:
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 "metric" 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, "${var.aggregation_db_primary_db_instance_id}-replica"] : [
for type, metric in var.db_alarm_metrics : {
instance_type = instance_type
"${type}" = 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:
> 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"
},
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论