โšก
Zero to Platform Architect2026
Phase 2: IaC & Policy-as-Code
Phase 2: IaC & Policy-as-Codeโฑ๏ธ 3 min readโšก Staff SRE Standard
๐ŸŽฏ Foundations Test Bank →

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

  1. Step 1 (State & Locking): Configure S3 + DynamoDB state locking in Lesson 1: Terraform State & Modules.
  2. Step 2 (Policy Gates): Write Rego & Checkov CI gates in Lesson 2: Policy-as-Code.
  3. Step 3 (Fleet Scaling & Secrets): Scale DRY configs across 50 accounts with Lesson 3: Terragrunt and dynamic credentials with Lesson 4: Vault.
  4. Step 4 (Refactoring & Automation): Master zero-downtime moved blocks (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_each vs count, dynamic blocks, locals, data sources, 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 provider blocks with alias. Using assume_role to 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

  1. 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 apply in two terminal windows simultaneously.
  2. 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 single terraform apply.
  3. The State Import Challenge:
    • Go into the AWS Console and manually create an EC2 instance. Write the corresponding Terraform code, and use terraform import to map the manual instance to your code without destroying it.
  4. Shift-Left Security Gate:
    • Write a Terraform module for an S3 bucket with encryption explicitly turned off. Run checkov -f main.tf and watch it fail. Fix the code, re-run, and pass.