Terraform offers the following exit codes:
0 = Succeeded with empty diff (no changes)
1 = Error
2 = Succeeded with non-empty diff (changes present)
I need to perform terraform apply only if any new resources need to be added or there are any changes to apply.
if [ $? -eq 0 ]; then
echo "No changes, not applying"
elif [ $? -eq 1 ]; then
echo "Terraform plan failed"
exit 1
elif [ $? -eq 2 ]; then
echo "Terraform apply needed"
fi
But I am getting exit code 0, even for a terraform plan command which outputs:
Plan: 9 to add, 0 to change, 0 to destroy.
What is the issue?