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

2

9

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?

Tributary answered 25/2, 2020 at 22:41 Comment(1)
you can assign as a variable as well.Craving
A
8

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.
Androgynous answered 26/2, 2020 at 23:9 Comment(1)
Sad though, one would think terraform would pick up the region the same way the aws cli uses it. Anyway, thanks for the great lead.Tributary
H
2

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 

Hervey answered 31/10, 2020 at 17:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.