What it is

It’s a file that serves as the source of truth for your Terraform configuration, listing all the resources being managed by it. Without it, Terraform has no context on which resources exist and therefore can’t compare what’s declared with what has been created.

Granularity

Sometimes, you would need to share the same infrastructure resources across multiple projects, teams, and so on. I’ve seen this a lot with networking resources, such as VPCs and subnets.

In cases like this, it is important to reduce the blast radius of changes and enable reusability.

Blast radius is reduced by having a granular state. The more isolated a state file is, the less damage a single apply can do.

Some general recommendations for keeping the state granular:

  • Group things by how frequently they change: stable resources should be isolated from resources that change frequently, so you don’t risk affecting their state by accident.
  • Group things by dependencies: if a resource is depended on by many other live resources (e.g. a shared VPC), it should be defined in its own state. This not only limits blast radius but also allows you to restrict write-access to the shared resource.

Reusability, on the other hand, is addressed through shared modules, which can be defined in a dedicated repository or as part of a monorepo. A module is just reusable code, it doesn’t carry state, so the same module can be instantiated many times by different configurations that do carry state.