#what-is-state-and-why
Why does Terraform need state? Why not just read the provider's API?
Что отвечать
State is the map between HCL and the real provider resources. It holds resource ids, computed attributes, and metadata. Without state Terraform does not know which `aws_s3_bucket.demo` in HCL matches which real bucket, because names in HCL are addresses, not global identifiers. Reading the API every time is expensive and sometimes impossible (data lag, eventual consistency). It also would not cover the "removed from HCL, so destroy the resource" case.
Что хотят услышать
A senior should: - explain addressing: `aws_s3_bucket.demo` is an address in the graph, while the real id (say `arn:aws:s3:::my-bucket-12345`) lives in state - name what state is for: mapping HCL to real ids, caching computed attributes, and knowing before-and-after for the plan diff - say that without state every plan would be a full refresh, which makes no sense time-wise for tens of thousands of resources - mention `terraform refresh` as a forced re-read from the API, usually not needed since apply refreshes on its own
Подводные камни
- ✗ Saying state is a 'config backup.' The config is the HCL, state is the mapping
- ✗ Thinking state can be rebuilt from the API. Partly yes, through import, but computed attributes like random_id will not come back
- ✗ Not mentioning why computed attributes live in state, which leaves it unclear where the passwords come from (sensitive values in state)
Follow-up
- ? What happens if you delete the state file? Which data can you recover?
- ? How does `terraform refresh` differ from `terraform plan -refresh-only`?
- ? Why are computed attributes in state if they were never in the HCL?
Глубина в базе знаний