Cheat Sheet 7: Maintaining Infrastructure
Import: binds an existing object to an address in state. Never creates or changes infrastructure.
CLI terraform import ADDR ID |
import { to = ADDR id = "ID" } |
|
|---|---|---|
| Preview | ✗, immediate state write | ✓, "N to import" in the plan |
| Resource block | Must exist first | Can be generated: plan -generate-config-out=FILE (file must not exist; review it) |
| Bulk | One per command | Multiple blocks, or for_each (1.7+) |
| VCS and HCP Terraform | ✗ | ✓ |
| Provider alias | — | provider = aws.west |
- The ID format is provider-specific. Check the resource docs' "Import" section. Not every resource supports import.
- Post-import changes in the plan mean the configuration is wrong. Fix it until the plan is clean.
- Remove
importblocks after applying, by convention. - 1.12 adds
identityas an alternative toid, for providers that support resource identity.
Inspect (read-only)
state list [prefix] [-id=ID]·state show ADDR(one resource, sensitive values redacted)show(whole state) ·show tfplan·show -jsonoutput(all outputs; sensitive ones shown as<sensitive>) ·output NAME/output -json/-raw NAME(sensitive values in plaintext)graph(DOT;| dot -Tsvg) ·providers·console(holds the lock) ·state pull- Quote addresses with brackets:
'res.x["key"]'.
State-modifying: state mv · state rm · state push · state replace-provider · import
Logging
TF_LOG=TRACE>DEBUG>INFO>WARN>ERROR, orJSON(TRACE as JSON)TF_LOG_PATH= file destination. Requires a level variable to be set as well.TF_LOG_CORE/TF_LOG_PROVIDER= scope logging to core or providers- Logs go to stderr by default. A core panic writes
crash.log. - Logs may contain secrets, so scrub them before sharing. Unset the variables afterwards.
Module 7 self-check
Module 7 self-check.
- Coverage: 7a (lesson 7.1, lab Parts A–C, Q1–Q5), 7b (lesson 7.2, lab Part D, Q6–Q8), and 7c (lesson 7.3, lab Part E, Q9–Q10).
- HCL:
importblocks withto,id, andfor_eachmatch 1.7+ syntax.docker_networksupports import by network ID. Thefor_eachimport ID comes from a variable, which is known at plan time. - Flagged uncertainty:
- The
identityargument forimportblocks is described as new in 1.12 with limited provider support. Verify it in the 1.12 import block documentation before relying on it. - Generated-config cleanup is framed as iterative because the exact generated attributes depend on the provider version.
- The post-import "No changes" result for
docker_networkassumes provider defaults match. The lab tells learners what to do if they don't.
- The
- Answers: rechecked against documented import, inspection, and logging behavior.