Skip to content

About

Terraform modules for a small AWS service (VPC, Fargate, RDS, S3, scoped IAM), tested with mock providers.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

12 Commits

Folders and files

Repository files navigation

stack-forge

Terraform for a small AWS service: a VPC, an ECS Fargate service behind an application load balancer, a Postgres database on RDS, an S3 bucket, and IAM roles that name the resources they touch. Four modules, two environments (dev and prod), and tests that check the safety properties of each.

modules/network    VPC, public and private subnets, NAT, route tables
modules/storage    private, versioned, encrypted S3 bucket with a TLS-only policy
modules/database   RDS Postgres in private subnets, password kept in Secrets Manager
modules/service    ECS cluster and Fargate service, ALB, both IAM roles
environments/dev   one NAT, one task, single-AZ database, plain HTTP
environments/prod  NAT per zone, two tasks, multi-AZ database, HTTPS only
scripts/           check_plan.py, a plan checker for drift and forbidden changes

What actually ran

Everything here was run with Terraform 1.16.4 and TFLint 0.64.0 on a laptop. There is no AWS account behind it.

  • terraform fmt, terraform validate and tflint pass on all six directories.
  • terraform test passes 39 runs (M): 6 network, 5 storage, 8 database, 11 service, 4 dev, 5 prod. About 18 s for all of them (M). Every test uses mock_provider "aws", so no call leaves the machine and no resource exists at any point.
  • scripts/tests has 16 unit tests (M) for the plan checker, built from hand-written plan JSON.
  • Never applied to real AWS. I have not seen terraform apply succeed on this code. Mock providers do not know AWS's own rules, so things like an unsupported RDS engine version, an instance class that is not offered in a zone, or an IAM policy AWS would reject are not caught here.
  • The GitHub Actions workflow in .github/workflows/ci.yml is written and runs the same make targets, but has not run on GitHub yet. It pins Terraform 1.12.2 and TFLint 0.54.0, which are older than the local versions.

Run it:

make all          # fmt, validate, tflint, terraform test, plan checker tests
make test         # just terraform test, in every module and environment

terraform init downloads the AWS provider, so the first run needs internet.

What the tests pin down

  • S3: all four public access switches on, versioning and default encryption on, a bucket policy that denies requests without TLS, force_destroy off by default, bad bucket names rejected.
  • RDS: publicly_accessible = false, storage encrypted, password handled by RDS in Secrets Manager (no password argument is ever set), ingress only from the app security group on 5432, backups cannot be set to zero.
  • Prod rules: a prod database that is not multi-AZ, or that skips the final snapshot, fails the plan through a precondition. So does a prod service with one task, or without an HTTPS certificate. Prod also refuses an image tagged latest.
  • IAM: no statement has a wildcard action. The only Resource: "*" is the one ecr:GetAuthorizationToken statement, which AWS does not let you scope. The task role reaches one bucket and cannot delete objects. The execution role reads exactly one secret.
  • Network: subnet ranges do not collide, the first private subnet keeps its CIDR (moving it would replace the subnet), one NAT in dev and one per zone in prod.

I checked that the IAM and NAT tests can fail by changing the code under them and watching them go red: s3:* in the task policy, Resource = "*" on the secret statement, and a single NAT in prod. Each turned the matching test red.

Tests of IAM policies use command = apply against the mock provider rather than plan. The policies embed ARNs, which are unknown at plan time, and Terraform cannot decode a JSON string that contains an unknown value.

The plan checker

terraform -chdir=environments/prod plan -out=plan.tfplan
terraform -chdir=environments/prod show -json plan.tfplan > plan.json
python3 scripts/check_plan.py plan.json --env prod

Exit 0 means nothing to report, 1 means drift (or a pending change when --expect-no-changes is set, for a scheduled drift job), 2 means a forbidden change. Forbidden is: deleting or replacing a database, bucket, VPC, secret or KMS key; any delete at all for --env prod; a database that turns public or unencrypted; a prod database losing multi-AZ or deletion protection; a public access switch turned off; an IAM policy granting a wildcard action.

It reads the JSON that Terraform writes and uses only the standard library. I have only run it against plan JSON I wrote by hand for the tests, never against a real plan, so the field layout is taken from the Terraform docs and not from observation.

Using the environments

cd environments/dev
cp backend.hcl.example backend.hcl          # point it at your own state bucket
terraform init -backend-config=backend.hcl
terraform plan -var image=<ecr-uri:tag> -var ecr_repository_arn=<arn> -var bucket_suffix=<account-id>

Prod also takes -var certificate_arn=<acm-arn>. The provider lock files are committed for linux_amd64 and darwin_arm64.

Decisions

  • Outputs expose what tests need. A test in an environment can see a module's outputs and nothing else. The first version of the environment tests reached into module.database.aws_db_instance, which Terraform refuses, so the modules now export settings, desired_count and similar. The fix is in the history.
  • RDS manages its own password. manage_master_user_password keeps the secret out of config and state. The cost is that rotation and the secret's KMS key are RDS defaults, not mine.
  • Policies are jsonencode, not aws_iam_policy_document, because the mock provider returns a placeholder for the data source's json, which makes the policy impossible to assert on. The trust policy still uses the data source.
  • Two environment folders, not workspaces. More repeated lines, but a plan in prod/ cannot be pointed at dev state by a forgotten workspace select.

Limits

  • Not applied, as above. Treat the IAM scoping and the security group rules as reviewed by tests, not by AWS.
  • The database engine version (16.4) and instance classes are fixed in code. If RDS has retired 16.4 by the time you read this, apply will say so.
  • Tasks reach AWS APIs through the NAT gateway. VPC endpoints for ECR, S3, Secrets Manager and CloudWatch Logs would be cheaper and keep that traffic off the internet. Not built.
  • No WAF, no flow logs, no CloudTrail, no KMS customer keys, no alarms. The ALB has access logs off.
  • The ALB health check expects GET /health to return 200. There is no application in this repo and no ECR repository: the image is an input.
  • TFLint runs only the bundled Terraform ruleset. The AWS ruleset needs a download from GitHub, and I left it out so CI stays offline after init.
  • Remote state is a partial backend "s3" block. The state bucket and its lock are yours to create.

About

Terraform modules for a small AWS service (VPC, Fargate, RDS, S3, scoped IAM), tested with mock providers.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages