【问题标题】:Using terraform 0.12 functions to fetch a value from list of maps使用 terraform 0.12 函数从地图列表中获取值
【发布时间】:2020-03-20 05:43:15
【问题描述】:

使用Auth0 provider 我需要创建一个custom_domain。为了对其进行验证,用户需要使用在此过程中生成的 CNAME 创建 DNS 记录。 verification 资源在 tfstate 文件中如下所示:

"verification": [
  {
    "methods": [
      {
        "name": "cname",
        "record": "some-random-cname.auth0.com"
      }
    ]
  }
],
  • 到目前为止,我能够获得一组地图(根据 tfstate 文件),但仍然无法获得 record

resource "auth0_custom_domain" "main" {
  domain              = "custom.example.com"
  type                = "auth0_managed_certs"
  verification_method = "txt"
}

locals {
  something = flatten(auth0_custom_domain.main.verification[*].methods)
}

output "my-local" {
  value = local.something
}
  • 输出如下所示:
my-local = [
  {
    "name" = "cname"
    "record" = "some-random-cname.auth0.com"
  },
]
  • 看来我已经接近获得该记录并将其用作 route53 条目的输入,但我无法获得它,任何帮助将不胜感激。

【问题讨论】:

  • 我终于设法做到了,但如果有更好的解决方案,我会保持打开状态:something = lookup(element(flatten(auth0_custom_domain.main.verification[*].methods), 0), "record")
  • 您应该考虑将其发布为自我回答,然后看看是否有人能想到更好的方法。不过,这对我来说看起来很合理。

标签: terraform


【解决方案1】:

正如ydaetskcoR 建议的那样,这是我设法解决的方法,但在我看来它太复杂了,所以我想看看是否有人有比这更好的解决方案:

something = lookup(element(flatten(auth0_custom_domain.main.verification[*].methods), 0), "record")

【讨论】:

  • 我在这里建议的唯一进一步改进是使用索引和属性访问语法,而不是 lookupelement 函数:auth0_custom_domain.main.verification[0].methods[0].record。请注意,这假设您只关心第一个 method 对象内的第一个 verification 对象;它会忽略任何其他的。
  • @MartinAtkins 我记得这是我尝试的第一种方法,但它没有用'
猜你喜欢
  • 1970-01-01
  • 2020-08-11
  • 1970-01-01
  • 2021-01-23
  • 1970-01-01
  • 2019-11-05
  • 1970-01-01
  • 2011-05-10
  • 2021-10-06
相关资源
最近更新 更多