Skip to content

Drift detection " terraform plan --refresh-only --detailed-exitcode " exits with 2, even when there are no changes. #36403

@stehubhex

Description

@stehubhex

Terraform Version

Terraform v1.10.5
on windows_amd64
+ provider registry.terraform.io/hashicorp/azurerm v4.16.0

Terraform 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

  1. terraform init
  2. terraform plan
  3. terraform apply
  4. terraform plan -refresh-only -detailed-exitcode
  5. $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

Image

References

Generative AI / LLM assisted development?

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions