Terraform 带有可选值的Yamldecode

Terraform 带有可选值的Yamldecode,terraform,Terraform,我想在YAML文件中存储Route53条目。但是,我要考虑的记录也可以是ELB的别名。仅当YAML文件中的条目属于一种类型时,代码才起作用(记录或别名,文件中不能同时包含这两种类型)。我正在使用以下模块 YAML文件(工作正常) YAML文件(不工作) 错误: Error: Inconsistent conditional result types on .terraform/modules/records/modules/records/main.tf line 15, in reso

我想在YAML文件中存储Route53条目。但是,我要考虑的记录也可以是ELB的别名。仅当YAML文件中的条目属于一种类型时,代码才起作用(
记录
别名
,文件中不能同时包含这两种类型)。我正在使用以下模块

YAML文件(工作正常)

YAML文件(不工作)

错误:

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循环在
本地人中构建了一个映射。我所要做的就是把
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
}

错误发生的原因是此行
records=lookup(r,“records”,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", [])
      }]
    }

您的
zone\u数据
与模块的关系如何?你是如何使用这个模块的?很抱歉,更新了更多的代码,我修复了它。谢谢你的帮助。@sdot257那么如果我的答案没有帮助,问题是什么?不是你的代码没有帮助,而是我在做什么。我的原始帖子底部有一个更新。@sdot257谢谢你让我知道。你可以回答自己的问题并接受它。最好在问题中提供解决方案作为答案。
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.
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 = {
      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", [])
      }]
    }