🎓 A1Learner
🔍
0 of 50 lessons completed

Remote State with an S3 Backend

A remote state backend stores the state file in shared storage instead of on one laptop. An S3 bucket is the usual choice for AWS work.

Bootstrap the bucket first

This small configuration creates the bucket that will hold state.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
  s3_use_path_style = true

  endpoints {
    s3 = "http://localhost:5000"
  }

  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true
}

resource "aws_s3_bucket" "state" {
  bucket = "terraform-state-demo"
}

Point the backend at the bucket

The backend block lives inside the terraform block.

terraform {
  backend "s3" {
    bucket = "terraform-state-demo"
    key    = "demo/terraform.tfstate"
    region = "us-east-1"
    skip_credentials_validation = true
    skip_metadata_api_check     = true
    skip_requesting_account_id  = true
    use_path_style = true

    endpoints = {
      s3 = "http://localhost:5000"
    }
  }
}

Move the state into the bucket

The flag answers yes to the migration question.

terraform init -migrate-state

Example

# Step 1: bootstrap the bucket (in a folder named bootstrap).
# Run terraform init and terraform apply -auto-approve there.
# It uses the configuration from the first snippet above.

# Step 2: main configuration (in a folder named demo).
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
  }

  backend "s3" {
    bucket = "terraform-state-demo"
    key    = "demo/terraform.tfstate"
    region = "us-east-1"
    skip_credentials_validation = true
    skip_metadata_api_check     = true
    skip_requesting_account_id  = true
    use_path_style = true

    endpoints = {
      s3 = "http://localhost:5000"
    }
  }
}

provider "aws" {
  region = "us-east-1"
  s3_use_path_style = true

  endpoints {
    s3 = "http://localhost:5000"
  }

  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true
}

resource "aws_s3_bucket" "demo" {
  bucket = "demo-remote-state"
}

# commands (with dummy credentials for the emulator)
export AWS_ACCESS_KEY_ID="test"
export AWS_SECRET_ACCESS_KEY="test"
terraform init -migrate-state
terraform apply -auto-approve
terraform destroy -auto-approve
# Then remove the bootstrap bucket too, from the bootstrap folder.

Key points

  • A backend block names the bucket, the key, and the region. The key is the path inside the bucket.
  • The backend cannot create its own bucket. That is the chicken-and-egg problem.
  • Bootstrap the bucket first with a separate small configuration. Then point the backend at it.
  • terraform init -migrate-state moves local state into the bucket. The local copy is left behind.
  • Every team member then reads and writes the same state. One copy stays current.

The bucket existed before the backend was configured, so the migration had somewhere to go. State now lives in the bucket, not on the laptop. The destroy at the end removes the demo bucket, and the bootstrap bucket is removed separately.

Note

the backend bucket here is a mock one on this machine, not a real S3 bucket.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

Why must the state bucket exist before the backend is configured?