/codes/terraform/terraform-plan-detailed-exitcode
terraform plan -detailed-exitcode: exit codes 0, 1 and 2
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · Terraform v1.16.6updated 2026-10-20
- tool
- Terraform
- versions
- Terraform 1.x, OpenTofu 1.x
- os
- any
Normally terraform plan exits 0 whether or not it found changes. With -detailed-exitcode the status tells you which case happened: 0 means success with no changes, 1 means an error, 2 means success and the plan contains changes.
All three codes
terraform init -input=false -no-color >/dev/null
touch main.tf
terraform plan -detailed-exitcode -no-color -input=false >/dev/null; echo "no changes: $?"
printf 'resource "terraform_data" "app" {\n input = "v1"\n}\n' > main.tf
terraform plan -detailed-exitcode -no-color -input=false >/dev/null; echo "changes: $?"
echo 'resource "broken" {' > main.tf
terraform plan -detailed-exitcode -no-color -input=false >/dev/null 2>&1; echo "error: $?"Output
no changes: 0 changes: 2 error: 1
Exit codes
| Code | Meaning | Typical CI action |
|---|---|---|
| 0 | succeeded, no changes (infrastructure matches the configuration) | pass; nothing to apply |
| 1 | error (syntax, provider, credentials, state lock …) | fail the job |
| 2 | succeeded, there are changes | post the plan for review, or alert on drift |
Drift detection job
set +e
terraform plan -detailed-exitcode -input=false -lock=false -out=tfplan
code=$?
set -e
case $code in
0) echo "no drift" ;;
2) echo "drift or pending changes"; exit 2 ;;
*) echo "plan failed"; exit 1 ;;
esacSources
Related pages
flags · terraformterraform plan -out: save a plan and apply exactly that planterraform plan -out=tfplan saves the plan to a file; terraform apply tfplan then applies exactly those changes without a prompt. Inspect it with terraform show.commands · terraformterraform fmt -check and -recursive: format HCL filesterraform fmt rewrites .tf files in canonical style; -check only reports unformatted files with a non-zero exit, -diff shows changes, -recursive adds modules.codes · bashBash exit codes: 0, 1, 2, 126, 127, 128+n and 255What bash exit codes mean: 0 success, 1 general error, 2 misuse of builtins, 126 not executable, 127 not found, 128+n killed by signal n, and how to read $?.