Score:0

How to set a default account for multiple aws providers in terraform?

be flag

I need to create resources for multiple accounts in my terraform code. So, I have created multiple providers in aws and using the same for individual modules. Other than above, if I create any resource, I want to use one of the accounts as default.

I tried below code but not working.

provider "aws" {
  alias  = "silver"
  region = "us-east-2"
  profile = var.profile
}

provider "aws" {
  alias  = "gold"
  region = "us-east-2"
  profile = "gold"
}
provider "aws" {
  alias  = "plt"
  region = "us-east-2"
  profile = "plt"
}
provider "aws" {
  alias  = "infra"
  region = "us-east-2"
  profile = "infra"
}
provider "aws" {
  alias  = "sandbox"
  region = "us-east-2"
  profile = "sandbox"
  default = true
}

Even I tried below setting but getting syntax error.

providers = { 
aws.infra = "aws.infra" 
aws.sandbox = "aws.sandbox" 
aws.plt = "aws.plt"
aws.gold = "aws.gold"
default = "aws.sandbox" 
 }

I tried below code also but I am getting syntax error.

providers = { 
aws = aws.sandbox 
 }

below is the error

╷ │ Error: Unsupported argument │ │ on c1-versions.tf line 52: │
52: providers = { │ │ An argument named "providers" is not expected here.

Score:0
ph flag

The default configuration for a given provider is written as just the provider's local name without any alias part.

For example, if you want a child module to treat aws.sandbox from the root module as its default configuration for the provider named aws then you can write that like this:

providers = {
  aws = aws.sandbox
}

The above tells Terraform that the default configuration for the provider inside the child module is the same configuration as the one named "sandbox" in the calling module. Each module has its own set of provider configuration identifiers, so you can use any combination of provider configurations from the parent module to populate the provider configuration addresses required by the child module.

Meghana d avatar
be flag
Thanks for the reply but my question is on how to set the default module for the resources including the root module and the sub module when provider is not mentioned for them. Not for just sub modules, for that the first part of code is enough.
ph flag
In the root module, the default configuration for a provider is always the one that has no `alias` specified. If you want the one you've currently called "sandbox" to be the default, then you should remove `alias = "sandbox"` and then Terraform will treat it as the default configuration.
Meghana d avatar
be flag
I have tried that earlier but it is taking a random account from the list of accounts. Above it is taking gold in my case. Can you suggest what to do?
ph flag
I'm afraid I don't know any situation where Terraform would randomly choose a provider configuration, so I'm not sure what exactly has happened. It might help to edit your question to include what you've changed based on my answer, and what new output you got from Terraform after that, and then I can try to guess why it behaved that way.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.