Skip to content

How to use a Backend-Config File for Terraform S3 State Configuration

If you are using S3 as the backend for your state management in Terraform, then you can create environment specific configuration files where you store the bucket, region among other things to be pulled in automatically when switching environments.

Step 1 – Create environment specific configs

Create a dev.conf file with the following content:

bucket = "your-bucket" region = "your-aws-region" key = "path/to/key"
Code language: plaintext (plaintext)

Step 2 – Set the terraform backend

In your, or file, specify the following:

terraform { backend "s3" { // you don't need to specify anything here } }
Code language: plaintext (plaintext)

Step 3 – Run init with backend-config

Now you can init your Terraform with the new config file.

You can easily change the dev.conf to be prod.conf or whichever environment you specify.

terraform init -backend-config=dev.conf
Code language: plaintext (plaintext)

See also  How to add Account Condition to AWS Lambda Permissions in Terraform
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x