Intermediate

Terraform in CI/CD

Build a GitHub Actions pipeline that runs terraform plan on pull requests and terraform apply on merge to main, with format checks, a plan-output comment, and manual approval gating production applies.

~2.5h
0 / 7 steps
🚀
Intro

What you're building

Running terraform apply from someone's laptop works until it doesn't — nobody else can see what was applied, credentials sit on individual machines, and there's no consistent record of what changed and why. The standard fix is running Terraform from CI: a pull request triggers a terraform plan whose output gets posted as a PR comment for review, and merging to main triggers the terraform apply, ideally gated by an explicit approval step for anything touching production.

You'll build this in GitHub Actions specifically, since it's free for public repos and common enough to be broadly transferable — the same shape (plan on PR, apply on merge, gate on approval) applies almost unchanged to GitLab CI, CircleCI, or HCP Terraform's own VCS-driven workflow if that's what your team actually uses.

🔨

Step 1 — Set up OIDC authentication instead of long-lived AWS keys

🔨

Step 2 — Write the plan-on-PR job

🔨

Step 3 — Add the apply-on-merge job with manual approval

🔨

Step 4 — Handle plan-vs-apply drift

🎯
Secret Mission

Secret Mission: add a policy-as-code check before apply

🧹
Wrap Up

Before You Go

Pro

Test what you just learned

Self-testing is one of the best ways to retain new skills. Unlock project quizzes to check your understanding.

Log in to unlock