在资源创建中使用地图列表的地图 [英] Use a map of lists of maps in resource creation

查看:17
本文介绍了在资源创建中使用地图列表的地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用 TF Vars 中定义的以下格式创建多个 R53 记录:

I want to be able to create multiple R53 records, using the following format defined in TF Vars:

custom_zone_records = {
  "demo.a.com" = [
    {"*.b" = {type: "CNAME", records: ["d.com"]}},
    {"*.c" = {type: "CNAME", records: ["d.com"]}}
  ]
}

以记录为例,应该创建R53记录:

Using the record as an example, it should create the R53 record of:

Name: *.b.demo.a.com
Type: CNAME
Records: ["d.com"]

输入变量定义为:

variable "custom_zone_records" {
  description = "A map of maps for custom zone records"
  type = map(list(map(object({
    type = string
    records: list(string)
  }))))
}

我尝试使用 for_each 在资源创建中改变数组,并尝试使用本地文件进行格式化,但没有成功.

I've tried mutating the array within the resource creation using a for_each and I've tried formatting using a locals file, with no success.

除了让数组更明确之外,还有更好的格式化或处理方法吗?

Is there a better way to format or handle this, other than having the array more explicit?

谢谢.

如果我不使用该变量,我将不得不编写以下代码(作为我想要实现的示例)

If I were to not use the variable, I would have to write the following code (as an example of what I am trying to achieve)

resource "aws_route53_record" "demo_a" {
  zone_id = "ZONE_ID"
  name    = "*.b.demo.a.com"
  type    = "CNAME"
  records = ["d.com"]
  ttl     = 60
}

resource "aws_route53_record" "demo_a" {
  zone_id = "ZONE_ID"
  name    = "*.c.demo.a.com"
  type    = "CNAME"
  records = ["d.com"]
  ttl     = 60
}

推荐答案

实现这一点的一种方法(假设我正确理解了这个问题)是创建一个辅助局部变量,它可以展开".将 custom_zone_records 放入对 for_each 更友好的列表中.

One way to do this (assuming I correctly understand the issue), would be to create an a helper local variable which would "unwind" the custom_zone_records into more for_each-friendly list.

在下面的例子中,它被称为helper_list:

In the following example, it is called helper_list:

locals {
  custom_zone_records = {
    "demo.a.com" = [
      {"*.b" = {type: "CNAME", records: ["d.com"]}},
      {"*.c" = {type: "CNAME", records: ["d.com"]}}
    ],
    "demo1.a.com" = [
      {"*.b1" = {type: "CNAME", records: ["d1.com"]}},
      {"*.c1" = {type: "CNAME", records: ["d1.com"]}}
    ]    
  }
  
  helper_list = flatten([for k, v in local.custom_zone_records: 
            [for v1 in v: 
              merge({name = join("", [keys(v1)[0], ".", k])}, values(v1)[0])
           ]
      ])
  
}

helper_list 的格式为:

helper_list = [                     
  {                          
    "name" = "*.b.demo.a.com"
    "records" = [   
      "d.com",               
    ]               
    "type" = "CNAME"         
  },             
  {                          
    "name" = "*.c.demo.a.com"
    "records" = [   
      "d.com",
    ]               
    "type" = "CNAME"
  },
  {
    "name" = "*.b1.demo1.a.com"
    "records" = [
      "d1.com",
    ]
    "type" = "CNAME"
  },
  {
    "name" = "*.c1.demo1.a.com"
    "records" = [
      "d1.com",
    ]
    "type" = "CNAME"
  },
]

这样,for_each的用法会更简单,可能是(以下我没有验证,所以把它当作伪代码):

This way, the for_each usage would be simpler, and could be (I haven't verified the following, so treat it as a pseudo-code):

resource "aws_route53_record" "demo" {

  for_each = toset(local.helper_list)

  zone_id = "ZONE_ID"

  name    = each.value.name
  type    = each.value.type
  records = each.value.records

  ttl     = 60
}

这篇关于在资源创建中使用地图列表的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆