迭代两个变量的值

问题描述 投票:0回答:1

我正在使用 terraform alb 社区模块将 IP 分配给子网。我设法分三组完成(如下)。有没有一种方法可以使用

for
for_each
循环来完成一个括号?谢谢!

    subnet_mapping =  [
    {
      allocation_id = var.allocation_id[0]
      subnet_id = var.public_subnets[0]
    },
    {
      allocation_id = var.allocation_id[1]
      subnet_id = var.public_subnets[1]
    },
    {
      allocation_id = var.allocation_id[2]
      subnet_id = var.public_subnets[2]
    }
    ]

变量.tf

variable "public_subnets" {
 type        = list(string)
 description = "Public Subnet ids"
 default     = ["subnet-0b11fb63b479xxxx", "subnet-0f401893ad414xxx", "subnet-0887c57b023fxxx"]
}
variable "allocation_id" {
    type = list(string)
    description = "List of allocation ids of elastic IPs"
    default = ["eipalloc-058fa2edc336f1xxx","eipalloc-012227feb7cf9xxx","eipalloc-075bf28dc2b5cxxx"]
}
for-loop variables foreach terraform
1个回答
0
投票

创建新的“合并”变量解决了我的问题

variable "subnet_allocation" {
    type = list(map(string))
    description = "List of allocation ids of elastic IPs"
    default = [
        {
            subnet_id = "subnet-03a4e8690b5903xxx"
            allocation_id = "eipalloc-058fa2edc336f1xxx"
        },
        {
            subnet_id = "subnet-0a656c5cc7f1xxx8"
            allocation_id = "eipalloc-012227feb7cxxx3"
        },
        {
            subnet_id = "subnet-07b8b64c8affxxx3"
            allocation_id = "eipalloc-075bf28dc2bxxx7"
        }
    ]
}

并将主要代码更改为

    subnet_mapping = var.subnet_allocation
© www.soinside.com 2019 - 2024. All rights reserved.