My overall problem is that terraform wants to destroy and recreate a database because it doesn’t “know” what the region will be until after apply.
I have my main state, and it references two modules.
The main state requires a google provider of “hashicorp/google”. Then it creates a provider resource like such
provider "google" {
region = "us-west1"
project = "worm"
zone = "us-west1-a"
alias = "worm"
}
It also creates another for a project = “security” with an alias of security.
It then creates this data object
data "google_client_config" "current" {
provider = google.worm
}
It does a few other small things, like setup an aws provider and use that for a few things.
The big things it does is call the two modules.
For each of those it passes in the providers like so…
module "worm_cluster" {
source = "modules/gke_cluster"
cluster_name = "worm"
...
providers = {
google = google.worm
google.security = google.security
}
}
and
module "tp_database" {
source = "modules/gcp_database"
cluster_name = module.worm_cluster.cluster_name
...
providers = {
google = google.worm
}
The stuff in the “…” for each of those are either hardcoded values, var.* things, or locals with no dependency on anything in the modules.
Now clearly the database depends on the cluster module since it is pulling values from the cluster module for it’s inputs.
Worm_cluster has this
...
configuration_aliases = [google, google.security]
...
data "google_client_config" "current" {}
And tp_database has this
#no configuration aliases set
data "google_client_config" "current" {
provider = google
}
I am assuming the cluster module uses the “google” alias as the provider.
Anyway at various places both modules use
data.google_client_config.current.region
Specifically the tp_database module uses the above region to set the region of a “google_sql_database_instance” resource.
The worm_cluster module of course creates a “google_container_cluster” resource and also uses the region for the location.
Now someone merged in a change that set a property on the “google_container_cluster” to False. That’s it.
Terraform identifies this and says it can do an in-place update on the cluster. All good.
BUT it also wants to completely destroy and recreate the database in tp_database. And the reason is that it says it doesn’t know the region yet.
region = "us-west1" -> (known after apply) # forces replacement
That region was set with to “data.google_client_config.current.region”
There are no “depends_on” for either module if that matters.
What is confusing terraform into not being able to “know” the region in one module while it appears to know it just fine in the other?