Chapter 3 of 520% of exam

Implementing and Maintaining State

This chapter explains what Terraform state is, why it matters, how teams store it safely with remote backends and locking, and the state-related commands for importing and adjusting tracked resources. State is the heart of how Terraform knows what exists.

What state is and why it exists

State maps the resources in your configuration to the real objects that exist and records metadata and dependencies, so Terraform can compute accurate plans. Without state Terraform would not know which real resource corresponds to which configuration block. State can contain sensitive values, so it must be handled carefully.

Remote backends and locking

Teams should store state in a remote backend such as Terraform Cloud or an object store, which provides shared access, encryption, and state locking. Locking prevents two operations from writing state at the same time and corrupting it. Committing state to version control or emailing it around risks leaks and conflicts.

Managing tracked resources

terraform import brings an existing, manually created resource under Terraform management by recording it in state; you still write matching configuration. terraform state rm stops Terraform from tracking a resource without destroying the real object. These commands let you reconcile drift between reality and state.

Report