Bernie Lenz
Bernie Lenz

Reputation: 2146

Terraform aws provider - how to use default region from ~/.aws/config

In my main.tf I have an empty aws provider defined

provider aws {}

In the absence of environment variables the aws provider picks the [default] credentials from ~/.aws/credentials. However I still get prompted to enter the region:

>terraform plan
provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.

  Enter a value: 

How can I get the aws provider to automatically pick up the corresponding region to the [default] credentials as defined in ~/.aws/config?

Upvotes: 9

Views: 6146

Answers (2)

FearlessHyena
FearlessHyena

Reputation: 4085

If the only reason that you have the provider block is to reference the region in your code then you can simply use the aws_region data source which allows you to reference the current region instead of having the provider block (the region should be picked up from the default profile in this case I believe)


data "aws_region" "current-region" {}

// Then get the region using
data.aws_region.current-region.name 

Upvotes: 2

mon
mon

Reputation: 22254

AWS provider has profile attribute but it does not pick up the region from .aws/config.

$ cat main.tf
provider aws {
     profile="default"
}

$ terraform plan
provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.
...

The way I can think of now is using the environment variable (I use this way).

$ export AWS_DEFAULT_REGION=$(aws configure get region --profile default)
$ terraform plan
Refreshing Terraform state in-memory prior to plan...
...

------------------------------------------------------------------------

No changes. Infrastructure is up-to-date.

Upvotes: 8

Related Questions