stavxyz / terraform-mailgun-aws

A Terraform module for creating a Mailgun domain, Route53 Zone, and corresponding DNS records
https://registry.terraform.io/modules/samstav/aws/mailgun
Apache License 2.0
21 stars 2 forks source link

value of 'count' cannot be computed on first run #10

Open stavxyz opened 5 years ago

stavxyz commented 5 years ago
Error: Error refreshing state: 2 error(s) occurred:

* module.mailer.aws_route53_zone.this: aws_route53_zone.this: value of 'count' cannot be computed
* module.mailer.aws_route53_zone.this: aws_route53_zone.this: value of 'count' cannot be computed

Why does this happen? Is this expected?

stavxyz commented 5 years ago

I forgot about this...

https://github.com/hashicorp/terraform/issues/12570#issuecomment-318414280

A more fiddly way is to use the -target option to request that Terraform should create or update a particular resource in isolation first, thus making it available in state for use as count on a subsequent run. This allows count to refer to a managed resource attribute, but requires this extra -target step first. (Automating this two-step process is what #4149 is about.)

We need to include instructions in the README for circumstances where users want to plan the creation of their zone outside of the terraform-mailgun-aws module. The module already handles existing/non-existing route 53 zones fine, but terraform needs that resource to already exist so that it can be used to compute count.

The workaround for now will be to use -target on my route53 zone(s) and ensure those are created in advance during a plan/apply run.

My definitions looked like this

resource "aws_route53_zone" "example-dot-com" {
  name          = "example.com."
}

module "mailer" {
  source                = "github.com/samstav/terraform-mailgun-aws?ref=v2.0.1a"
  domain                = "example.com"
  mailgun_smtp_password = "${var.mailgun_smtp_password}"
  zone_id               = "${aws_route53_zone.example-dot-com.zone_id}"
}

The reason I ran into the error noted in the issue description is that the route53 zone did not exist when I first attempted to plan/apply this.

The count for the route53 zone is computed using the zone id which is optionally passed in. For a resource that hasn't been created yet, terraform is unable to determine count which must be computed at plan time. This is impossible since the resource doesn't exist yet.

https://github.com/samstav/terraform-mailgun-aws/blob/master/main.tf#L80

stavxyz commented 5 years ago

Can address this when a solution for https://github.com/hashicorp/terraform/issues/4149 comes to life.