004 · Domain 2: Terraform fundamentals · 13% of the exam
Task 2.4: Explain how Terraform uses and manages state
Why state exists: mapping configuration to real objects, metadata such as dependencies, performance on large estates, and the default terraform.tfstate file, which holds every value in plain text and is never edited by hand.
Study it
What state is for: bindings, metadata, caching, and why it is sensitive
Lesson coming
Sample questions
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
An engineer suggests that Terraform could skip state entirely and find its resources by tags on every run. Why does Terraform keep state instead?
AIt needs a binding for each resource instance, and many resources cannot be tagged
BTags are read-only for providers, so Terraform cannot set them on the resources it creates
CState is only a performance cache, and Terraform could work without it at the cost of speed
DState stores the configuration, so Terraform can run without the .tf files
Show the answer and why
AIt needs a binding for each resource instance, and many resources cannot be tagged
Correct
State maps configuration to real objects, such as an instance ID; tags were tried early on and failed because not all resources support them.
BTags are read-only for providers, so Terraform cannot set them on the resources it creates
Incorrect
Providers can usually set tags; the problem is that many resource types have none.
CState is only a performance cache, and Terraform could work without it at the cost of speed
Incorrect
Caching is the most optional part of state. The mapping and metadata make state a requirement.
DState stores the configuration, so Terraform can run without the .tf files
Incorrect
State does not replace configuration; it records what the configuration manages.
State is required: it is the database of bindings between configuration and the real world, plus metadata that configuration alone cannot carry.
A configuration has an instance inside a subnet. Both blocks are deleted from the configuration in the same change. The configuration no longer says that the instance depends on the subnet, yet Terraform destroys the instance first. How does it know the order?
AState keeps each resource's most recent dependencies, so the order is still known
BTerraform has a built-in ranking of resource types that puts instances before subnets
CTerraform reads the previous version of the configuration from the .terraform directory
DThe provider deletes resources in the order the API returns them
Show the answer and why
AState keeps each resource's most recent dependencies, so the order is still known
Correct
Terraform retains a copy of the latest set of dependencies in state so it can order destruction when items are removed from configuration.
BTerraform has a built-in ranking of resource types that puts instances before subnets
Incorrect
Terraform does not rank resource types; that approach was rejected as too complex across every provider.
CTerraform reads the previous version of the configuration from the .terraform directory
Incorrect
The .terraform directory holds providers, modules and backend settings, not old configuration.
DThe provider deletes resources in the order the API returns them
Incorrect
Terraform, not the API listing, decides the order of operations.
Besides mappings, state stores metadata such as dependencies, exactly for operations on resources that have left the configuration.
A reporting tool needs the values of a configuration's root outputs and the attributes of its resources after each apply in a pipeline. What is the supported integration point?
AParsing terraform.tfstate directly, which holds every value the tool needs
BReading the .terraform.lock.hcl file after apply
CScraping the colored text that terraform apply prints
DThe -json output of terraform output and terraform show
Show the answer and why
AParsing terraform.tfstate directly, which holds every value the tool needs
Incorrect
terraform.tfstate is not the documented integration point for other software; the JSON output of Terraform's own commands is.
BReading the .terraform.lock.hcl file after apply
Incorrect
The lock file lists provider selections, not outputs or attributes.
CScraping the colored text that terraform apply prints
Incorrect
Human-oriented output is not a stable interface; the JSON outputs are.
DThe -json output of terraform output and terraform show
Correct
terraform output -json and terraform show -json produce JSON intended for external software, so tools do not parse state directly.
Run terraform output -json and terraform show -json right after a successful apply and keep the result as an artifact of the run, so other software never needs to run Terraform itself.
A day after an apply, an engineer runs terraform output in the same working directory and gets the load balancer's address immediately, without any provider being contacted. Where does the value come from?
AThe root module's output values recorded in the state
BA new refresh of the load balancer, run quietly in the background by the output command
CThe saved plan file, which terraform apply leaves in the working directory
DThe .terraform directory, where init caches the configuration's outputs
Show the answer and why
AThe root module's output values recorded in the state
Correct
terraform output reads output values from the state file, which stores root module outputs after each apply.
BA new refresh of the load balancer, run quietly in the background by the output command
Incorrect
output does not refresh resources; it reads what the latest state snapshot holds.
CThe saved plan file, which terraform apply leaves in the working directory
Incorrect
apply without -out writes no plan file, and output does not read plans.
DThe .terraform directory, where init caches the configuration's outputs
Incorrect
init keeps what it installs and sets up there, never output values.
Root outputs are part of state, so terraform output reads them without planning.