Introduction
Problem: Sometimes, when provisioning infrastructure and executing a run in HCP Terraform, the run successfully addresses resource drift and provisions the required resources. However, it may fail to update certain attributes or metadata in the state file, even though these changes should have been applied as part of the run.
Cause: Such issues can occur due to intermittent network interruptions, timeouts, or other runtime anomalies during the apply phase. As a result, while the infrastructure changes are applied, the state file may not reflect all updates accurately.
Solution:
To resolve this, you can perform a no-operation (empty) apply run. This allows HCP Terraform to update the state file without making any actual infrastructure changes.
An empty apply run processes a plan that contains no resource changes. During the apply phase, Terraform can still update the state file or upgrade the state version if needed. This is particularly useful when:
The plan has no changes, but state metadata needs updating.
You're upgrading the workspace to a newer Terraform version (e.g., from Terraform 0.12 to 0.13).
If no changes are detected in the plan, HCP Terraform will automatically apply it and update the workspace's state accordingly. If changes are present, the run behaves like a standard apply, requiring confirmation (unless auto-apply is enabled).
How to trigger an empty apply run:
Via API:
Set the
allow-empty-applyfield totruewhen creating the run via the Terraform Cloud API.-
API Documentation: Create a Run
Via UI:
Navigate to the workspace’s Overview page.
Click + New Run.
Select Allow empty apply as the run type.
Trigger the run.
Outcome: Executing an empty apply run will ensure the state file is updated and synchronized with the current configuration. This resolves inconsistencies resulting from previous incomplete or partial state updates
Additional Information:
Empty apply run: https://developer.hashicorp.com/terraform/cloud-docs/run/modes-and-options#allow-empty-apply
Create Run API call: https://developer.hashicorp.com/terraform/cloud-docs/api-docs/run#create-a-run