Terraform · Lesson 8 of 12 · 3 min read
Team workflow, CI/CD and policy
A pull-request workflow
- Engineer opens a PR changing Terraform.
- CI runs
fmt -check,validate, tflint, security scanning (Trivy/tfsec, Checkov), thenplanfor each affected stack, and posts the plan to the PR. - Policy as code checks the plan JSON (OPA/conftest, Sentinel in HCP Terraform): no public S3 buckets, no 0.0.0.0/0 SSH, required tags, allowed instance types.
- Reviewers read the plan, not just the code — especially any
-/+replacements or destroys. - After approval/merge, CI applies the saved plan (or Atlantis applies on comment), with a protected environment for prod.
- Scheduled drift detection runs
plan -detailed-exitcodenightly.
Tools: Atlantis (PR automation, self-hosted), HCP Terraform/Terraform Enterprise, Spacelift, env0, or plain GitHub Actions/Jenkins.
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.
GitHub Actions with OIDC (no static keys)
name: terraform
on:
pull_request: { paths: ["live/prod/eks/**", "modules/**"] }
push: { branches: [main], paths: ["live/prod/eks/**", "modules/**"] }
permissions:
id-token: write # needed for OIDC
contents: read
pull-requests: write
jobs:
plan-apply:
runs-on: ubuntu-latest
defaults: { run: { working-directory: live/prod/eks } }
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/gha-terraform-prod
aws-region: ap-south-1
- uses: hashicorp/setup-terraform@v3
with: { terraform_version: 1.16.7 } # pin an exact version
- run: terraform init -input=false
- run: terraform fmt -check -recursive
- run: terraform validate
- run: terraform plan -input=false -out=tfplan
- if: github.event_name == 'push'
run: terraform apply -input=false tfplan
The IAM role trusts GitHub's OIDC provider with a condition on the repository and branch/environment (token.actions.githubusercontent.com:sub = repo:acme/platform-infra:environment:prod), so only that workflow can assume it — no long-lived access keys. The same pattern works from Jenkins on EKS with IRSA.
Least privilege for Terraform
Separate roles for plan (read-only + state access) and apply (write), per account/environment. Prod apply only from CI, behind approvals. SCPs prevent dangerous actions (disabling CloudTrail, leaving the organisation) even for Terraform roles.
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.