Phase 2: Terraform & Policy-as-Code
๐ง Beginner Mental Model (Explain Like Iโm 5)
Clicking buttons in the AWS Console (ClickOps) is like building a house without blueprintsโif it burns down, nobody remembers how to rebuild it.
- Terraform & Terragrunt (IaC): Machine-readable blueprints stored in Git that build, update, and clone entire cloud data centers automatically.
- Policy-as-Code (OPA & Checkov): An automated building inspector robot that scans your Terraform blueprints in CI and blocks insecure changes before a single cloud resource is created.
- HashiCorp Vault: An automated bank vault that mints temporary 1-hour database passwords on demand instead of storing static passwords in files.
๐งญ Step-by-Step Execution Playbook for Phase 2
- Step 1 (State & Locking): Configure S3 + DynamoDB state locking in Lesson 1: Terraform State & Modules.
- Step 2 (Policy Gates): Write Rego & Checkov CI gates in Lesson 2: Policy-as-Code.
- Step 3 (Fleet Scaling & Secrets): Scale DRY configs across 50 accounts with Lesson 3: Terragrunt and dynamic credentials with Lesson 4: Vault.
- Step 4 (Refactoring & Automation): Master zero-downtime
movedblocks (Lesson 5), HCL labs (Lesson 6), and Python automation (Lesson 7).
๐ Deep-Dive Index
1. Terraform Core & State Management
- State Architecture: Remote state (S3), State locking (DynamoDB) to prevent concurrent runs.
- State Manipulation:
terraform state rm(removing without deleting),terraform import(bringing manual resources into IaC),terraform taint/apply -replace. - HCL Mastery:
for_eachvscount,dynamicblocks,locals,datasources,depends_on, lifecycle rules (create_before_destroy).
2. Enterprise Module Design
- Root vs Child Modules: Composition over inheritance. Passing variables, outputting IDs.
- Multi-Account Deployments: Configuring multiple
providerblocks withalias. Usingassume_roleto authenticate to child accounts from a central management CI/CD pipeline.
3. Policy-as-Code (Shift-Left) & Cloud Automation (Shift-Right)
- Checkov / tfsec: Static analysis of Terraform code.
- OPA (Open Policy Agent) & Rego: Declarative assertion rules evaluated against
tfplan.json(e.g., โAll S3 buckets must have server-side encryptionโ). - Python for Platform Engineers: Building automated OPA CI evaluation harnesses, handling deep nested JSON, and structuring Pull Request policy failure reports.
- AWS Lambda Serverless Governance: Event-driven auto-remediation (CloudTrail + EventBridge -> Python 3.12 Lambda via
boto3) to quarantine non-compliant resources in under 3 seconds. - Local Cloud Testing with Moto: Unit testing cloud automation Python locally with in-memory AWS mocks without needing real credentials.
๐ ๏ธ Job-Essential Exercises
- The Remote Backend Setup:
- Manually create an S3 bucket and DynamoDB table. Configure a Terraform backend block to use them. Verify state locking by running
terraform applyin two terminal windows simultaneously.
- Manually create an S3 bucket and DynamoDB table. Configure a Terraform backend block to use them. Verify state locking by running
- The Cross-Account Deployer:
- Write a Terraform script with two AWS providers (using aliases and
assume_role). Deploy an S3 bucket in Account A and an SQS queue in Account B in a singleterraform apply.
- Write a Terraform script with two AWS providers (using aliases and
- The State Import Challenge:
- Go into the AWS Console and manually create an EC2 instance. Write the corresponding Terraform code, and use
terraform importto map the manual instance to your code without destroying it.
- Go into the AWS Console and manually create an EC2 instance. Write the corresponding Terraform code, and use
- Shift-Left Security Gate:
- Write a Terraform module for an S3 bucket with encryption explicitly turned off. Run
checkov -f main.tfand watch it fail. Fix the code, re-run, and pass.
- Write a Terraform module for an S3 bucket with encryption explicitly turned off. Run