Hello! I ran into a problem with the way terraform 0.13 seems to handle Providers within Modules.
Specifically, the issue crops up when using a submodule with an implicitly-inherited, aliased provider. A reason this might come up is that the submodule needs two different google providers (to create resources in two google projects).
I have the following (example) root module and submodule:
# main.tf (root module, defines providers)
provider "google" {
project = "my-primary-project"
}
provider "google" {
alias = "other-project"
project = "my-other-project"
}
module "some_submodule" {
source = "./some_submodule"
}
# some_submodule/main.tf (submodule, uses both google providers)
provider "google" {
alias = "my-other-project"
}
resource "google_service_account" "some_primary_project_resource" {
provider = google
# ... other config
}
resource "google_service_account" "some_other_project_resource" {
provider = google.other-project
# ... other config
}
Note that I am not explicitly passing down the other-project
alias to the submodule. The docs say that this shouldn’t work:
Additional provider configurations (those with the alias argument set) are never inherited automatically by child modules, and so must always be passed explicitly using the providers map.
Given the documentation, I would expect this terraform config not to work at all. The submodule should fail with some kind of error like Provider not found: google.other-project
. The submodule, which is not explicitly passed the aliased configuration, shouldn’t have access to that particular provider. Therefore some_submodule
should not know to create some_other_project_resource
in the my-other-project
GCP project.
However, this config can actually be applied, and it leads to a messy state. Somehow the submodule is actually able to find the other-project
alias (and therefore knows to create some_other_project_resource
in the GCP project named my-other-project
), but in terraform state, the resource’s provider is named module.some_submodule.providers[registry.terraform.io/hashicorp/google].other-project
. So it is reusing the same provider configuration, but giving it a new name and tying it to the some_submodule
module. This leads to some confusion, because now the some_submodule
module cannot be deleted (because terraform thinks the provider definition is gone, even though it isn’t).
Is this actually a valid setup? My sense is that this configuration should cause an error to be thrown during terraform plan
, perhaps with an error message that recommends adding a providers = { ... }
argument to the some_submodule
definition.