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?

Bernie Lenz
  • 1,967
  • 23
  • 45

2 Answers2

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.
mon
  • 18,789
  • 22
  • 112
  • 205
  • 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. – Bernie Lenz Mar 01 '20 at 02:01
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 

FearlessHyena
  • 3,527
  • 33
  • 39