0
0
Terraformcloud~7 mins

State file performance at scale in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
When managing many resources with Terraform, the state file can grow large and slow down operations. This concept helps keep Terraform fast and reliable by organizing state efficiently.
When your Terraform project manages hundreds or thousands of resources.
When Terraform commands like plan or apply start taking too long to complete.
When multiple teams work on different parts of the infrastructure using the same Terraform setup.
When you want to avoid conflicts and improve collaboration by splitting state files.
When you need to store state files securely and access them quickly.
Config File - backend.tf
backend.tf
terraform {
  backend "s3" {
    bucket         = "example-terraform-state"
    key            = "prod/networking/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "example-terraform-lock"
    encrypt        = true
  }
}

# Example of splitting state using workspaces or separate state files
# This file configures the backend to store state in S3 with locking via DynamoDB

This configuration sets up a remote backend using AWS S3 to store the Terraform state file securely and reliably. The bucket is where the state file lives. The key organizes the state file path to allow splitting state by environment or module. The dynamodb_table enables locking to prevent concurrent changes that could corrupt the state. Encryption keeps the state file safe.

Splitting state files by using different keys or workspaces helps keep each state smaller and faster to load.

Commands
Initializes Terraform and configures the backend to use the remote S3 bucket for state storage. This step sets up the environment to manage state at scale.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Successfully configured the backend "s3"! Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes.
Creates a new workspace named 'prod-networking' to isolate state for this part of the infrastructure. Workspaces help split state logically to improve performance and collaboration.
Terminal
terraform workspace new prod-networking
Expected OutputExpected
Created and switched to workspace "prod-networking". You are now using workspace "prod-networking".
Generates an execution plan using the current workspace and remote state. This command shows what changes Terraform will make without applying them.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_vpc.main will be created + resource "aws_vpc" "main" { + cidr_block = "10.0.0.0/16" + tags = { + "Name" = "prod-vpc" } } Plan: 1 to add, 0 to change, 0 to destroy.
Applies the planned changes automatically without asking for confirmation. This updates the infrastructure and the remote state file.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_vpc.main: Creating... aws_vpc.main: Creation complete after 10s [id=vpc-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skips manual approval to apply changes immediately
Lists all resources tracked in the current workspace's state file. This helps verify what Terraform manages and confirms state is loaded correctly.
Terminal
terraform state list
Expected OutputExpected
aws_vpc.main
Key Concept

If you remember nothing else from this pattern, remember: splitting and storing Terraform state remotely keeps your infrastructure management fast, safe, and scalable.

Common Mistakes
Keeping all resources in a single large state file without splitting.
Large state files slow down Terraform commands and increase risk of conflicts.
Split state by environment or module using workspaces or separate state files with different backend keys.
Not enabling state locking when using remote backends.
Without locking, concurrent Terraform runs can corrupt the state file.
Use a locking mechanism like DynamoDB table with S3 backend to prevent simultaneous changes.
Storing state files locally on developer machines.
Local state files are hard to share, back up, and secure, causing collaboration issues.
Use remote backends like S3 or Terraform Cloud to centralize and protect state files.
Summary
Configure a remote backend like AWS S3 with locking to store Terraform state securely.
Use Terraform workspaces or separate state files to split state and improve performance.
Run terraform init to set up the backend, then terraform plan and apply to manage infrastructure safely.
Use terraform state list to verify resources tracked in the current state.