nerdexam
HashiCorp

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.

Implement and maintain state

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)
  • B
    95% (18)
  • C
    5% (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.

ARun terraform refresh

`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.

BIt will happen automaticallyCorrect

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.

CManually update the state fire

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.

DRun terraform import

`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

#Terraform State#State Management#Drift Detection#Implicit Refresh

Community Discussion

No community discussion yet for this question.

Full TA-002-P Practice