Terraform – 如何将三个列表组合在一起以构建一个地图列表

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

Terraform - How to combine three lists together to build a list of maps

问题

以下是您要翻译的内容:

我编写了以下代码来展开一个包含列表的列表,但它没有产生我期望的结果。

locals {

    alarms = {
        names = ["NumberOfMessagesReceived", "NumberOfMessagesDeleted", "ApprNumberOfMessagesDelayed",
                 "NumberOfMessagesSent", "NumberOfEmptyReceives", "ApprNumberOfMessagesNotVisible"],

        thresholds = ["1000", "50", "100", "100", "100", "50"]
    }

    sqs_queues = {
        queues = ["default", "general"]
    }

    alarms2 = flatten([

        for name, threshold in local.alarms : [

            for sqs_queue in local.sqs_queues : {

                alarm_name = name
                threshold_value = threshold
                sqs_queue_name = sqs_queue

            }

        ]

    ])
}

期望的输出是,

alarm_name = "NumberOfMessagesReceived"
threshold_value = "1000"
sqs_queue_name = "default"

alarm_name = "NumberOfMessagesReceived"
threshold_value = "1000"
sqs_queue_name = "general"

报警名称应与相应的阈值关联。每个报警名称和阈值的组合都应与每个sqs_queue关联...

英文:

I wrote code as below to flatten a list of lists, but it’s not producing me the expected results.

locals {

    alarms = {
        names = ["NumberOfMessagesReceived", "NumberOfMessagesDeleted", "ApprNumberOfMessagesDelayed",
                 "NumberOfMessagesSent", "NumberOfEmptyReceives", "ApprNumberOfMessagesNotVisible"],

        thresholds = ["1000", "50", "100", "100", "100", "50"]
    }

    sqs_queues = {
        queues = ["default", "general"]
    }

    alarms2 = flatten([

        for name, threshold in local.alarms : [

            for sqs_queue in local.sqs_queues : {

                alarm_name = name
                threshold_value = threshold
                sqs_queue_name = sqs_queue

            }

        ]

    ])
}

The expected output is,

alarm_name = "NumberOfMessagesReceived"
threshold_value = "1000"
sqs_queue_name = ""default"

alarm_name = "NumberOfMessagesReceived"
threshold_value = "1000"
sqs_queue_name = ""general"

The alarm name should be associate with corresponding threshold. And every combination of alarm name and threshold should be associated with each sqs_queue...

答案1

得分: 1

您可以使用setproduct来生成local.alarms.nameslocal.sqs_queues.queues之间的所有可能组合,然后通过迭代结果,可以使用索引访问与名称对应的阈值值:

locals {

  alarms = {
    names = ["NumberOfMessagesReceived", "NumberOfMessagesDeleted", "ApprNumberOfMessagesDelayed",
    "NumberOfMessagesSent", "NumberOfEmptyReceives", "ApprNumberOfMessagesNotVisible"],

    thresholds = ["1000", "50", "100", "100", "100", "50"]
  }

  sqs_queues = {
    queues = ["default", "general"]
  }

  alarms2 = [
    for i,v in setproduct(local.alarms.names,local.sqs_queues.queues) : {
      alarm_name = v[0]
      threshold_value = element(local.alarms.thresholds,index(local.alarms.names,v[0]))
      sqs_queue_name = v[1]
    }
  ]
}
$ terraform console
> local.alarms2
[
  {
    "alarm_name" = "NumberOfMessagesReceived"
    "sqs_queue_name" = "default"
    "threshold_value" = "1000"
  },
  {
    "alarm_name" = "NumberOfMessagesReceived"
    "sqs_queue_name" = "general"
    "threshold_value" = "1000"
  },
  {
    "alarm_name" = "NumberOfMessagesDeleted"
    "sqs_queue_name" = "default"
    "threshold_value" = "50"
  },
  {
    "alarm_name" = "NumberOfMessagesDeleted"
    "sqs_queue_name" = "general"
    "threshold_value" = "50"
  },
  ...
英文:

You can use setproduct to generate all possible combinations between local.alarms.names and local.sqs_queues.queues, and by iterating over the result you can use the index to access the threshold value corresponding to the name:

locals {

  alarms = {
    names = ["NumberOfMessagesReceived", "NumberOfMessagesDeleted", "ApprNumberOfMessagesDelayed",
    "NumberOfMessagesSent", "NumberOfEmptyReceives", "ApprNumberOfMessagesNotVisible"],

    thresholds = ["1000", "50", "100", "100", "100", "50"]
  }

  sqs_queues = {
    queues = ["default", "general"]
  }

  alarms2 = [
    for i,v in setproduct(local.alarms.names,local.sqs_queues.queues) : {
      alarm_name = v[0]
      threshold_value = element(local.alarms.thresholds,index(local.alarms.names,v[0]))
      sqs_queue_name = v[1]
    }
  ]
}
$ terraform console
> local.alarms2
[
  {
    "alarm_name" = "NumberOfMessagesReceived"
    "sqs_queue_name" = "default"
    "threshold_value" = "1000"
  },
  {
    "alarm_name" = "NumberOfMessagesReceived"
    "sqs_queue_name" = "general"
    "threshold_value" = "1000"
  },
  {
    "alarm_name" = "NumberOfMessagesDeleted"
    "sqs_queue_name" = "default"
    "threshold_value" = "50"
  },
  {
    "alarm_name" = "NumberOfMessagesDeleted"
    "sqs_queue_name" = "general"
    "threshold_value" = "50"
  },
  ...

答案2

得分: -2

在Terraform中,您可以使用concat函数将列表组合在一起。这个函数将多个列表连接成一个单独的列表。

以下是如何使用它的示例:

variable "list1" {
  default = ["item1", "item2", "item3"]
}

variable "list2" {
  default = ["item4", "item5", "item6"]
}

variable "list3" {
  default = ["item7", "item8", "item9"]
}

output "combined_list" {
  value = concat(var.list1, var.list2, var.list3)
}

在这个示例中,list1、list2和list3是包含不同列表的变量。concat函数在combined_list输出中用于将这三个列表合并成一个。

当您运行terraform apply时,combined_list输出将显示合并后的列表:

combined_list = ["item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9"]

这是在Terraform中合并列表的一种简单方法。您只需要确保要合并的所有列表具有相同的类型,因为Terraform不支持混合类型的列表。

英文:

In Terraform, you can combine lists together by using the concat function. This function concatenates multiple lists into a single list.

Here's an example of how you can use it:

variable "list1" {
  default = ["item1", "item2", "item3"]
}

variable "list2" {
  default = ["item4", "item5", "item6"]
}

variable "list3" {
  default = ["item7", "item8", "item9"]
}

output "combined_list" {
  value = concat(var.list1, var.list2, var.list3)
}

In this example, list1, list2, and list3 are variables holding different lists. The concat function is used in the combined_list output to merge all three lists into one.

When you run terraform apply, the combined_list output will display the combined list:

combined_list = [  "item1",  "item2",  "item3",  "item4",  "item5",  "item6",  "item7",  "item8",  "item9",]

This is a straightforward way to combine lists in Terraform. You just have to ensure that all the lists you want to combine are of the same type, as Terraform does not support lists with mixed types.

huangapple
  • 本文由 发表于 2023年7月27日 22:52:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780967.html
匿名

发表评论

匿名网友

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

确定