Terraform depends on module help

Soldato
Joined
18 May 2010
Posts
22,296
Location
London
I am hoping someone can point me in the right direction.

I have one module that creates an instance per availability zone in aws in the eu-west-2 region. (using a for each loop)

I have another module that takes in the output of the ec2 module and registers the ec2 instance with route53.

My issue is if I destory the instance to start form a clean slate, when I run the configuration it gives me a message:

"
│ Error: Invalid for_each argument

│ on .terraform/modules/route53/main.tf line 9, in resource "aws_route53_record" "arecord":
│ 9: for_each = var.instances_to_register
│ ├────────────────
│ │ var.instances_to_register is a map of dynamic, known only after apply

│ The "for_each" 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 for_each depends on.

"

It works if I comment out the route53 part creates the instances then run the config again with the route53 included.

The internet saus I can use a depends_on in the module to make the route53 dependant on the ec2 module but I cant get it to work.

For example:

module "heartbeat" {
source = "/var/tmp/testing-route53/module/elkheartbeat"

target_vpc = var.target_vpc
env = var.env
tag_role = var.tag_role
tag_deployment_state = var.tag_deployment_state
deployment_subnet_name = var.deployment_subnet_name
ec2_cluster_sg_description = var.ec2_cluster_sg_description
}

module "route53" {
source = "/var/tmp/testing-route53/module/route53"
depends_on = [module.heartbeat]

route53_host_zone = "my.hosted.zone.com"
route53_ttl = 3306
route53_record_type = "A"
route53_region = "eu-west-2"
instances_to_register = module.heartbeat.heartbeat_cluster_private_dns_names_ip
}

Accordng to this article what I have tried above should work. (see the last section called "The True Module Dependencies Approach")

When I run this code I get

"
│ Error: Invalid for_each argument

│ on .terraform/modules/route53/main.tf line 9, in resource "aws_route53_record" "arecord":
│ 9: for_each = var.instances_to_register
│ ├────────────────
│ │ var.instances_to_register is a map of dynamic, known only after apply

│ The "for_each" 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 for_each depends on.

"
 
Back
Top Bottom