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

0 min read 130 words

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"

Step 2 – Set the terraform backend

In your main.tf, providers.tf or state.tf file, specify the following:

terraform {
  backend "s3" {
    // you don't need to specify anything here
  }
}

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
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags