CD4: Terraform for teams

Lesson 4 of 5 · reading 15–25 minutes; practice and project time is on the module page

Reviewing infrastructure changes

🎯You'll be able to

Run plans in pull requests, apply exactly the plan that was reviewed, and add automatic checks that catch dangerous or non-compliant changes.

🏠The everyday picture

Before a bank transfer above a limit goes out, a second officer checks the details on the form and signs. The transfer that goes out is the one on the signed form, not a new one typed again later.

📘What it really is

📝A worked example

Input: in the finished kit, run terraform plan -out=tfplan -var app_count=3, then terraform show -json tfplan > plan.json and node -e "const p=require('./plan.json'); console.log(p.resource_changes.filter(r => r.change.actions.includes('delete')).map(r => r.address))".

Expected output: the printed list shows the resources the plan would delete, here none (the third copy is only added). terraform apply tfplan then applies exactly that. Running terraform apply tfplan a second time fails because the saved plan is now stale. This is the idea behind a policy check that stops plans which delete something important.

🌍Where it's used

📝Case scenario

A logistics company let engineers apply from laptops after a quick code review. A reviewer approved a one-line change to a subnet's address range; nobody looked at the plan, which replaced the subnet and every server in it. Afterwards, plans ran in pull requests, any plan with deletions needed a second reviewer, and only the pipeline could apply to production.

🖱️Try it

Save a plan, apply it, then try to apply the same file again. Then add -var app_count=1 to a new plan and run the delete-listing command. Success: you see the stale-plan refusal, and the second list shows the copies that would be removed.

⚠️Common mistakes

🛠️For your project

Project 2's workflow checks formatting and validity in pull requests. As a stretch, add a plan step and a check that fails if the plan deletes the database.

✅Quick check

What does terraform apply tfplan add over a plain apply?

Show the answer

It applies exactly the saved, reviewed plan, and refuses if state has changed since.

Name one kind of automatic check for infrastructure changes.

Show the answer

A linter (TFLint), a security scanner (Checkov, Trivy) or a policy check (OPA, Sentinel).

← PreviousEnvironments: dev, staging and production
ProLearners Assist