nerdexam
HashiCorp

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.

Use the Terraform CLI (terraform plan, apply, destroy, fmt, init, validate, workspace, import, taint, providers, output)

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)
  • B
    91% (31)
  • C
    3% (1)
  • D
    6% (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.

AThe Terraform state file contains all 16 VMs in the team account. Execute terraform

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.

BThe Terraform state file only contains the one new VM. Execute terraform destroy.Correct

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.

CDelete the Terraform state file and execute Terraform apply.

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.

DDelete the VM using the cloud provider console and terraform apply to apply the changes

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

#Terraform state#terraform destroy#Resource lifecycle#Terraform CLI

Community Discussion

No community discussion yet for this question.

Full TA-002-P Practice