terraform state

Core Idea

State is Terraform’s snapshot of your cloud resources at a point in time: it maps config to real infrastructure, and the CLI commands are how you read and work with it.

  • What state is: the recorded condition of resources at a specific time.
  • How Terraform preserves state.
  • The Terraform CLI commands that operate on it.

What is State?

**A particularĀ conditionĀ of cloud resources at a specific time.**

eg. We expect there to be a Virtual Machine running CentOS on AWS with a compute type of t2.micro.

How does Terraform preserve state?

When you provision infrastructure via Terraform it will create a state file namedĀ terraform.tfstate.
ThisĀ state file is a JSON data structureĀ with a one-to-one mapping fromĀ resource instancesĀ toĀ remote objects

Configuration files:

  • main.tf
  • variables.tf
  • terraform.tfvars

Terraform apply command
Terraform API
Remote objects
State file
terraform.tfstate


Terraform CLI commands

  • terraform stateĀ listĀ List resources in the state
  • terraform stateĀ mvĀ Move an item in the state
  • terraform stateĀ pullĀ Pull current remote state and output to stdout
  • terraform stateĀ pushĀ Update remote state from a local state
  • terraform stateĀ replace-providerĀ Replace provider in the state
  • terraform stateĀ rmĀ Remove instances from the state
  • terraform stateĀ showĀ Show a resource in the state