Infrastructure as Code for Kaapi, written in Terraform. This repo is the single source of truth for what Kaapi runs on AWS: networking (VPC, subnets, route tables, Internet Gateway), compute (ECS), container images (ECR), configuration and secrets (Secrets Manager), monitoring (CloudWatch alarms, SNS alerts) and, as modules are filled in, RDS, ALB, KMS, SSM, EventBridge and IAM.
Each environment (staging, prod, and any added later) is built from the same shared modules but has its own values, its own AWS account guard and its own remote state in S3. A change applied to one environment can never affect another.
Make infra changes here and apply them through GitHub Actions. Do not edit Terraform-managed resources by hand in the AWS console: Terraform will undo or conflict with manual changes on the next apply.
kaapi-infra/
├── .github/
│ ├── actions/terraform-plan/ # Shared init + plan step (used by PR plans and deploys)
│ │ └── redact.sh # Hides AWS IDs in plan/apply output
│ ├── workflows/ # pr-checks, pr-plan, deploy-staging, deploy-prod, plan-and-apply
│ └── CODEOWNERS
├── bootstrap/ # One-time setup per AWS account: the Terraform state bucket
├── environments/
│ ├── staging/ # Wires the modules together with staging's values
│ └── prod/
├── modules/ # Reusable building blocks shared by all environments
│ ├── vpc/ # VPC (root) + submodules:
│ │ ├── subnet/ # subnets of one tier (public or private)
│ │ ├── route-table/ # a route table, its routes and subnet associations
│ │ └── internet-gateway/ # the Internet Gateway
│ ├── ecs-cluster/ ecs-task-definition/ ecs-service/
│ ├── ecr/ cloudwatch/ sns/
│ └── rds/ alb/ iam/ kms/ ssm/ secrets-manager/ eventbridge/ elastic-ip/ ec2/ # placeholders
├── docs/ # Diagrams
├── Makefile # Local init / plan helpers
├── .env.example # Local AWS profile, region and state bucket (copy to .env.<env>)
└── .pre-commit-config.yaml
Each environment folder contains:
| File | Purpose |
|---|---|
main.tf |
Which modules the environment uses and how they are wired together |
variables.tf |
Every input, with type, default and validation |
constants.tfvars |
Committed, non-sensitive values (names, sizes, CIDRs, ECS config) |
terraform.tfvars.example |
Template for the local, never-committed terraform.tfvars |
backend.tf, providers.tf, locals.tf, outputs.tf, versions.tf |
State backend, AWS provider and default tags, naming, outputs, versions |
Resource names follow <project>-<environment>-<name>, e.g. kaapi-staging-vpc-main, kaapi-staging-cluster. Every resource is tagged Product, Environment and ManagedBy = terraform.
.pre-commit.yaml runs these checks on every git commit:
| Hook | What it does |
|---|---|
gitleaks |
Blocks the commit if it finds secrets (keys, tokens, passwords) |
terraform_fmt |
Formats changed .tf / .tfvars files (terraform fmt) |
terraform_validate |
Runs terraform validate in folders with changed Terraform files |
One-time setup:
brew install pre-commit # or: pip install pre-commit, uv tool install pre-commit
pre-commit install --config pre-commit.yaml # adds the git hook to this cloneRun all hooks on every file (e.g. after updating the config):
pre-commit run --all-files --config pre-commit.yamlIf terraform_fmt reformats a file, the commit stops: review the change, git add it, and commit again. pr-checks runs terraform fmt -check in CI too, so unformatted code fails there as well.
Changes reach AWS only through GitHub Actions, and only after approval.
| Step | Workflow | What happens | Who acts |
|---|---|---|---|
| Open or update a PR | pr-checks |
terraform fmt -check, no REPLACE_ME left, terraform validate (no AWS access) |
Author |
| Open or update a PR that touches Terraform | pr-plan |
After approval of staging-pr-plan, plans staging with a read-only role and comments the list of changed resources on the PR | Staging approvers |
Merge to main |
deploy-staging |
Waits at staging-plan | — |
| Approve staging-plan | plan-and-apply |
Creates the plan; the full plan is in the Plan step log | Staging approvers |
| Approve staging-apply | plan-and-apply |
Applies that exact saved plan | Staging approvers, after reading the plan |
Push a version tag (v1.2.3) on a main commit |
deploy-prod |
Same plan → review → apply flow with prod-plan and prod-apply | Release manager, prod approvers |
- PR plans never apply anything and skip PRs from forks.
- Several merges while staging waits for plan approval: each new run cancels the waiting one, so a single approval plans and applies everything merged so far.
- An apply is never cancelled midway. If state changed after a plan was made, Terraform refuses that plan; re-run the workflow to plan again.
- If a plan has no changes, the apply job is skipped.
- Only tags like
v1.2.3deploy prod, and only when the tag points to a commit onmain. Tags such asv1.2.3-rc1are ignored. - Staging can also be re-run from Actions → deploy-staging → Run workflow.
pr-checkscurrently validatesstagingonly;prodwill be added back once its environment is set up.
The repository is public, so its Actions logs are too.
- No plan output in public summaries: the job summary and artifacts never contain plan output. The PR comment lists only resource addresses and actions.
- IDs are redacted: plan and apply logs go through
.github/actions/terraform-plan/redact.sh, which hides AWS resource IDs, account IDs, UUIDs and email hashes. - Sensitive values stay hidden: sensitive Terraform values show as
(sensitive value), and GitHub masks secrets as***. - App secret values never touch Terraform: they stay in Secrets Manager, and Terraform only references their ARNs.
- AWS roles: the PR plan role is read-only and scoped to what
terraform planneeds. The deploy role can only be assumed by thestaging-plan/staging-apply(and prod) environments. - No secrets in commits:
gitleakschecks every commit locally.
