Opscraft
Terraform · Lesson 8 of 12 · 3 min read

Team workflow, CI/CD and policy

A pull-request workflow

  1. Engineer opens a PR changing Terraform.
  2. CI runs fmt -check, validate, tflint, security scanning (Trivy/tfsec, Checkov), then plan for each affected stack, and posts the plan to the PR.
  3. 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.
  4. Reviewers read the plan, not just the code — especially any -/+ replacements or destroys.
  5. After approval/merge, CI applies the saved plan (or Atlantis applies on comment), with a protected environment for prod.
  6. Scheduled drift detection runs plan -detailed-exitcode nightly.

Tools: Atlantis (PR automation, self-hosted), HCP Terraform/Terraform Enterprise, Spacelift, env0, or plain GitHub Actions/Jenkins.

Enlarge diagramOpen PRchange .tf filesChecksfmt, validate, tflint,Trivy/CheckovPlanposted on the PRPolicyOPA/conftest on plan JSONReviewread the plan, watch for -/+ApplyCI applies the saved plan aftermerge123456
Figure 1. The pull-request workflow for Terraform: humans review the plan, and only CI applies.
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 diagramPull requestany branchmain + approvalprotected environmentplan roleread-only + state readapply rolewrite accessAWSaccountOIDCOIDC
Figure 2. Least privilege for pipelines: a read-only role plans on every PR; only the protected main branch can assume the apply role.
Swipe sideways to see all of it, or tap to open it full screen.