I often create classic load balancers with Terraform. The TF outputs of these projects should include the (current) IP address of the loadbalancers. To accomplish this I have the following in my code:
data "dns_a_record_set" "lb_dns_a" {
host = "${aws_elb.myelb.dns_name}"
}
output "load_balancer_ip" {
value = "${data.dns_a_record_set.lb_dns_a.addrs}"
}
This does work, except for when initially creating the ELB. More often than not, there is quite the delay between creating the ELB and its DNS name being resolvable, so that I get an error that the DNS name cannot be resolved. After a couple of seconds/minutes every terraform refresh; terraform output; shows the correct current IP address of the ELB. I guess I need to inject some sort of hold down timer to give the DNS record time to become available, when first deploying the infrastructure.
How can I implement such a hold down timer, so that the ELB is created and then TF waits for (say) 2 minutes before it creates data.dns_a_record_set.lb_dns_a?
The best solution would be to wait until the name can be resolved successfully, before proceeding.
P.S.: I only have this problem with ELB. ALB and NLB seem to be quicker, or it just takes long enough to create listeners and target groups so that their DNS names are always resolvable by the time the outputs are created.