2
votes

I have this terraform code...

// Route53 Zone
module "zones" {
  source  = "terraform-aws-modules/route53/aws//modules/zones"
  version = "~> 1.2.0"

  create = var.environment == "PRO" ? true : false

  zones = {
    "example.com" = {
      comment = "example.com (production)"
      tags = merge(var.global_tags, { environment = var.environment})
    }
  }
}

// Route53 Records
module "records" {
  source  = "terraform-aws-modules/route53/aws//modules/records"
  version = "~> 1.2.0"

  create = var.environment == "PRO" ? true : false

  zone_name = keys(module.zones.this_route53_zone_zone_id)[0]

  records = [
    {
      name    = "ci"
      type    = "A"
      ttl     = 300
      records = [
        "34.24.14.04",
      ]
    },
  ]

  depends_on = [module.zones]
}

I select a environment different than PRO, so this modules don't create anything but... After run terraform plan I get...

Error: Invalid index

  on main.tf line 287, in module "records":
 287:   zone_name = keys(module.zones.this_route53_zone_zone_id)[0]
    |----------------
    | module.zones.this_route53_zone_zone_id is object with no attributes

The given key does not identify an element in this collection value.

How can I deal with this? I want to use my code with any value into de variable environment.

1

1 Answers

4
votes

We created a route53 module as part of our open-source terraform library at github.com/mineiros-io/terraform-aws-route53

The solution using this module would look like this:

  • create the zone for example.com
  • use a predictable way to reference the zone_id = module.zones.zone["example.com"].zone_id
  • create the records in the zone

Note: The dependencies should be resolved in a natural way so no need to use depends_on in this solution. If unexpected dependencies are needed we also offer a way for this for terraform 0.12 by using module_depends_on = [...] which will force dependencies on all resources within the module.

module "zones" {
  source  = "mineiros-io/route53/aws"
  version = "~> 0.3.0"

  # not called 'create' but 'module_enabled' in our modules.
  module_enabled = var.environment == "PRO"

  # it is possible to create multiple zones at once all sharing the same
  # delegation set (nameservers)
  name = [ 
    "example.com" ,
  ]

  comment = "example.com (${var.environment})"

  tags = merge(var.global_tags, {
    environment = var.environment
  })
}

module "records" {
  source  = "mineiros-io/route53/aws"
  version = "~> 0.3.0"

  module_enabled = var.environment == "PRO"

  zone_id = module.zones.zone["example.com"].zone_id

  records = [
    {
      name = "ci"
      type = "A"
      ttl  = 300
      records = [
        "34.24.14.04",
      ]
    },
  ]
}

see some more complex examples here: github.com/mineiros-io/terraform-aws-route53/tree/master/examples

The plan looks something like:

  # module.records.aws_route53_record.record["a-ci"] will be created
  + resource "aws_route53_record" "record" {
      + allow_overwrite = false
      + fqdn            = (known after apply)
      + id              = (known after apply)
      + name            = "ci"
      + records         = [
          + "34.24.14.04",
        ]
      + ttl             = 300
      + type            = "A"
      + zone_id         = (known after apply)
    }

  # module.zones.aws_route53_delegation_set.delegation_set[0] will be created
  + resource "aws_route53_delegation_set" "delegation_set" {
      + id           = (known after apply)
      + name_servers = (known after apply)
    }

  # module.zones.aws_route53_zone.zone["example.com"] will be created
  + resource "aws_route53_zone" "zone" {
      + comment           = "example.com (PRO)"
      + delegation_set_id = (known after apply)
      + force_destroy     = false
      + id                = (known after apply)
      + name              = "example.com"
      + name_servers      = (known after apply)
      + tags              = {
          + "Name"        = "example.com"
          + "environment" = "PRO"
        }
      + zone_id           = (known after apply)
    }

Plan: 3 to add, 0 to change, 0 to destroy.

Note: Just to be clear you can also just merge the two calls of the module and create the zone and records within the same module call. I just splitted the usage to match your initial example.