TA-002-P · Question #48
You have never used Terraform before and would like to test it out using a shared team account for a cloud provider. The shared team account already contains 15 virtual machines (VM). You develop a Te
The correct answer is B. The Terraform state file only contains the one new VM. Execute terraform destroy.. When a Terraform configuration creates a new resource, its state file only tracks that specific resource, allowing terraform destroy to target only the managed resources.
Question
You have never used Terraform before and would like to test it out using a shared team account for a cloud provider. The shared team account already contains 15 virtual machines (VM). You develop a Terraform configuration containing one VM, perform terraform apply, and see that your VM was created successfully. What should you do to delete the newly-created VM with Terraform?
Options
- AThe Terraform state file contains all 16 VMs in the team account. Execute terraform
- BThe Terraform state file only contains the one new VM. Execute terraform destroy.
- CDelete the Terraform state file and execute Terraform apply.
- DDelete the VM using the cloud provider console and terraform apply to apply the changes
How the community answered
(34 responses)- B91% (31)
- C3% (1)
- D6% (2)
Why each option
When a Terraform configuration creates a new resource, its state file only tracks that specific resource, allowing `terraform destroy` to target only the managed resources.
The Terraform state file does not automatically discover and include pre-existing resources in the cloud provider; it only contains the resources explicitly managed by the configuration.
The Terraform state file explicitly tracks only the resources defined and managed by its corresponding configuration; therefore, `terraform destroy` will only delete the one VM that was created by this Terraform configuration.
Deleting the Terraform state file removes Terraform's knowledge of the managed VM, making it impossible for Terraform to destroy the VM through its own state management.
Deleting the VM directly through the cloud provider console removes it from the infrastructure but leaves a discrepancy with the Terraform state file, which would then attempt to re-create the VM on the next `terraform apply`.
Concept tested: Terraform state file management and destroy operations
Source: https://developer.hashicorp.com/terraform/cli/commands/destroy
Topics
Community Discussion
No community discussion yet for this question.