Infrastructure as Code (IaC) Review Process Implementation
This guide implements a robust IaC review process using Terraform (the most common IaC tool), GitHub for version control and peer reviews, GitHub Actions for CI/CD, Open Policy Agent (OPA) with Conftest for policy checks, Terraform Cloud for remote state/drift detection/rollouts, and Git history + CloudWatch/ELK for audit trails. The process enforces:
Module Versioning : Semantic versioning (SemVer) with Git tags.
Automated Policy Checks : Security/compliance scanning.
Drift Detection : Pre-apply validation.
Peer Reviews : Required PR approvals.
Staging Rollouts : Progressive promotion (dev → staging → prod).
Rollback Procedures : Automated with audit trails.
Architecture Overview
code Copy code
GitHub Repo (IaC Modules + Environments)
├── modules/ # Versioned modules (e.g., v1.2.0)
├── environments/ # env-specific configs (dev/staging/prod)
└── .github/workflows/ # CI/CD pipelines
Triggers:
- PR: Lint → Policy Checks → Plan → Peer Review
- Merge to main: Drift Detect → Apply to Staging → Promote to Prod
- Rollback: Git revert + re-apply
Tools Stack :
Component Tool VCS GitHub CI/CD GitHub Actions + Atlantis (optional for PR plans) IaC Terraform State/Drift Terraform Cloud (TFC) Policy OPA/Conftest + Checkov Audit Git logs + TFC runs + S3/CloudWatch
Step-by-Step Implementation
1. Repository Structure & Module Versioning
Organize repo for reusability and versioning.
code Copy code
my-iac-repo/
├── modules/
│ └── vpc/ # Example module
│ ├── main.tf
│ ├── variables.tf
│ ├── outputs.tf
│ └── versions.tf
├── environments/
│ ├── dev/
│ │ ├── main.tf # Calls modules with env vars
│ │ ├── terraform.tfvars
│ │ └── backend.tf # Points to TFC workspace
│ ├── staging/ # Similar structure
│ └── prod/
├── policies/ # OPA policies (rego files)
├── .github/
│ └── workflows/
│ ├── pr-checks.yml # PR validation
│ ├── deploy-staging.yml
│ └── deploy-prod.yml
└── README.md
Version Modules :
Use Git tags: git tag modules/vpc/v1.2.0 && git push --tags.
In main.tf: source = "git::https://github.com/org/my-iac-repo.git//modules/vpc?ref=modules/vpc/v1.2.0".
Automate tagging in CI (see workflows below).
Enforce : Branch protection on main requires tags for merges.
2. Automated Policy Checks
Scan for compliance (e.g., no public S3 buckets, encrypted volumes).
Install Tools :
Checkov: pip install checkov.
OPA/Conftest: brew install conftest or Docker.
Policy Examples (policies/s3.rego):
rego Copy code
package terraform
deny[msg] {
resource := input.planned_values.root_module.resources[_]
resource.type == "aws_s3_bucket"
not resource.values.server_side_encryption_configuration
msg := "S3 buckets must have server-side encryption"
}
GitHub Actions Workflow (.github/workflows/pr-checks.yml):
yaml Copy code
name: PR IaC Checks
on: [pull_request]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
with: { terraform_version: 1.5.0 }
- name: Terrascan/Checkov
uses: aquasecurity/tfsec-action@v1 # Or checkov-action
with: { soft_fail: true }
- name: OPA Policy Check
uses: open-policy-agent/conftest-action@v1
with:
modules: policies/
path: environments/
- name: Terraform Validate
run: terraform validate
- name: Terraform Plan
env:
TF_CLOUD_ORG: myorg
TF_API_TOKEN: ${{ secrets.TF_API_TOKEN }}
run: |
cd environments/dev # Or matrix for all envs
terraform init
terraform plan -out=tfplan
terraform show -json tfplan > plan.json # For drift/policy
3. Drift Detection
Detect config vs. actual state mismatches.
Use Terraform Cloud Workspaces per env (dev/staging/prod).
In backend.tf:
hcl Copy code
terraform {
cloud {
organization = "myorg"
workspaces { name = "dev" } # Dynamic per env
}
}
Workflow Extension (add to pr-checks.yml post-plan):
yaml Copy code
- name: Drift Detection
run: |
terraform plan -detailed-exitcode # 2 = drift
if [ $? -eq 2 ]; then echo "Drift detected!"; exit 1; fi
Scheduled Drift Check: Cron job in GitHub Actions (on: schedule daily).
4. Peer Reviews
GitHub Branch Protection (Settings > Branches > main):
Require PR reviews: ≥2 approvals.
Require status checks: pr-checks, terraform-plan.
Dismiss stale approvals on push.
Use labels: approved, needs-review.
Optional: Atlantis for comment-driven applies (atlantis plan, atlantis apply).
5. Staging Rollouts
Progressive deployment post-merge.
Deploy Staging (.github/workflows/deploy-staging.yml):
yaml Copy code
name: Deploy Staging
on:
push:
branches: [main]
paths: ['environments/staging/**', 'modules/**']
jobs:
deploy:
runs-on: ubuntu-latest
environment: staging # GitHub environments for approvals
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
- name: Drift + Policy + Plan
# Reuse pr-checks logic
- name: Apply
env: { TF_API_TOKEN: ${{ secrets.TF_API_TOKEN }} }
run: |
cd environments/staging
terraform init
terraform apply -auto-approve tfplan # Or TFC run
Promote to Prod (manual trigger + manual approval):
.github/workflows/deploy-prod.yml: Trigger on workflow_dispatch (manual).
Require manual approval in GitHub Environments (Settings > Environments > prod > Require approval).
6. Rollback Procedures with Audit Trails
Git-Based Rollback :
git revert HEAD (or specific commit).
PR → Merge → Auto-deploy.
Terraform Rollback :
Tag previous version: git checkout modules/vpc/v1.1.0 && git tag rollback/v1.1.0.
Workflow: terraform apply with old module ref.
Automated Rollback (on apply failure):
yaml Copy code
# In deploy workflows, post-apply
- name: Health Check
run: curl -f https://staging.example.com/health || (terraform apply -target=module.old_version)
Audit Trails :
Source Tool Retention Code Changes GitHub Audit Logs 90 days TF Plans/Applies TFC Run Logs (export to S3) 1 year Policies/Drift GitHub Actions Artifacts 30 days Full Audit AWS CloudTrail + S3 Indefinite
Export TFC logs: TFC Sentinel policy or API webhook to CloudWatch.
Query: aws logs query --query-text "terraform apply".
Best Practices & Monitoring
Secrets : Use GitHub Secrets/OIDC for TFC.
Matrix Jobs : Test all envs in parallel.
Notifications : Slack/Teams on failure/rollback via Actions.
Metrics : Track MTTR with GitHub Insights + TFC Cost Estimates.
Scale : Use Terragrunt for DRY envs, Spacelift for enterprise.
Testing : Add terratest or kitchen-terraform to CI.
Onboard :
Clone repo, set up TFC workspaces.
Configure GitHub protections/secrets.
Test: Create PR → Observe full flow.
This process ensures safe, auditable IaC changes. Customize for your cloud (AWS/GCP/Azure). Questions? Provide your stack for tweaks!