6 State Management

 

This chapter covers

  • Reasons that State exist
  • Considerations when Managing State
  • Storing State in Centralized Backends
  • Understanding how State is Structured
  • Fixing common State errors
  • Cross Project State Access
  • Resources that exist only in State

When using Terraform the concept of state comes up quite often. We’ve talked about it a little bit so far, defining it back in Chapter 1 as “a collection of metadata about the resources that Terraform manages”. What is “state” though, and why do we need it?

One of the easiest ways to talk about state is to talk about what it looks like to have a system without state (also referred to as a stateless system). When you’re working with programming languages and protocols a stateless system is one that has no memory of past runs. Every interaction is a standalone interaction that does not depend on the interactions before it. If you’ve ever worked with web based APIs you may be familiar with the HTTP based REST Protocol, which is a stateless system: every request to the API has to contain everything it needs for that request.

Systems that have state are a bit different. Stateful systems have a memory of what has happened before that they used to drive their current actions. This means that they have to keep track of the current state of the system (hence the name). Terraform is an example of a stateful system: it keeps a record of all the resources under its control and refreshes that record as part of every plan.

6.1 Purpose of State

6.1.1 Real World Linkage

6.1.2 Reduced Complexity

6.1.3 Performance

6.1.4 State Only Resources

6.2 Important Considerations

6.2.1 Reliability

6.2.2 Security

6.2.3 Availability

6.3 Dissecting State

6.3.1 State as JSON

6.3.2 State Versions

6.3.3 Lineage and Serial

6.3.4 Resources, Outputs, and Checks

6.4 Storing State

6.4.1 Possible Backends

6.4.2 Configuring the Backend Itself

6.4.3 Backend Block

6.4.4 Alternative Authentication Methods

6.4.5 Cloud Block

6.4.6 Migrating Backends

6.4.7 Workspaces

6.4.8 Upgrading Backends

6.5 Manipulating State

6.5.1 Backup and Restore

6.5.2 Code Driven Changes

6.5.3 CLI Driven Changes

6.5.4 Manually Editing

6.6 State Drift

6.6.1 Accidental Manual Changes

6.6.2 Intentional Manual Changes

6.6.3 Conflicting Automated Changes

6.6.4 Terraform Errors

6.7 Accessing State Across Projects