Note: Some resource attributes intentionally omitted for brevity.
Problem:
I have a google_compute_instance
that triggers a re-creation if the boot_disk.image
or metadata_startup_script
change. I manage this via random_id.keepers
like so:
resource "random_id" "suffix" {
byte_length = 2
keepers = {
image = data.google_compute_image.image.self_link
startup_script = data.template_file.startup_script.rendered
}
}
resource "google_compute_instance" "instance" {
metadata_startup_script = random_id.suffix.keepers.startup_script
boot_disk {
initialize_params {
image = random_id.suffix.keepers.image
}
}
lifecycle {
create_before_destroy = true
}
}
I use create_before_destroy
so that the service this instance runs is highly available. The issue I’m having is that clients of this service connect via DNS.
resource "google_dns_record_set" "dns" {
type = "A"
ttl = 15
rrdatas = [google_compute_instance.instance.network_interface.0.network_ip]
}
Running the terraform apply
results in the following order of operations:
random_id.suffix: Creating...
random_id.suffix: Creation complete after 0s
google_compute_instance.instance: Creating...
google_compute_instance.instance: Still creating...
google_compute_instance.instance: Creation complete after 13s
google_compute_instance.instance: Destroying...
google_compute_instance.instance: Still destroying...
google_dns_record_set.dns: Modifying...
google_dns_record_set.dns: Modifications complete after 2s
google_compute_instance.instance: Still destroying...
Once the replacement instance finishes creating it starts the destroy and the DNS modification at the same time.
I’m looking for a way to update the DNS and wait for the TTL to expire before destroying the old instance. Is this possible?