Terraform: create and depends_on together

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.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top