Terraform Tutorial — Backends Practice Exercises
Practice configuring and migrating Terraform backends.
Exercise 1: S3 Backend
Task: Write a complete backend "s3" block that stores state in bucket acme-tf-state, key prod/web-app/terraform.tfstate, region us-east-1, with encryption and DynamoDB locking using table tf-locks.
Answer
terraform {
backend "s3" {
bucket = "acme-tf-state"
key = "prod/web-app/terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "tf-locks"
}
}
Exercise 2: Migration
Task: You have state stored locally and want to move it to S3. What are the exact steps?
Answer
- Add the
backend "s3"block to your configuration - Run
terraform init -migrate-state - Confirm the migration when prompted (
yes) - Verify with
terraform state list - Delete the local
terraform.tfstatefile (it’s now in S3)
Exercise 3: .gitignore
Task: List five file patterns that should always be in a Terraform project’s .gitignore.
Answer
*.tfstate— state files contain secrets*.tfstate.*— state backup files.terraform/— provider plugins (large, regenerated)*.tfvars— may contain sensitive variable valuescrash.log— Terraform crash logs with sensitive info
Exercise 4: Backend Comparison
Task: Compare S3 backend with Terraform Cloud. When would you choose each?
Answer
Choose S3 backend when:
- Your team is AWS-only
- You want simple setup with existing AWS infrastructure
- You manage access through IAM policies
Choose Terraform Cloud when:
- You need a UI for state management and history
- You want built-in policy enforcement (Sentinel)
- You need remote execution (runs happen in the cloud, not on developer machines)
- Your team works across multiple cloud providers
Chapter Summary
In this chapter, you learned:
- Local backend: simple but single-user, no encryption, no locking
- S3 backend: team-friendly with encryption, versioning, and DynamoDB locking
- DynamoDB: provides state locking to prevent concurrent modifications
- Terraform Cloud: managed backend with UI, policies, remote runs
- Migration:
terraform init -migrate-statecopies state between backends - .gitignore: always exclude
*.tfstate,.terraform/,*.tfvars,crash.log
Next up: Troubleshooting — debugging common Terraform errors.