Terraform : How to use provider alias as an argument/parameter in a module?

Viewed 44

I have aliased providers configured within my terraform modules for service account impersonnation purposes.

For the dev environment, in the root module , I have one that I dubbed as_dev_admin that enables the impersonnation of an admin account on the dev environment.

When I want to use this alias to create resources, for example, I do :


resource "google_folder" "some-folder-name" {
  provider     = google.as_dev_admin
...
}

But when I want to use it in an internal module, I do :

module "the-famous-module-instance" {
  providers = {
    google = google.as_dev_admin
  }
  source             = "../modules/the-famous-module"
}

Except I get this warning :

│ Warning: Reference to undefined provider
│
│   on main.tf line 3, in module "the-famous-module-instance":
│    3:     google = google.as_dev_admin
│
│ There is no explicit declaration for local provider name "google" in module.the-famous-module-instance, so Terraform is assuming you mean to pass a configuration for
│ "hashicorp/google".
│
│ If you also control the child module, add a required_providers entry named "google" with the source address "hashicorp/google".
╵

Is there some other syntax to use in order to do this correctly? How do I disable this warning if it's alright.

1 Answers

The terraform block must be included in the declared module sourced at ../modules/the-famous-module. The most important nested block there for your issue would be the required_providers. This can be found in the documentation.

terraform {
  required_providers {
    google = {
      ...
    }
  }
}

also I thought the terraform block is inherited from root module, but apparently not

It can be partially passed to declared modules with a providers block, but otherwise no inheritance. You already are using this to pass an alias, and it can also pass provider configuration argument values, but not retrieval meta-argument values e.g. version, source, etc.

Related