Why does Terraform use state files in its design?

We’ve started using Terraform at my organization and it’s going well.

I was curious as to why Terraform requires the generation and storage of state files by design. In theory, it should be possible for it to generate the state files to internal memory, and use that for the comparison against the live resources during plans or applys.

I’m interested in the design rationale here & looking to learn. Thanks!

At the very least you need a way to map the resources mentioned in your code to the actual resources (e.g. in your cloud provider)

Hi @steven-hyland,

You can read more about the ways Terraform uses state snapshots in Purpose of Terraform State.

That doc page is quite old now, and so the details it discusses might not be 100% accurate today, but the high-level reasons and benefits there remain true.

You can think of it as Terraform’s memory of what you have done and what it is managing on your behalf. Otherwise Terraform could just tramp all over anything else that is being done outside of its state.
For example in my job we are predominantly Terraform but occasionally we pick up some CloudFormation configuration from AWS provided solutions (although I use Terraform to manage that CloudFormation!). So CF knows what it is managing through its use of stacks. In this case Terraform manages the stack definition and lifecycle but not the resources defined inside the stack itself.