Members course · AWS · Intermediate

Terraform on AWS: Nonprod and Production Environments

Build a production-grade AWS platform with Terraform, one module at a time: an S3 remote state backend with native locking, multi-account providers with default tags, a VPC with one NAT in nonprod and three in production, an ALB with HTTPS, IAM least-privilege roles, a hardened private S3 bucket module, RDS PostgreSQL with Secrets Manager passwords and Multi-AZ in prod, ECR with immutable tags, ECS Fargate with a circuit breaker and autoscaling, EC2 workers with SSM access and a scale-to-zero schedule, CloudFront with OAC, ACM, Route 53 and WAF, CloudWatch alarms, AWS Budgets, and a GitHub Actions pipeline with OIDC authentication and a production approval gate. Every lesson uses real terraform plan output from the AWS provider.

Start the course

1. Foundations: Layout, State, Providers and Organization

Build the base every AWS Terraform project needs: a module-based directory layout, file-naming conventions with IAM policies and inventories, remote state on S3 with native locking, and multi-account providers with environment-scoped default tags.

  1. Project Layout: modules/, envs/ and tfvars — Organise a Terraform repository so the same module code is shared across nonprod and production without copying it.
  2. Code Organization: Files, IAM Policies and Inventories — Structure Terraform files for a small team: standard file names per root, IAM policies as data sources in dedicated files, and resource inventories with locals and for_each.
  3. Remote State: S3 Backend with Native Locking — Store Terraform state in S3 with versioning and server-side encryption, and use Terraform 1.17 native S3 locking to avoid the DynamoDB table.
  4. Multi-account Providers: assume_role and default_tags — Configure separate AWS provider blocks for nonprod and prod accounts, use assume_role for credential isolation, and apply environment-scoped default tags to every resource.

2. Network: VPC, Subnets, ALB and Security Groups

Build a reusable VPC module with three subnet tiers and NAT gateways, then front it with an Application Load Balancer that terminates HTTPS.

  1. VPC, Subnets and NAT Gateway — Write a VPC module with public, private and database subnet tiers across three availability zones, using one NAT gateway in nonprod and one per AZ in production.
  2. ALB, HTTPS Listeners and Security Groups — Front your VPC with an Application Load Balancer that terminates HTTPS, redirects HTTP to HTTPS, and forwards to a target group, backed by security groups that only allow what's needed.

3. Identity and Data: IAM, S3 and RDS

Secure every component with least-privilege IAM roles, store static assets in a hardened S3 bucket module, and provision a PostgreSQL database that is Multi-AZ in production but not in nonprod.

  1. IAM Roles: Execution Role, Task Role and Least Privilege — Write separate IAM roles for the ECS execution role (what ECS needs to start the task) and the task role (what your application code is allowed to do), using aws_iam_policy_document for type-safe, reviewable policies.
  2. S3 Private Bucket Module — Build a reusable module for a hardened private S3 bucket: versioning, server-side encryption, public access block, TLS-only bucket policy and lifecycle rules.
  3. RDS PostgreSQL: Secrets Manager, Multi-AZ and Environment Differences — Provision a PostgreSQL RDS instance with a password managed by Secrets Manager, single-AZ in nonprod and Multi-AZ with deletion protection in production.

4. Containers and Compute: ECR, ECS Fargate and EC2

Build a container registry with lifecycle policies, an ECS Fargate service with a deployment circuit breaker and autoscaling, and an EC2 Auto Scaling group accessed through SSM with a nonprod night-time schedule.

  1. ECR Repositories: Immutable Tags, Scan on Push and Lifecycle Policies — Create ECR repositories with immutable image tags and lifecycle policies that keep the last N images and clean up untagged layers, so your registry stays tidy without manual cleanup.
  2. ECS Fargate Service: Task Definition, Autoscaling and Circuit Breaker — Define an ECS Fargate service with a task definition, ALB target group registration, a deployment circuit breaker to auto-roll back on health check failures, and Application Auto Scaling.
  3. EC2 Workers: Launch Template, Auto Scaling Group and SSM Access — Provision a background worker fleet on EC2 using a launch template with IMDSv2 and an SSM instance profile instead of SSH, and add a nonprod schedule that scales to zero at night.

5. Edge: CloudFront, Route 53, ACM and WAF

Serve a static frontend from S3 through CloudFront with an Origin Access Control, provision an ACM certificate with DNS validation, map it to a Route 53 record, and attach a WAF Web ACL.

  1. CloudFront Static Site: S3 Origin with OAC and Cache Policies — Serve a React or static site from a private S3 bucket through CloudFront using an Origin Access Control so the bucket is never public, with a cache policy that handles SPA client-side routing.
  2. Route 53, ACM and WAF: DNS Validation and Edge Protection — Provision an ACM certificate with Route 53 DNS validation, map a domain alias record to your ALB and CloudFront distribution, and attach a WAF Web ACL with managed rule groups.

6. Operations: Monitoring, Cost, CI/CD and Go-Live

Set up CloudWatch alarms and log retention, create cost budgets and nonprod schedules, build a GitHub Actions pipeline that plans on pull requests and applies with approval, and walk through a go-live checklist.

  1. CloudWatch: Log Groups, Alarms and SNS Notifications — Create CloudWatch log groups with retention policies, metric alarms for ECS service health and RDS, and an SNS topic that triggers an email or webhook when an alarm fires.
  2. Cost: Budgets, Cost Tags and Nonprod Schedules — Set up AWS Budgets with SNS alerts, enforce cost allocation tags, and schedule nonprod resources to stop outside business hours using EventBridge Scheduler.
  3. GitHub Actions Pipeline: OIDC, Plan on PR and Apply with Approval — Build a GitHub Actions workflow that authenticates to AWS using OIDC (no stored keys), runs terraform plan on every pull request, and applies to production only after a manual approval step.
  4. Capstone: Promoting nonprod to prod and the Go-Live Checklist — Walk through the final steps of a real launch: verifying nonprod thoroughly, pinning module versions before promoting to prod, running drift detection, and confirming the go-live checklist.

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake