-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Drift detection " terraform plan --refresh-only --detailed-exitcode " exits with 2, even when there are no changes. #36403
Description
Terraform Version
Terraform v1.10.5
on windows_amd64
+ provider registry.terraform.io/hashicorp/azurerm v4.16.0Terraform Configuration Files
# Resource Group
resource "azurerm_resource_group" "rg" {
name = var.resource_group_name
location = var.location
tags = {
Environment = "Testing"
}
}
# Storage Account
resource "azurerm_storage_account" "sa" {
name = var.storage_account_name
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
account_tier = "Standard"
account_replication_type = "LRS"
tags = {
Environment = "Testing"
}
}Debug Output
No changes. Your infrastructure still matches the configuration.
Terraform has checked that the real remote objects still match the result of your most recent changes, and found no differences.
Expected Behavior
The command terraform plan -refresh-only -detailed-exitcode should return 0 if the configuration was just deployed with terraform apply and there are no changes detected .
Actual Behavior
When running the command terraform plan -refresh-only -detailed-exitcode it returns "$LASTEXITCODE 2" even though it is ran right after terraform apply and there are no changes detected .
Steps to Reproduce
- terraform init
- terraform plan
- terraform apply
- terraform plan -refresh-only -detailed-exitcode
- $LASTEXITCODE
Additional Context
When deploying a new configuration methodically and checking the drift on each step I see the following :
Deploying successfully the resource "azurerm_resource_group" "rg" and running terraform plan -refresh-only -detailed-exitcode -out=tfplan and then checking $LASTEXITCODE returns 0 as expected. Checking the plan with terraform show -json tfplan also confirms that there are no changes pending.
Continuing with the same actions for the next resource "azurerm_storage_account" "sa" , it is successfully deployed. Immediately after the terraform apply a terraform plan -refresh-only -detailed-exitcode -out=tfplan1 is triggered in the terminal. This results in "No changes." , but suddenly $LASTEXITCODE returns 2 . Reviewing the plan terraform show -json tfplan1 also does not indicate any changes.
This behavior is seen with other resources as well for example: resource "azurerm_service_plan" "sp" ; resource "azurerm_linux_function_app" "fn" ; and some more complicated resources other than a "azurerm_resource_group" .
The documentation clearly explains the expected behavior and results for the -detailed-exitcode option
References
- terraform plan -refresh-only -detailed-exitcode gives incorrect exit code 2 even if the console output says no changes detected #35117
- Drift detection feature request:
terraform planshould execute Checks and exit with0if everything matches. #35226
Generative AI / LLM assisted development?
No response
