0
votes

I want to store Route53 entries in a YAML file. However, I'm accounting for records that can also be an alias to let's say an ELB. The code only works if the entries in the YAML file is of one type (record or an alias, you can't have both in the file). I'm using the following module.

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 file (Works fine)

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 file (Doesn't work)

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:

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.

UPDATE:

I broke the code and module apart and realized my mistake. In the module, the author turned the records into a map using tomap. I was doubling the work but using a for loop to build a map within locals. All i had to do was pass in the records from the 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
}
1
How your zone_data relates to the module? How do you use the module?Marcin
Sorry about that, updated with more codesdot257

1 Answers

0
votes

The error happens due to this line records = lookup(r, "records", null). This is because when you mix records and alias, records will be once list and once null.

To overcome the issue, make it empty list, not 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", [])
      }]
    }