hashicorp / terraform-google-consul

A Terraform Module for how to run Consul on Google Cloud using Terraform and Packer
Apache License 2.0
94 stars 90 forks source link

terraform apply not idempotent #50

Closed tpdownes closed 4 years ago

tpdownes commented 4 years ago

A successful terraform apply followed immediately by terraform apply should result in no actions being proposed. The use of name_prefix in 2 locations within the the consul-cluster module causes the instance template to be unnecessarily modified on the 2nd and all subsequent runs. Root issue: a timestamp is appended to the prefix and, of course, time marches on.

I believe this could be fixed by switching from name-prefix to name with a dynamically generated name that is stored in tfstate. e.g.

provider "random" {
  version = "~> 2.2.1"
}
...
resource "random_pet" "example" {}
...
resource "google_compute_instance_template" "consul_server_private" {
...
  name = "${var.cluster_name}-${random_pet.example.id}"
...
}
...
resource "google_compute_instance_template" "consul_server_public" {
...
  name = "${var.cluster_name}-${random_pet.example.id}"
...
}

There are a number of other options besides "pet names" which are of the form "wascally-wabbit". I'll put together a PR if one is welcome.

robmorgan commented 4 years ago

I've removed the template_file workaround in #55 and I believe this fixes this issue.