In Chapter 11 we briefly looked at remote state. Now we’re going to dive deeper, because it’s one of the pillars of teamwork with Terraform. In this subchapter we’ll configure the most widely used backend in the AWS world: S3 + DynamoDB. S3 stores the state, and DynamoDB handles the “locking” so multiple people don’t step on each other’s toes. Let’s see it step by step.

Recap: why you need a remote backend

Remember from subchapter 11.3 the problem with local state: if the terraform.tfstate file is on your laptop, your team can’t collaborate (everyone would have their own copy), and if you lose the file, you lose control of your infrastructure. The solution is to store the state in a central and shared place: a remote backend.

Local state (bad for teams):         Remote state (good):
  tfstate on your laptop               tfstate in S3 (central)
  - only you have it                   - the whole team shares it
  - it can be lost                     - safe, with copies and versions

The S3 + DynamoDB backend: the two components

The standard backend in AWS combines two services you already know, each with a role:

┌─────────────── Remote backend ───────────────┐
│  S3        → stores the state file           │
│  DynamoDB  → manages the "lock"              │
└──────────────────────────────────────────────┘
  • S3 (Chapter 5): stores the tfstate file. It’s durable, versioned, and encrypted.
  • DynamoDB (subchapter 8.3): manages locking to prevent two people from modifying the state at the same time (we’ll see this in detail in subchapter 20.2).

Step 1: Create the S3 bucket and DynamoDB table

These two resources are the “foundations” that must exist before configuring the backend. They’re usually created just once:

# S3 bucket to store the state
resource "aws_s3_bucket" "estado" {
  bucket = "mi-empresa-terraform-estado"
}

# Enable versioning: saves the state history (highly recommended!)
resource "aws_s3_bucket_versioning" "estado" {
  bucket = aws_s3_bucket.estado.id
  versioning_configuration {
    status = "Enabled"
  }
}

# DynamoDB table for locking
resource "aws_dynamodb_table" "locks" {
  name         = "terraform-locks"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "LockID"

  attribute {
    name = "LockID"
    type = "S"
  }
}

Note two important details:

  • The bucket’s versioning (see subchapter 5.3) saves the history of the state. If something goes wrong, you can roll back to a previous version. It’s an invaluable safety net.
  • The DynamoDB table uses a LockID key: that’s where Terraform writes the “lock” when someone is working.

Step 2: Configure the backend in your project

Once the bucket and table exist, configure your project to use that backend. This is done in the terraform block:

terraform {
  backend "s3" {
    bucket         = "mi-empresa-terraform-estado"   # the S3 bucket
    key            = "produccion/terraform.tfstate"  # state file path
    region         = "eu-west-1"
    dynamodb_table = "terraform-locks"               # locking table
    encrypt        = true                            # encrypt the state
  }
}

Let’s review each line:

  • bucket: the S3 bucket where the state is stored.
  • key: the path of the file inside the bucket. Note produccion/...: using a different path per environment is key for the directory strategy (subchapter 19.2). Each environment has its own separate state.
  • dynamodb_table: the table that manages locks.
  • encrypt = true: encrypts the state at rest. Important, because the state may contain sensitive data (subchapter 11.2).

Step 3: Initialize

After configuring the backend, run terraform init (subchapter 11.4). Terraform detects the backend and, if you already had local state, asks if you want to migrate it to remote:

terraform init
  → Terraform detects the S3 backend
  → "Migrate existing state to S3?" → yes
  → from now on, the state lives in S3

Done! Your state is now centralized, versioned, encrypted, and locked. Your team can collaborate safely.

A detail: the “chicken and egg” problem

You might wonder: if Terraform creates the bucket and table... where is the state for those resources stored before the backend exists? It’s a small circular dilemma. The usual solution:

  1. Create the bucket and table with local state (no backend yet).
  2. Once they exist, add the backend configuration and run init to migrate the state to S3.

Another option is to create these “base” resources manually once. In any case, it’s an initial step that’s only done once per organization.

What you should remember

  • Local state doesn’t work for teams (it’s not shared and can be lost); you need a central, shared remote backend.
  • The standard backend in AWS is S3 + DynamoDB: S3 stores the state file (durable, versioned, encrypted) and DynamoDB manages the lock so no one steps on each other’s toes.
  • Configuration: create the S3 bucket (with versioning as a safety net) and the DynamoDB table (LockID key); then declare the backend "s3" with bucket, key (per-environment path), dynamodb_table, and encrypt = true.
  • Use a different key per environment (produccion/..., dev/...) to separate states (strategy from subchapter 19.2).
  • After configuring, terraform init migrates the state to the backend. The “base” resources (bucket and table) are created only once (with an initial step for the “chicken and egg” problem).

In the next subchapter we’ll dive deeper into the piece that makes teamwork safe: state locking and how it prevents state corruption.

Cloud, AWS & Terraform — From Zero to Expert

Chapter 1 · What is cloud computing

Chapter 2 · The cloud market and major providers

Chapter 3 · Regions, availability zones and edge

Chapter 4 · Compute: EC2

Chapter 5 · Storage: S3

Chapter 6 · Networking: VPC

Chapter 7 · Identity and access: IAM

Chapter 8 · Managed databases

Chapter 9 · Why Infrastructure as Code

Chapter 10 · HCL: the Terraform language

Chapter 11 · Providers and state

Chapter 12 · Your first real infrastructure in Terraform

Chapter 13 · Load balancing and auto scaling

Chapter 14 · Serverless with Lambda

Chapter 15 · Messaging and events

Chapter 16 · Content delivery and DNS

Chapter 17 · Containers on AWS

Chapter 18 · Modules: reuse and composition

Chapter 19 · Workspaces and environment management

Chapter 20 · Remote backends and locking

Chapter 21 · Infrastructure testing

Chapter 22 · Terraform in CI/CD

Chapter 23 · Defense in depth

Chapter 24 · Observability: logs, metrics and traces

Chapter 25 · Cost optimization

Chapter 26 · High availability and disaster recovery

Chapter 27 · AWS Well-Architected Framework

Chapter 28 · Serverless architectures at scale

Chapter 29 · Data platforms on AWS

Chapter 30 · Multi-account and landing zones

Chapter 31 · Platform Engineering and Internal Developer Platform

Chapter 32 · Relevant AWS certifications

Chapter 33 · Projects to consolidate what you've learned

Chapter 34 · Resources and community

© Copyright 2024. All rights reserved