TA-002-P · Question #14
If you manually destroy infrastructure, what is the best practice reflecting this change in Terraform?
The correct answer is B. It will happen automatically. When infrastructure is manually destroyed, Terraform automatically detects this drift during the refresh phase of subsequent terraform plan or terraform apply commands, synchronizing the state file with the real world.
Question
If you manually destroy infrastructure, what is the best practice reflecting this change in Terraform?
Options
- ARun terraform refresh
- BIt will happen automatically
- CManually update the state fire
- DRun terraform import
How the community answered
(19 responses)- B95% (18)
- C5% (1)
Why each option
When infrastructure is manually destroyed, Terraform automatically detects this drift during the refresh phase of subsequent `terraform plan` or `terraform apply` commands, synchronizing the state file with the real world.
`terraform refresh` is deprecated in favor of the implicit refresh in `terraform plan` and `terraform apply`, or using `terraform plan -refresh-only`, making "automatic detection" during normal workflow a better description.
Terraform automatically performs a refresh operation as part of `terraform plan` and `terraform apply` commands. If a resource is manually destroyed, this implicit refresh will detect its absence and remove it from the Terraform state, thereby reflecting the change automatically without needing a separate `terraform refresh` command.
Manually editing the Terraform state file (`terraform.tfstate`) is highly discouraged due to its complexity and potential for corruption, and there are built-in commands for managing state changes.
`terraform import` is used to bring *existing* infrastructure under Terraform management, not to remove manually destroyed resources from the state file.
Concept tested: Terraform state drift detection and synchronization
Source: https://developer.hashicorp.com/terraform/cli/commands/refresh
Topics
Community Discussion
No community discussion yet for this question.