Terraform Field Guide · Chapter 12 of 12
Capstone: an AWS environment from scratch
Put every chapter together. Build a production-shaped AWS environment from your own modules, run it as dev and prod with separate state, change it only through reviewed plans in a pipeline, then prove it survives the bad days. This is the repository you show in an interview.
- 60 min
- Advanced
- Project
- Free
By Joshua Sequeira, founding engineer running Terraform in production. Updated .
By the end you can
0 of 5
Live lab in this chapter
A real-feeling terminal and a live cluster map, right in your browser. Every command block below has a ▶ Run in lab button.
The brief
Build a small web service on AWS entirely with Terraform: a container running on ECS Fargate behind a load balancer, a PostgreSQL database, inside its own network. Run it as two environments, dev and prod, from one set of modules. Use your own app image if you have one; if not, podinfo works well, with a health endpoint and a simple UI.
The goal is not the app, and not the number of resources. It is to show that you can describe infrastructure as code that a team could safely change every day, and explain every decision in it.
Target architecture
tree -L 2
.
├── bootstrap/ # state bucket, applied once
├── modules/
│ ├── network/ # VPC, subnets, routes, NAT
│ ├── service/ # ECS service, ALB, logs, alarms
│ └── database/ # RDS, security group, secret
├── envs/
│ ├── dev/ # backend key dev/, small inputs
│ └── prod/ # backend key prod/, larger inputs
├── policy/ # conftest rules
└── .github/workflows/ # plan on PR, apply on merge, drift| Layer | Terraform | Chapter |
|---|---|---|
| Foundation | S3 state bucket, backend with locking | 3 |
| Network | VPC, subnets per AZ, route tables, NAT gateway | 5, 6 |
| Service | ECS cluster, task definition, service, ALB, target group, log group, alarm | 6 |
| Data | RDS PostgreSQL, security group allowing only the service | 8, 11 |
| Environments | One root module per environment, inputs in tfvars | 4, 7 |
| Delivery | GitHub Actions with OIDC, plan comments, approvals | 10 |
| Guard rails | Checkov, conftest, Infracost, scheduled drift check | 9, 11 |
The picture at the top of the page is the target: one module library, one state per environment, and changes that only arrive through a reviewed plan.
Milestones
Tick them off as you go. Progress is saved in this browser.
What it costs
This builds real AWS resources, so it costs real money while it runs. Approximate us-east-1 on-demand prices for one environment, left running all month:
| Resource | Price | A month |
|---|---|---|
| Application Load Balancer | $0.0225 an hour, plus usage | about $16 |
| NAT gateway, one | $0.045 an hour, plus $0.045 per GB | about $33 |
| Public IPv4 addresses (ALB and NAT) | $0.005 an hour each | about $11 for three |
| Fargate task, 0.25 vCPU and 0.5 GB | about $0.012 an hour | about $9 |
| RDS db.t4g.micro, 20 GB | $0.016 an hour, plus storage | about $14 |
That is roughly $80 a month per environment if you leave it up, and well under a dollar for an afternoon. Build it, take your evidence (plan comments, screenshots, the drift check), then run terraform destroy in each environment. Check the AWS pricing pages for your region, and set an AWS Budgets alert at $20 before you start.
cd envs/dev && terraform destroy
cd ../prod && terraform destroy
prevent_destroy- Your database guard will stop this, as designed. For the lab, set deletion_protection = false and remove prevent_destroy in a commit, apply, then destroy. Notice how deliberate that has to be.
Day two: will it survive?
Before you call it done, be honest about what you built. Tick only what is really in your repository, then read down the list. Each of these bad days happens to real teams, and each one maps to a chapter.
Interactive · Day two
Tick what your build has. Which bad days does it survive?
Stretch goals
- Separate AWS accounts. Dev and prod in different accounts under one AWS Organization, each with its own OIDC role.
- A module release process. Modules in their own repository with semantic version tags and a changelog; environments pin versions and are bumped one at a time.
- Adopt something. Create a resource by hand in the console, then bring it under Terraform with an import block and get the plan to "No changes".
- Reviewed-plan applies. Replace the merge-time re-plan with Atlantis or HCP Terraform, so the exact plan that was reviewed is the one applied.
- A second region. Prove your modules are region-agnostic by standing up dev in another region with only input changes.
Self-review checklist
Before you call it done, answer each of these in your README:
- If two people merge at the same time, what happens, and how do I know state is still correct?
- If someone changes production in the console, how long until we know, and who decides what to do?
- What stops a pull request from deleting the database? List every layer.
- Who and what can apply to prod? Where are the credentials, and when do they expire?
- Where does the database password live, and is it anywhere in state?
- What does each environment cost per month, and which single change would cut that most?
- How would I add a third environment? How many lines change?
Mini mission
Write the one-page architecture note
Write one page a teammate could read in five minutes: what you built, a diagram, three decisions with the alternative you rejected and why, the monthly cost per environment, and the top risk you would fix next.
Reveal one safe solution
A structure that works:
- Context in two sentences: what runs, and for whom.
- Diagram: modules, environments, state, the pipeline and where credentials come from.
- Decisions: for example, folder per environment over workspaces (separate state and access); Fargate over EC2 (no servers to patch, higher price per vCPU); RDS-managed password over random_password (nothing secret in state).
- Cost: per environment, with the biggest line called out and what you would do about it.
- Risks: what is still a single point of failure, and the next fix.
Link it from your portfolio, next to the repository. A clear write-up of trade-offs says more than any list of tools; it is how the case studies on this site are written.
Check yourself
Quiz
0 of 5 answered