Terraform 004 state management explained
Objectives 6 and 7 of the Terraform Associate (004) cover state management and maintaining infrastructure. They ask how Terraform stores state (local backend, locking, remote state), how to handle drift, and how to import existing resources, inspect state with the CLI and use verbose logging. These are the operational skills that separate someone who has run Terraform from someone who has only read about it.
Objective 6: State management
6a: The local backend. By default Terraform keeps state in a file called terraform.tfstate in the working directory. That is fine for learning, but it is hard to share and unsafe for teams, because the file contains resource attributes and may contain secrets.
6b: State locking. When a backend supports it, Terraform locks state during operations that could write it, so two people cannot apply at once and corrupt it. Know that support depends on the backend, and that a failed run may leave a lock that needs releasing carefully.
6c: Remote state. A backend block inside the terraform block tells Terraform to store state remotely, for example in object storage or HCP Terraform. Changing the backend needs terraform init again, and Terraform offers to migrate existing state. Remote backends give a shared source of truth, often with locking, versioning and encryption.
6d: Drift. Drift is when real infrastructure differs from state or configuration because someone changed it outside Terraform. A plan detects it by reading real objects and proposing changes to bring them back to the configuration. A refresh-only plan or apply updates state to match reality without changing infrastructure.
Objective 7: Maintaining infrastructure
| Task | Tool |
|---|---|
| Bring an existing resource under management | terraform import or an import block |
| List resources in state | terraform state list |
| Inspect one resource | terraform state show |
| Debug a provider or core problem | TF_LOG environment variable |
Import attaches an existing real object to a resource address in state. You must still write the matching resource configuration, and the newer import block lets the import be planned and reviewed like any other change. State inspection commands read state without changing infrastructure. Verbose logging is enabled with TF_LOG at a level such as TRACE, DEBUG, INFO, WARN or ERROR, and TF_LOG_PATH writes it to a file.
Typical traps
- Editing
terraform.tfstateby hand. Use state commands. - Thinking remote state removes the need for protecting secrets.
- Believing import writes the configuration for you in the classic command.
- Expecting
planto ignore changes made outside Terraform.
Sample questions
Question 1. Someone changed a security setting in the cloud console on a resource that Terraform manages. What will the next terraform plan most likely show?
- A. No changes, because Terraform ignores console edits
- B. A proposed change to bring the resource back to the configuration
- C. An automatic update of the configuration files
- D. An error stating the state is corrupt
Show answer
Answer: B
Plan reads the real object, sees it differs from the configuration and proposes a change to bring it back. It does not ignore console edits, and it does not silently rewrite your configuration to match them.
Want more questions like this? Full Terraform 004 practice tests →
Question 2. A team moves from the local backend to a remote backend by adding a backend block. What must they do next?
- A. Run terraform init and migrate the state when asked
- B. Run terraform destroy and start again
- C. Edit the state file by hand
- D. Nothing, Terraform switches automatically at the next apply
Show answer
Answer: A
Changing the backend requires terraform init, which detects the change and offers to migrate existing state. Apply alone does not reconfigure the backend, and destroy would delete infrastructure.
Want more questions like this? Full Terraform 004 practice tests →
Question 3. An engineer needs to see which resource addresses Terraform currently tracks, without changing anything. Which command fits?
- A. terraform apply
- B. terraform destroy
- C. terraform state list
- D. terraform init
Show answer
Answer: C
terraform state list reads state and prints the tracked resource addresses without modifying infrastructure. Apply and destroy change infrastructure, and init sets up the working directory rather than listing resources.
Want more questions like this? Full Terraform 004 practice tests →
Next
Finish the technical objectives with HCP Terraform, or revisit modules.