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

  • Home /
  • Blog Posts /
  • 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"

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