terraform-aws-modules/terraform-aws-route53

Invalid count argument, and zones applied 0

theherk opened this issue · 4 comments

I have the following configuration:

locals {
  group = "acc"
  tags = {
    Group     = local.group
    Env       = var.env
    Terraform = true
  }
  domain   = "${local.group}.some-domain.net"
  zone_int = "${var.env}.${local.group}.internal.${local.domain}"
  zone_ext = "${var.env}.${local.group}.${local.domain}"
}

module "zones" {
  source = "terraform-aws-modules/route53/aws//modules/zones"

  zones = {
    (local.zone_ext) = {
      comment = local.zone_ext

      tags = local.tags
    }

    (local.zone_int) = {
      comment = local.zone_int
      vpc = {
        vpc_id = module.main_vpc.vpc_id
      }

      tags = local.tags
    }
  }
}

module "records_internal" {
  source       = "terraform-aws-modules/route53/aws//modules/records"
  zone_id      = module.zones.this_route53_zone_zone_id[local.zone_int]
  private_zone = true

  records = [
    {
      name    = "account-db.${local.zone_int}"
      type    = "CNAME"
      records = [module.db.this_rds_cluster_endpoint]
    }
  ]

  depends_on = [module.zones]
}

I would expect this to create two zones: one public acc-dev.some-domain.net and acc-dev.internal.some-domain.net. The latter being in the vpc.

Then I intend to create a CNAME pointing to the db endpoint in the internal zone.

However, when I run plan, I get:

terraform plan                      

Error: Invalid count argument

  on .terraform/modules/acc_dev.records_internal/modules/records/main.tf line 7, in data "aws_route53_zone" "this":
   7:   count = var.create && (var.zone_id != null || var.zone_name != null) ? 1 : 0

The "count" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the count depends on.

Releasing state lock. This may take a few moments...

So, I attempt to build the zones first:

terraform apply -target module.zones

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Releasing state lock. This may take a few moments...

But I assure you, no zones are created, and the plan still returns the same output. What am I missing?

version info:

terraform version
Terraform v0.14.4
+ provider registry.terraform.io/hashicorp/aws v3.23.0
+ provider registry.terraform.io/hashicorp/random v3.0.1

So far, the only way I've found around this issue, is to comment out the record blocks, let the zones create, then add the record blocks back.

+1 have the same issue, if i try to create a zone (which doesn't exist) with records, even with depends_on zones in the records module the count error comes. I have to do it in a 2 step apply, first the zone, then the records

There is a code in examples/complete which works (please always check examples), and the trick is to use:

zone_name = keys(module.zones.this_route53_zone_zone_id)[0]  # <-- index may be not zero in your configuration

instead of:

zone_id = module.zones.this_route53_zone_zone_id[local.zone_int]

My understanding of this issue is that it is related to the way how Terraform treats references to objects in regards to depends_on:

  1. A complete object referenced in zone_name.
  2. Element by key referenced in zone_id.

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.