Skip to content
EgyKode
04 · Infrastructure as CodeLab 22 / 59
Guided labterraform

Terraform Remote State & Locking

Move state off your laptop into an encrypted, versioned, locked backend — and prove the lock works by breaking it deliberately.

Time
45 min
Level
Intermediate
Objectives
4 objectives
Cost
Low cost

Where this fits in the platform

This lab adds

  • State in S3 with locking — safe for more than one person

Before you start

You will need

  • Terraform >= 1.6
  • AWS CLI v2, configured

You do not need these already — the lab environment below provides them.

You will be able to

  • Migrate local state to an S3 backend without recreating resources
  • Prove a concurrent apply is blocked rather than corrupting state
  • Recover from a stale lock safely

CostLow cost

— an S3 bucket and a PAY_PER_REQUEST DynamoDB table. A few applies a week costs effectively nothing.

Nothing to pay in the browser. Open the terminal runs this against a simulated cloud — the same API calls and the same commands, with no account and no bill. The figure above applies only if you build it in your own.

How to clean up

The scenario#

State is on your laptop. A colleague runs terraform apply from theirs, sees none of your resources, and creates a second copy of everything — or worse, destroys yours.

This is the lab that makes Terraform usable by more than one person.

Hands-on environment

Run this lab in a real terminal, free and in your browser. The environment is temporary and yours alone — break it as much as you like.

Open the terminal

Opens in Killercoda, in a new tab — keep this page open for the steps.

Run it on AWS

This lab builds real cloud infrastructure, so it needs your own AWS account. Follow the cost and cleanup notes above — the resources are yours, and so is the bill.

Anything you tick here is your own record. EgyKode cannot see inside that terminal, so the success criteria stay self-assessed even when the environment checks your work for you.

The backend has a chicken-and-egg problem

Step 1 of 4

Clean up#

Run this even if you did not finish.

DestructiveThis removes real resources. Check which environment you are in first.

Terminal
terraform destroy -auto-approve
# Empty the state bucket before deleting it (versioning keeps old objects):
aws s3 rm s3://<state-bucket> --recursive   # current objects
# Versioning keeps old objects; remove every version before deleting the bucket.
# In the console: Empty bucket, which handles versions and delete markers.
aws s3 rb s3://<state-bucket> --force
aws dynamodb delete-table --table-name <lock-table>

Cost of this lab: Free tier — an S3 bucket and a PAY_PER_REQUEST DynamoDB table. A few applies a week costs effectively nothing.

Success criteria

0 of 4

The concept behind it

Ready to try it without help?Do the challenge

Next up

Lab 22 of 59 on the project path

AWS VPC, Subnets, Gateways & Route TablesRebuild the network you made by hand as Terraform modules, and see the plan account for every subnet and route.Why next: State in S3 with locking — safe for more than one person47 minIntermediateBillable — destroy resources when you finish

Previous: Terraform Modules