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

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

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

问题

  1. 在给定的Terraform变量中:
  2. 变量 "db_alarm_metrics" 包含如下内容:
  3. ```plaintext
  4. {
  5. "ReadIOPS" = {
  6. "threshold" = "20",
  7. "period" = "60000"
  8. },
  9. "WriteIOPS" = {
  10. "threshold" = "20",
  11. "period" = "60000"
  12. }
  13. }

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

  1. mysql-aggregation

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

  1. locals {
  2. instance_metrics = flatten([
  3. for instance_type in [ var.aggregation_db_primary_db_instance_id, "${var.aggregation_db_primary_db_instance_id}-replica" ] : [
  4. for metric_name, metric in var.db_alarm_metrics : {
  5. instance_type = instance_type
  6. metric_name = metric_name
  7. metric = metric
  8. }
  9. ]
  10. ])
  11. }

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

  1. {
  2. "instance_type" = "mysql-aggregation",
  3. "metric_name" = "ReadIOPS",
  4. "metric" = {
  5. "period" = "60000",
  6. "threshold" = "20"
  7. }
  8. },
  9. {
  10. "instance_type" = "mysql-aggregation",
  11. "metric_name" = "WriteIOPS",
  12. "metric" = {
  13. "period" = "60000",
  14. "threshold" = "20"
  15. }
  16. },
  17. {
  18. "instance_type" = "mysql-aggregation-replica",
  19. "metric_name" = "ReadIOPS",
  20. "metric" = {
  21. "period" = "60000",
  22. "threshold" = "20"
  23. }
  24. },
  25. {
  26. "instance_type" = "mysql-aggregation-replica",
  27. "metric_name" = "WriteIOPS",
  28. "metric" = {
  29. "period" = "60000",
  30. "threshold" = "20"
  31. }
  32. }

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

  1. {
  2. "instance_type" = "mysql-aggregation",
  3. "ReadIOPS" = {
  4. "period" = "60000",
  5. "threshold" = "20"
  6. }
  7. },
  8. {
  9. "instance_type" = "mysql-aggregation",
  10. "WriteIOPS" = {
  11. "period" = "60000",
  12. "threshold" = "20"
  13. }
  14. },
  15. {
  16. "instance_type" = "mysql-aggregation-replica",
  17. "ReadIOPS" = {
  18. "period" = "60000",
  19. "threshold" = "20"
  20. }
  21. },
  22. {
  23. "instance_type" = "mysql-aggregation-replica",
  24. "WriteIOPS" = {
  25. "period" = "60000",
  26. "threshold" = "20"
  27. }
  28. }

我做错了什么?谢谢!

  1. <details>
  2. <summary>英文:</summary>
  3. 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"
}

  1. 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
}
]
])
}

  1. 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"
}
},

  1. 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"
}
},

  1. What am I doing wrong? TIA!
  2. </details>
  3. # 答案1
  4. **得分**: 3
  5. ```hcl
  6. instance_metrics = flatten([
  7. for instance_type in [var.aggregation_db_primary_db_instance_id, "${var.aggregation_db_primary_db_instance_id}-replica"] : [
  8. for type, metric in var.db_alarm_metrics : {
  9. instance_type = instance_type
  10. "${type}" = metric
  11. }
  12. ]
  13. ])

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

  1. > local.instance_metrics
  2. [
  3. {
  4. "ReadIOPS" = {
  5. "period" = "60000"
  6. "threshold" = "20"
  7. }
  8. "instance_type" = "mysql-aggregation"
  9. },
  10. {
  11. "WriteIOPS" = {
  12. "period" = "60000"
  13. "threshold" = "20"
  14. }
  15. "instance_type" = "mysql-aggregation"
  16. },
  17. {
  18. "ReadIOPS" = {
  19. "period" = "60000"
  20. "threshold" = "20"
  21. }
  22. "instance_type" = "mysql-aggregation-replica"
  23. },
  24. {
  25. "WriteIOPS" = {
  26. "period" = "60000"
  27. "threshold" = "20"
  28. }
  29. "instance_type" = "mysql-aggregation-replica"
  30. },
  31. ]
英文:

You can achieve this with a slight adjustment:

  1. instance_metrics = flatten([
  2. for instance_type in [var.aggregation_db_primary_db_instance_id, &quot;${var.aggregation_db_primary_db_instance_id}-replica&quot;] : [
  3. for type, metric in var.db_alarm_metrics : {
  4. instance_type = instance_type
  5. &quot;${type}&quot; = metric
  6. }
  7. ]
  8. ])

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:

  1. &gt; local.instance_metrics
  2. [
  3. {
  4. &quot;ReadIOPS&quot; = {
  5. &quot;period&quot; = &quot;60000&quot;
  6. &quot;threshold&quot; = &quot;20&quot;
  7. }
  8. &quot;instance_type&quot; = &quot;mysql-aggregation&quot;
  9. },
  10. {
  11. &quot;WriteIOPS&quot; = {
  12. &quot;period&quot; = &quot;60000&quot;
  13. &quot;threshold&quot; = &quot;20&quot;
  14. }
  15. &quot;instance_type&quot; = &quot;mysql-aggregation&quot;
  16. },
  17. {
  18. &quot;ReadIOPS&quot; = {
  19. &quot;period&quot; = &quot;60000&quot;
  20. &quot;threshold&quot; = &quot;20&quot;
  21. }
  22. &quot;instance_type&quot; = &quot;mysql-aggregation-replica&quot;
  23. },
  24. {
  25. &quot;WriteIOPS&quot; = {
  26. &quot;period&quot; = &quot;60000&quot;
  27. &quot;threshold&quot; = &quot;20&quot;
  28. }
  29. &quot;instance_type&quot; = &quot;mysql-aggregation-replica&quot;
  30. },
  31. ]

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:

确定