HashiCorp Terraform Associate (004) Question 41
Single answer2d Explain how Terraform uses and manages stateA team stores its Terraform state in an S3 backend for a shared AWS networking workspace. Last week, an engineer manually deleted one of the subnets in the AWS console without updating the Terraform configuration. Today, another engineer runs terraform plan from the same workspace and notices Terraform wants to recreate the missing subnet. Which statement best explains why Terraform behaves this way?
- A
Terraform compares the configuration to the recorded state and refreshes real infrastructure data, so it detects that a resource tracked in state is missing and plans to recreate it.
- B
Terraform only reads the configuration files during plan, so it assumes any resource in the code already exists and recreates it without checking state.
- C
Terraform stores the full resource configuration only in AWS tags, so deleting the subnet removed the configuration and forced Terraform to rebuild it.
- D
Terraform ignores remote state changes unless terraform refresh is run first, so the recreation plan is caused by stale local cache rather than the actual remote state.
Show answer and explanation
Correct answer: A
Explanation
Terraform uses state to keep a mapping between resources in your configuration and the corresponding real infrastructure objects. This state allows Terraform to determine what it manages, what attributes are known, and what actions are needed to converge actual infrastructure toward the declared configuration. When a managed resource is deleted outside Terraform, that is drift. During terraform plan, Terraform typically refreshes resource data from the provider, detects the object is missing, and, because the resource is still declared in configuration, plans to create it again. Using a remote backend such as S3 changes where state is stored and shared, but it does not change Terraform's core state behavior. This aligns with Terraform documentation on state, refresh during planning, and drift detection best practices.
- A. Correct.
Correct. Terraform state maps resources in configuration to real-world infrastructure objects. During planning, Terraform uses the state and refreshes information from the provider to determine the current real-world status of managed objects. If a resource still exists in configuration and state but is found missing in the provider, Terraform will typically propose creating it again to reconcile actual infrastructure with the declared configuration.
- B. Incorrect.
Incorrect. Terraform does not ignore state during planning. State is central to how Terraform tracks managed resources and maps them to provider objects. A common misconception is that Terraform works only from configuration, but in practice it uses configuration, state, and provider-read data together to build the execution plan.
- C. Incorrect.
Incorrect. Terraform does not store its resource configuration in AWS tags as its source of truth. The source of truth for desired infrastructure is the Terraform configuration, while state stores metadata and resource bindings. Although providers may set tags on resources, those tags are not how Terraform preserves or reconstructs state.
- D. Incorrect.
Incorrect. In normal operation, terraform plan refreshes state by querying the provider unless refresh is explicitly disabled. The behavior described is not due to a local cache issue. Another misconception here is that remote backend state prevents refresh; in reality, remote state storage changes where state is stored, not whether Terraform can detect drift.