Terraform:嵌套的for表达式,无重复。

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

Terraform: Nested for expression with no duplicity

问题

我是Terraform新手,在不当的逻辑使用情况下请提前谅解。

有一个嵌套映射,希望使用现有映射创建新映射。

terraform.tfvars

vpcs_info= {
  "devops" = {
    main = {
      cidr = "10.14.0.0/16"
      region = "ap-south-1"
      peering = {
        creator = {
          devops = "poc",
          uat    = "main"
	    }
      }
    },
    poc = {
      cidr = "10.9.0.0/16"
      region = "ap-south-1"
      peering = {
        creator = {
          dev = "main"
	    }
      }
    }
  }
}

locas.tf

locals {
  vpcs_info = {
    for vpc, properties in var.vpcs_info.devops:
      vpc => {for dst_env, dst_vpc in properties.peering.creator : vpc => {"name": "${dst_env}-${dst_vpc}-vpc", "id": "${timestamp()}" }...}
   }
}

实际输出:

{
  "main" = {
    "main" = [
      {
        "id" = "2023-02-05T21:23:24Z"
        "name" = "devops-poc-vpc"
      },
      {
        "id" = "2023-02-05T21:23:24Z"
        "name" = "uat-main-vpc"
      },
    ]
  }
  "poc" = {
    "poc" = [
      {
        "id" = "2023-02-05T21:23:24Z"
        "name" = "dev-main-vpc"
      },
    ]
  }
}

如果我们观察,输出中存在键的重复。需要使用for表达式获得以下期望的输出:

期望输出:

{
  "main" = {
      {
        "id" = "2023-02-05T21:23:24Z"
        "name" = "devops-poc-vpc"
      },
      {
        "id" = "2023-02-05T21:23:24Z"
        "name" = "uat-main-vpc"
      }
  }
  "poc" = {
      {
        "id" = "2023-02-05T21:23:24Z"
        "name" = "dev-main-vpc"
      }
  }
}

注意: 实际需求中的"id"是敏感信息,为了避免安全问题,这里使用了"${timestamp()}"。

英文:

I am newbie to Terraform, apologies in advance in case inappropriate logic is used.

There is nested map, want to create new map using existing one.

terraform.tfvars

vpcs_info= {
  "devops" = {
    main = {
      cidr = "10.14.0.0/16"
      region = "ap-south-1"
      peering = {
        creator = {
          devops = "poc",
          uat    = "main"
	    }
      }
    },
    poc = {
      cidr = "10.9.0.0/16"
      region = "ap-south-1"
      peering = {
        creator = {
          dev = "main"
	    }
      }
    }
  }
}

locas.tf

locals {
  vpcs_info = {
    for vpc, properties in var.vpcs_info.devops:
      vpc => {for dst_env, dst_vpc in properties.peering.creator : vpc => {"name": "${dst_env}-${dst_vpc}-vpc", "id": "${timestamp()}" }...}
   }
}

Actual output:

{
  "main" = {
    "main" = [
      {
        "id" = "2023-02-05T21:23:24Z"
        "name" = "devops-poc-vpc"
      },
      {
        "id" = "2023-02-05T21:23:24Z"
        "name" = "uat-main-vpc"
      },
    ]
  }
  "poc" = {
    "poc" = [
      {
        "id" = "2023-02-05T21:23:24Z"
        "name" = "dev-main-vpc"
      },
    ]
  }
}

If we observe, in the output there is duplicity in KEYS. Need to get below desired output using for expression:

Desired output:

{
  "main" = {
      {
        "id" = "2023-02-05T21:23:24Z"
        "name" = "devops-poc-vpc"
      },
      {
        "id" = "2023-02-05T21:23:24Z"
        "name" = "uat-main-vpc"
      }
  }
  "poc" = {
      {
        "id" = "2023-02-05T21:23:24Z"
        "name" = "dev-main-vpc"
      }
  }
}

Note: "id" is sensitive information in actual requirement, to avoid security challenges, "${timestamp()}" is being used here.

答案1

得分: 2

你可以使用 merge 完成此操作:

  vpcs_info = merge([
    for vpc, properties in var.vpcs_info.devops: {
       for dst_env, dst_vpc in properties.peering.creator: 
          vpc => {
            "name" = "${dst_env}-${dst_vpc}-vpc"
            "id" = "${timestamp()}" 
           }...
       }
   ]...)
英文:

You can do that with merge:

  vpcs_info = merge([
    for vpc, properties in var.vpcs_info.devops: {
       for dst_env, dst_vpc in properties.peering.creator: 
          vpc => {
            "name" = "${dst_env}-${dst_vpc}-vpc"
            "id" = "${timestamp()}" 
           }...
       }
   ]...)

huangapple
  • 本文由 发表于 2023年2月6日 05:38:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355704.html
匿名

发表评论

匿名网友

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

确定