Terraform Workspaces and State Cheat Sheet

Manage Terraform CLI workspaces, local and remote state, imports, and state surgery commands.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Workspaces
List workspaces
terraform workspace list

# Show available Terraform CLI workspaces for the current configuration.

Show current workspace
terraform workspace show

# Print the current Terraform CLI workspace name.

Create workspace
terraform workspace new dev

# Create and switch to a new workspace.

Select workspace
terraform workspace select prod

# Switch to an existing workspace.

Delete workspace
terraform workspace delete dev

# Delete a Terraform CLI workspace.

## Import, Move, Remove
Import existing object
terraform import aws_s3_bucket.logs my-logs-bucket

# Associate an existing infrastructure object with a resource address in state.

Import with config generation
terraform import -generate-config-out=generated.tf aws_s3_bucket.logs my-logs-bucket

# Generate configuration while importing supported resources.

Move state address
terraform state mv aws_instance.old aws_instance.new

# Rename or move a resource address in state.

Remove resource from state
terraform state rm aws_instance.web

# Stop tracking a resource without destroying the remote object.

Replace provider source in state
terraform state replace-provider hashicorp/aws registry.acme.corp/acme/aws

# Update provider source addresses recorded in state.

## State File Operations
Pull raw state JSON
terraform state pull > terraform.tfstate

# Download the latest state and write it to a local file.

Push raw state JSON
terraform state push terraform.tfstate

# Upload a local state file into the configured backend.

List module resources
terraform state list module.network

# List resources beneath a specific module path.

## Providers and Backend Migration
Migrate state during init
terraform init -migrate-state

# Move state to a newly configured backend during re-init.

Force-copy backend state
terraform init -force-copy

# Automatically copy existing state to a new backend without prompting.

Readonly provider lock file
terraform init -lockfile=readonly

# Use providers from the dependency lock file without updating it.

## Remote State Patterns
Plan with alternate state path
terraform plan -state=terraform.tfstate

# Use a non-default local state path for commands that support it.

Ignore files for remote runs
.terraformignore
.git/
.terraform/
node_modules/
*.pem

# Exclude paths from uploaded configuration when using remote CLI-driven runs.

Remote backend snippet
terraform {
  backend "remote" {
    organization = "acme"
    workspaces {
      name = "networking-prod"
    }
  }
}

# Example backend configuration for HCP Terraform remote state and runs.