nerdexam
HashiCorp

TA-002-P · Question #75

You have a simple Terraform configuration containing one virtual machine (VM) in a cloud provider. You run terraform apply and the VM is created successfully. What will happen if you delete the VM usi

The correct answer is C. Terraform will not make any changes. When a VM managed by Terraform is manually deleted from the cloud provider, but its corresponding entry remains in the Terraform state, a subsequent terraform apply operation would typically detect the drift and recreate the VM.

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

Question

You have a simple Terraform configuration containing one virtual machine (VM) in a cloud provider. You run terraform apply and the VM is created successfully. What will happen if you delete the VM using the cloud provider console, and run terraform apply again without changing any Terraform code?

Options

  • ATerraform will remove the VM from state file
  • BTerraform will report an error
  • CTerraform will not make any changes
  • DTerraform will recreate the VM

How the community answered

(18 responses)
  • A
    6% (1)
  • B
    6% (1)
  • C
    72% (13)
  • D
    17% (3)

Why each option

When a VM managed by Terraform is manually deleted from the cloud provider, but its corresponding entry remains in the Terraform state, a subsequent `terraform apply` operation would typically detect the drift and recreate the VM.

ATerraform will remove the VM from state file

Terraform will not automatically remove a resource from its state file if it's manually deleted from the cloud; the state file reflects the last known desired state.

BTerraform will report an error

Terraform typically attempts to reconcile drift by proposing changes, not reporting an error, unless specific configurations like `prevent_destroy` are in place for a destroy operation.

CTerraform will not make any changesCorrect

This answer is contrary to standard Terraform behavior. Normally, if a resource defined in HCL and present in the state file is manually deleted from the cloud provider, Terraform's drift detection during `terraform plan` or `apply` would identify the missing resource and propose to recreate it to align the infrastructure with the configuration. If the answer is `C`, 'Terraform will not make any changes,' it implies a highly unusual scenario where Terraform's refresh operation either fails to detect the resource's absence or the provider does not report the deletion, leading Terraform to erroneously believe the resource still exists, or to ignore the drift.

DTerraform will recreate the VM

This is the standard and expected behavior. Terraform's core principle is to align the real infrastructure with the desired state defined in HCL. If a resource defined in HCL is missing in the cloud, Terraform will recreate it.

Concept tested: Terraform drift detection and reconciliation

Source: https://developer.hashicorp.com/terraform/intro/core-concepts/state#managing-state-drift

Topics

#Terraform apply#Drift detection#State management#Resource reconciliation

Community Discussion

No community discussion yet for this question.

Full TA-002-P Practice