DEV Community

Cover image for Day 94: A State File Is Not the Cloud and an Empty String That Deleted Something

Day 94: A State File Is Not the Cloud and an Empty String That Deleted Something

Nnamdi Felix Ibe on November 28, 2026

Day 94 of DevOps, Day 44 of Azure. First Terraform task in the series, which closes the Ansible block at Days 82 to 93. Both halves ask the same q...
Collapse
 
dafili profile image
Renhua Wei •

"Proves something narrower than it looks" applies to terraform plan too: it exits 0 whether or not there is a diff, so the CI step that runs plan and trusts the exit code is the same trap one level up. terraform plan -detailed-exitcode gives 0 for no changes, 2 for changes pending, and 1 for an error — gate on 2 and the drift you were chasing fails the build instead of printing green.

Collapse
 
ndcodes profile image
Nnamdi Felix Ibe •

@dafili Right, and the codes are documented as you have them: 0 empty diff, 2 non-empty, 1 error.

Though a 2 means config and reality disagree, not that the world moved. On a PR where I've just edited main.tf, a 2 is expected, so failing on it would block every change. It only means drift on a scheduled run against unchanged config.

Collapse
 
dafili profile image
Renhua Wei •

The distinction you want isn't in the exit code, it's in the plan body — 2 only says "something changed", so gate on the changed addresses instead: terraform plan -out=tfplan -detailed-exitcode; terraform show -json tfplan | jq -r '[.resource_changes[]? | select(.change.actions != ["no-op"]) | .address] | join(",")', then assert that list is a subset of the module paths the PR touched, and empty on the scheduled run. Same 2, two different predicates, and the PR stops getting blocked. One catch: a moved or import block also lands in resource_changes as non-no-op, so a refactor PR passes the subset check only if you allow those addresses explicitly.

Thread Thread
 
ndcodes profile image
Nnamdi Felix Ibe •

@dafili Agreed on moving the predicate into the plan body, and both your catches are already labelled. A moved resource carries previous_address, and an imported one carries importing, so you can filter on those instead of keeping an allow-list.

And for the scheduled run, resource_drift is the field rather than resource_changes. It's what Terraform detected when comparing the current state to the prior state, which is drift. resource_changes is what it plans to do about it.

Thread Thread
 
dafili profile image
Renhua Wei •

resource_drift[].change.actions uses the same action vocabulary, so that filter is the same loop rather than a second one. One thing worth knowing: a drift-only plan still exits 2 under -detailed-exitcode, but applyable at the top level is false there, which is the cheapest line between "state moved" and "something to apply" if a human has to be paged.

Collapse
 
xulingfeng profile image
xulingfeng •

Only six will be 100🎉

Collapse
 
ndcodes profile image
Nnamdi Felix Ibe •

Six! 😄 I'll finish just in time to start wondering what to do with a free evening. Oh dear me! I still got a chair at the café.

Cheers @xulingfeng

Collapse
 
xulingfeng profile image
xulingfeng •

🍻hahaha,cheers!

Collapse
 
csm18 profile image
csm •

For your consistency 🙌🙌

Collapse
 
ndcodes profile image
Nnamdi Felix Ibe •

Thank you,@csm18 csm. 🙏 Six more, and then I get my evenings back. 😄

Collapse
 
honeykiran profile image
Honey Kiran •

"A command succeeded, so what did it actually prove?" is the exact question, and both halves of your day are the same answer: it proved the step ran. Nothing more.

Terraform's state listing proves what Terraform believes. The Azure step's success proved the script executed — while it deleted the placeholder it was meant to fill. Exit codes report on the command. They say nothing about the world.

The habit that survives contact with this: verify the world, not the report of the world. Read back the resource, the file, the live page — whatever the ground truth is — and grade that. Day 94's lesson is worth more than most tooling chapters: success is a claim, and claims get checked.

Collapse
 
ndcodes profile image
Nnamdi Felix Ibe •

I like what you said about "Verify the world, not the report of the world", so I would be stealing that. Is really a good habit to have.

The cruel part of the Azure half is that nothing lied to me. The patch honestly reported the placeholder removed, and it was removed. 😄