Defining provider aliases with string interpolation not working in Terraform 0.12

Hi @harsh-dialpad!

Provider selections cannot be dynamic like this. Although it didn’t produce an error in Terraform 0.11, it also didn’t work: Terraform 0.11 just ignored it and treated it as a literal string, just as the terraform 0.12upgrade tool showed. Terraform 0.12 has an explicit validation check for it to give you better feedback that it’s not supported.

The connections between resources and their providers happens too early for Terraform to be able to evaluate expressions in that context, because the provider must be known in order to understand the other contents of the block.

If the configuration you’re showing here is in a re-usable module that is intended to work in any region, you should configure the providers in the calling module and then pass them explicitly to the child module:

provider "google" {
  alias = "uswest"

  region = "us west"
}

provider "kubernetes" {
  alias = "uswest"

  # other region-specific settings
}

module "example" {
  source = "./modules/google-cloud-kubernetes"

  # (other module arguments, but no longer "region"
  # because that is implied by the provider configurations
  # passed below)

  providers = {
    google     = google.uswest
    kubernetes = kubernetes.uswest
  }
}

The providers map in the above example states that the unaliased provider google in the child module uses the same configuration as the google.uswest provider in the root module, and likewise for the kubernetes provider. As a result, you don’t need to use provider arguments on any of the resource blocks inside the module, because from the module’s perspective it is the unaliased provider you need to use.

This method allows instantiating the same module multiple times with different provider aliases to create infrastructure across multiple regions, while still allowing Terraform to understand exactly which provider configuration each resource belongs to without resolving any expressions.

4 Likes