HashiCorp Terraform Associate (004) Question 44
Single answer2d Explain how Terraform uses and manages stateA team stores Terraform state for a shared AWS environment in an S3 backend with DynamoDB state locking. One engineer manually deletes an EC2 instance in the AWS console, but the resource block remains in the Terraform configuration. Later, another engineer runs terraform plan from their workstation. Which outcome should the team expect, and why?
- A
Terraform will compare the configuration with the recorded state and the provider's current view of the infrastructure, detect that the instance is missing, and plan to recreate it.
- B
Terraform will trust only the state file, assume the EC2 instance still exists, and show no changes until someone runs
terraform refreshmanually. - C
Terraform will fail immediately because remote state backends prevent plans when infrastructure has drifted from the state file.
- D
Terraform will remove the EC2 instance from state automatically and delete the resource block from configuration to match the real infrastructure.
Show answer and explanation
Correct answer: A
Explanation
Terraform uses state to map configuration to real infrastructure and to track metadata about managed resources. During a normal terraform plan, Terraform typically refreshes managed resource data from the provider, allowing it to detect drift such as a resource deleted outside Terraform. If a resource still exists in configuration but is missing in the real environment, Terraform plans to recreate it. In this scenario, the S3 backend affects where state is stored, and DynamoDB locking helps coordinate safe state operations across team members, but those backend features do not change the core state behavior. This aligns with Terraform documentation on state, drift detection during planning, and backend locking best practices.
- A. Correct.
Correct. Terraform state is the record of resources it manages, but during planning Terraform also refreshes data about managed resources from the provider unless that behavior is explicitly disabled. In this scenario, Terraform will detect that the EC2 instance no longer exists in AWS even though it is still in state and configuration. Because the resource block is still present in configuration, Terraform will plan to create the instance again to converge real infrastructure to the desired configuration.
- B. Incorrect.
Incorrect. This reflects a common misconception that Terraform uses only the state file during planning. State is important, but Terraform also checks the real-world infrastructure through the provider during plan/apply in normal workflows. A separate
terraform refreshcommand is not required for drift detection in typical runs. - C. Incorrect.
Incorrect. Remote backends such as S3 store state remotely, and DynamoDB locking helps prevent concurrent writes, but neither feature causes Terraform to fail simply because drift exists. Drift is a normal condition Terraform is designed to detect and reconcile through planning.
- D. Incorrect.
Incorrect. Terraform does not automatically rewrite configuration based on infrastructure changes, and it does not silently forget managed resources just because they were deleted outside Terraform. If the configuration still declares the resource, Terraform treats it as desired and plans actions to restore it.