During a 'terraform plan', you see the following output: 'Plan: 1 to add, 2 to change, 0 to destroy.' However, after running 'terraform apply', the actual number of resources changed is different. What is the most likely reason?
Trap 1: Terraform state locking prevented the apply from executing…
State locking is designed to prevent multiple concurrent operations from modifying the state file simultaneously, ensuring data integrity and consistency. If a lock were active, `terraform apply` would typically either block until the lock is released or fail with an explicit locking error, preventing execution. It would not, however, cause the number of planned changes to silently diverge from a previously generated plan; the operation would simply not proceed as intended.
Trap 2: The state file was corrupted during the apply.
State file corruption would manifest as critical errors during `terraform apply`, preventing the operation from proceeding or leading to unpredictable failures due to unreadable or invalid resource definitions. Terraform would likely halt execution, reporting parsing errors or missing resource data, rather than successfully applying a different set of changes. It would not result in a successful apply with a modified count of changes; instead, it would likely abort the execution entirely.
Trap 3: The 'terraform apply' command includes an implicit refresh that…
`terraform apply` does indeed perform an implicit refresh to update the state file with the current real-world infrastructure status before proposing changes. However, this refresh primarily updates the *known state* of resources; it does not inherently alter the *desired state* defined by the configuration or the *actions* derived from it. A different number of changes would only occur if the configuration itself was modified, not merely due to a state refresh synchronizing the actual infrastructure with the state file.
- A
Terraform state locking prevented the apply from executing correctly.
Why wrong: State locking is designed to prevent multiple concurrent operations from modifying the state file simultaneously, ensuring data integrity and consistency. If a lock were active, `terraform apply` would typically either block until the lock is released or fail with an explicit locking error, preventing execution. It would not, however, cause the number of planned changes to silently diverge from a previously generated plan; the operation would simply not proceed as intended.
- B
The state file was corrupted during the apply.
Why wrong: State file corruption would manifest as critical errors during `terraform apply`, preventing the operation from proceeding or leading to unpredictable failures due to unreadable or invalid resource definitions. Terraform would likely halt execution, reporting parsing errors or missing resource data, rather than successfully applying a different set of changes. It would not result in a successful apply with a modified count of changes; instead, it would likely abort the execution entirely.
- C
The configuration was modified after the plan was generated.
The `terraform plan` command generates an execution plan based on the configuration files present at the time it is run, representing a snapshot of the desired state. If the Terraform configuration (`.tf` files) is altered *after* a plan is generated but *before* `terraform apply` is executed, the `apply` command will use the *modified* configuration. This discrepancy causes the actual changes performed during `apply` to differ from the previously generated plan, as the desired state has fundamentally changed.
- D
The 'terraform apply' command includes an implicit refresh that changes the plan.
Why wrong: `terraform apply` does indeed perform an implicit refresh to update the state file with the current real-world infrastructure status before proposing changes. However, this refresh primarily updates the *known state* of resources; it does not inherently alter the *desired state* defined by the configuration or the *actions* derived from it. A different number of changes would only occur if the configuration itself was modified, not merely due to a state refresh synchronizing the actual infrastructure with the state file.