TechyPilots Info
TechyPilots.INFO
Home/Tutorials/Terraform/Terraform State
Terraform Fundamentals

Terraform State

Learn how Terraform state tracks infrastructure resources, stores resource information, supports remote backends, and helps manage Infrastructure as Code.

Level: Intermediate
Topic: State Management

Terraform State Workflow

Configuration → State → Infrastructure

Read Current State

Compare Configuration

Create Execution Plan

Apply Infrastructure Changes

Update Terraform State

Introduction

How Terraform remembers your infrastructure

Terraform state is one of the core components of Terraform. It allows Terraform to maintain information about the infrastructure resources that it manages.

When you create or modify infrastructure, Terraform uses state information to understand what resources already exist and how they relate to your configuration.

Understanding Terraform state is important when working with larger infrastructure environments, teams, remote backends, and reusable Infrastructure as Code.

Core Concepts

Understanding Terraform state management

These concepts explain how Terraform tracks infrastructure and manages state across different environments and workflows.

01

Terraform State

Terraform state is a record of the infrastructure resources that Terraform manages and the information Terraform needs to understand their current state.

Key Topics

  • Resource tracking
  • Infrastructure mapping
  • Resource metadata
  • Current state
02

State Files

Terraform commonly stores infrastructure state in a state file, allowing Terraform to track resource information between operations.

Key Topics

  • terraform.tfstate
  • Resource attributes
  • State data
  • Infrastructure records
03

Remote State

Remote state stores Terraform state in a shared backend instead of keeping the state only on a local machine.

Key Topics

  • Remote backends
  • Shared infrastructure
  • Team collaboration
  • Centralized state
04

State Locking

State locking helps prevent multiple Terraform operations from modifying the same state at the same time.

Key Topics

  • Concurrent operations
  • State protection
  • Team workflows
  • Infrastructure safety

Local State

Understanding the local state file

By default, Terraform can maintain state information locally in the working directory.

The state file contains information that Terraform uses to map configuration resources to the infrastructure resources it manages.

Example Local Project

terraform-project/
│
├── main.tf
├── variables.tf
├── outputs.tf
│
└── terraform.tfstate

Simplified State Example

{
  "version": 4,

  "terraform_version": "1.x",

  "resources": [
    {
      "type": "aws_instance",
      "name": "web_server"
    }
  ]
}

State Information

What Terraform records in state

Terraform state contains information about the resources managed by the Terraform configuration.

This information can include resource identifiers, attributes, relationships, and other metadata required for Terraform to understand the managed infrastructure.

State Workflow

How Terraform uses state

Terraform uses state information throughout the infrastructure management workflow.

Terraform Configuration
        │
        ▼
terraform plan / apply
        │
        ▼
Terraform State
        │
        ▼
Cloud Infrastructure
01

Read

Terraform reads existing state information to understand the infrastructure it currently manages.

02

Compare

Terraform compares configuration and current infrastructure information to determine the required changes.

03

Plan

Terraform creates an execution plan describing the infrastructure actions it intends to perform.

04

Apply

Terraform creates, updates, or removes resources according to the approved configuration.

05

Update State

After infrastructure changes are completed, Terraform updates the state information it manages.

State Commands

Inspect and manage Terraform state

Terraform provides state commands that can help inspect resources tracked by the current state and perform specific state management operations.

State operations should be performed carefully because they can change how Terraform maps configuration resources to existing infrastructure.

Common State Commands

# List resources tracked by Terraform

terraform state list

# Show information about a resource

terraform state show aws_instance.web_server

# Move a resource in the state

terraform state mv old_resource new_resource

# Remove a resource from state

terraform state rm aws_instance.web_server

Backend Configuration Example

terraform {
  backend "s3" {
    bucket = "terraform-state-bucket"

    key = "production/terraform.tfstate"

    region = "us-east-1"
  }
}

Remote State

Store state outside the local machine

Remote state stores Terraform state using a configured backend instead of relying only on a local state file.

Remote state is useful for shared infrastructure workflows where multiple users or automation systems need access to the same infrastructure state.

State Locking

Prevent conflicting infrastructure changes

When multiple users or automation systems manage the same infrastructure, concurrent operations can create conflicts.

State locking can help protect the infrastructure workflow by preventing conflicting Terraform operations from modifying the same state at the same time.

Shared Infrastructure

Teams can work with a centralized infrastructure state instead of maintaining separate local copies.

Concurrent Operations

Locking can help prevent multiple Terraform operations from changing the same state simultaneously.

Safer Workflows

State management practices help reduce the risk of conflicting infrastructure operations.

Benefits

Why Terraform state is important

Track Infrastructure

Terraform state allows Terraform to understand which resources it manages and how those resources are configured.

Detect Changes

Terraform compares configuration, state information, and infrastructure data to determine what changes may be required.

Manage Dependencies

State information helps Terraform understand relationships between different infrastructure resources.

Support Team Collaboration

Remote state makes it easier for teams to work with shared infrastructure configurations.

Modules and State

Access information from Terraform modules

Terraform modules can expose infrastructure information through output values.

The root module can then reference those outputs when working with infrastructure created by reusable child modules.

Module Output Example

# Root module output

output "server_ip" {
  value = module.web_server.public_ip
}

Learning Roadmap

Master Terraform state management

Follow these steps to understand how Terraform uses and manages infrastructure state.

Step 1

Understand Terraform State

Learn why Terraform needs state to track the infrastructure resources it manages.

Step 2

Inspect Local State

Create infrastructure and understand how Terraform records resource information in its state.

Step 3

Use State Commands

Learn how to inspect and manage resources tracked by Terraform state.

Step 4

Understand Remote State

Learn why shared infrastructure environments often use a remote backend for state storage.

Step 5

Learn State Locking

Understand how locking can help prevent conflicting Terraform operations.

Step 6

Follow State Best Practices

Protect state files, avoid unnecessary manual changes, and use an appropriate backend for your workflow.

Best Practices

Working safely with Terraform state

Protect State Files

Treat Terraform state carefully because it can contain important infrastructure information.

Use Remote State for Teams

Use an appropriate shared backend when multiple users or automation systems manage the same infrastructure.

Avoid Manual Editing

Avoid directly editing state files unless you fully understand the consequences and have a safe recovery process.

Use State Commands Carefully

Commands that modify state can affect how Terraform tracks existing infrastructure resources.

Use State Locking

Use a backend and workflow that can help prevent conflicting concurrent infrastructure operations.

Review Infrastructure Changes

Review Terraform plans before applying changes to understand how configuration updates affect managed infrastructure.

Next Step

Manage Terraform infrastructure with confidence

Practice inspecting Terraform state, understanding resource tracking, using state commands, and learning how remote state fits into larger infrastructure workflows.