Skip to content

/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

bash
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

CodeMeaningTypical CI action
0succeeded, no changes (infrastructure matches the configuration)pass; nothing to apply
1error (syntax, provider, credentials, state lock …)fail the job
2succeeded, there are changespost the plan for review, or alert on drift

Drift detection job

bash
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 ;;
esac

Sources