Skip to Content

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 , founding engineer running Terraform in production. Updated .

modules/written oncenetworkservicealarmsdatabasedevsmall, 1 AZstatestaginglike prod, smallerstateprod3 AZs, alarms pagestatePR: planmerge:apply
The chapter in one pictureWhat you will have by the end: every environment from one set of modules, changed only through reviewed plans.

By the end you can

0 of 5

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

Repository layout
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
LayerTerraformChapter
FoundationS3 state bucket, backend with locking3
NetworkVPC, subnets per AZ, route tables, NAT gateway5, 6
ServiceECS cluster, task definition, service, ALB, target group, log group, alarm6
DataRDS PostgreSQL, security group allowing only the service8, 11
EnvironmentsOne root module per environment, inputs in tfvars4, 7
DeliveryGitHub Actions with OIDC, plan comments, approvals10
Guard railsCheckov, conftest, Infracost, scheduled drift check9, 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:

ResourcePriceA month
Application Load Balancer$0.0225 an hour, plus usageabout $16
NAT gateway, one$0.045 an hour, plus $0.045 per GBabout $33
Public IPv4 addresses (ALB and NAT)$0.005 an hour eachabout $11 for three
Fargate task, 0.25 vCPU and 0.5 GBabout $0.012 an hourabout $9
RDS db.t4g.micro, 20 GB$0.016 an hour, plus storageabout $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.

End of every session
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:

    1. Context in two sentences: what runs, and for whom.
    2. Diagram: modules, environments, state, the pipeline and where credentials come from.
    3. 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).
    4. Cost: per environment, with the biggest line called out and what you would do about it.
    5. 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

    1. 1. Why is the state bucket created by a separate bootstrap configuration?
    2. 2. Your service module adds a new required variable. What happens to existing callers?
    3. 3. The prod plan in your pipeline shows 1 to destroy on the database. What stops it, in order?
    4. 4. Which single resource in this design usually costs the most when idle?
    5. 5. How do you show an interviewer this project is production-shaped?

    Further reading