I wrote a Terraform code in which the root module calls a child module that creates projects in google folders.
I want Terraform to use the proper google provider for creating projects, therefore in the root module, I defined a default google provider with an empty configuration and multiple alternate google providers, each one having in their configuration the credentials of a service account which has the necessary rights for creating projects in a specific folder. Thus each provider should be able to create projects in the folder in which its service account has the necessary rights as long as the proper folder ID is also passed to the module.
Then in the root module, I set the
providers argument within the
module block to explicitly define which google provider should be passed to the child module.
However when I run
terraform plan I get the following error message:
Error: Attempted to load application default credentials since neither
access_token was set in the provider block. No credentials loaded. To use your gcloud credentials, run ‘gcloud auth application-default login’. Original error: google: could not find default credentials. See Authenticating as a service account | Authentication | Google Cloud for more information.
Thus, it seems that since I do not specify a service account credentials in the default google provider, Terraform does not select the alternate providers when running command
terraform plan and the command fails since no credentials are selected. What should I do so that Terraform selects the alternate google providers I specified in my code when running commands
terraform apply or
terraform destroy ?
Note that in case I specify only alternate providers and no default provider, I get the following error message in addition :
Provider “registry.terraform.io/hashicorp/google” requires explicit configuration. Add a provider block to the root module and configure the provider’s required arguments as described in the provider documentation.
Thank you in advance for your help