【问题标题】:Yamldecode with optional value带有可选值的 Yamldecode
【发布时间】:2021-06-30 09:55:58
【问题描述】:

我想将 Route53 条目存储在 YAML 文件中。但是,我正在考虑也可以是别名的记录,比如说 ELB。该代码仅在 YAML 文件中的条目属于一种类型(recordalias,文件中不能同时包含两者)时才有效。我正在使用以下模块。

https://github.com/terraform-aws-modules/terraform-aws-route53/blob/master/modules/records/main.tf

locals {
  zone_data_raw = yamldecode(file("${path.module}//zone_data.yaml"))
  zone_data = {
      zone_id = local.zone_data_raw.zone_id
      records = [for r in local.zone_data_raw.records : {
        name    = r.name
        type    = r.type
        ttl     = lookup(r, "ttl", null)
        alias   = lookup(r, "alias", {})
        records = lookup(r, "records", null)
      }]
    }
}

module "records" {
  source  = "terraform-aws-modules/route53/aws//modules/records"
  zone_id = local.zone_data_raw.zone_id
  private_zone = true
  records = local.zone_data.records
}

YAML 文件(工作正常)

zone_id: Z12345678
records:
- name: route53-alias1.example.com
  type: A
  alias:
    name: abc.com
    zone_id: Z12345678
- name: route53-alias.example.com
  type: A
  alias:
    name: alias.elb.amazon.com
    zone_id: Z12345678

YAML 文件(不起作用)

zone_id: Z12345678
records:
- name: route53-alias1.example.com
  type: A
  records:
  - 192.168.0.1
- name: route53-alias.example.com
  type: A
  alias:
    name: alias.elb.amazon.com
    zone_id: Z12345678

错误:

Error: Inconsistent conditional result types

  on .terraform/modules/records/modules/records/main.tf line 15, in resource "aws_route53_record" "this":
  15:   for_each = var.create && (var.zone_id != null || var.zone_name != null) ? local.recordsets : tomap({})
    |----------------
    | local.recordsets is object with 2 attributes

The true result value has the wrong type: object is required.

更新:

我将代码和模块分开并意识到我的错误。在模块中,作者使用tomap将记录变成了地图。我将工作加倍,但使用 for 循环在 locals 内构建地图。我所要做的就是从yamldecode 传递记录。

locals {
  workspace_defaults  = file("../_${terraform.workspace}.yaml")
  common_settings = merge(
    yamldecode(local.workspace_defaults)
  )

  zone_data_raw = yamldecode(file("${path.module}//zone_data.yaml"))
}

module "records" {
  source  = "terraform-aws-modules/route53/aws//modules/records"
  zone_id = local.zone_data_raw.zone_id
  private_zone = true
  records = local.zone_data_raw.records
}

【问题讨论】:

  • 您的zone_data 与模块有何关系?您如何使用该模块?
  • 抱歉,更新了更多代码

标签: terraform


【解决方案1】:

由于这一行records = lookup(r, "records", null) 而发生错误。这是因为当您混合记录和别名时,records 将成为一次 list 和一次 null

要解决此问题,请将其设为空 list,而不是 null

  zone_data = {
      zone_id = local.zone_data_raw.zone_id
      records = [for r in local.zone_data_raw.records : {
        name    = r.name
        type    = r.type
        ttl     = lookup(r, "ttl", null)
        alias   = lookup(r, "alias", {})
        records = lookup(r, "records", [])
      }]
    }

【讨论】:

  • 我修好了。不过感谢您的帮助。
  • @sdot257 如果我的回答没有帮助,问题是什么?
  • 不是你的代码没有帮助,而是我正在做的事情。我在原始帖子的底部有更新。
  • @sdot257 感谢您告诉我。您可以回答自己的问题并接受它。最好在问题中提供解决方案作为答案。
猜你喜欢
  • 2013-10-05
  • 2013-05-18
  • 2016-02-23
  • 1970-01-01
  • 2020-09-08
  • 1970-01-01
  • 2020-12-08
  • 2019-02-20
  • 2022-01-22
相关资源
最近更新 更多