Terraform remote state and errors about AWS_DEFAULT_REGION

This may be obvious to others, but it wasn't to me.

I was setting up Terraform remote state storage (to an s3 bucket) like so:

terraform remote config -backend=s3 \
           -backend-config="bucket=mig5-terraform-state" \
           -backend-config="key=terraform.tfstate"

I kept getting the error on the above:

Failed to read state: Error initializing remote driver 's3': missing 'region' configuration or AWS_DEFAULT_REGION environment variable

This worked, of course:

AWS_DEFAULT_REGION=us-west-2 terraform remote config -backend=s3 \
            -backend-config="bucket=mig5-terraform-state" \
            -backend-config="key=terraform.tfstate"

.. but it's annoying. I don't want to have to specify the region here, because it's already set as 'region' in ~/.aws/config in the [default] section. I put it in ~/.aws/credentials too, to no avail. Probably a bug.

To make matters worse, even after the above worked, commands that compare state like 'terraform plan' would still error out with the same message.

It turns out you need to specify the region as a -backend-config parameter to the 'terraform remote config' command. Like this:

terraform remote config -backend=s3 \
           -backend-config="bucket=mig5-terraform-state" \
           -backend-config="key=terraform.tfstate" \
           -backend-config="region=us-west-2"

After that, terraform commands run fine.

Naturally, only after writing this article did I notice that the Terraform doc has finally been updated to include the 'region' parameter. I am sure it wasn't there a month or so ago. Grr!

Tags: