Manages Terraform state operations including importing existing resources, moving resources between states, removing resources from state, and migrating state backends...
This skill provides safe workflows for Terraform state operations and troubleshooting.
Use this skill when:
ALWAYS follow these rules when working with state:
terraform plan after state changes to verifyWorkflow:
# Step 1: Write the resource configuration in .tf file
# Step 2: Import the resource
terraform import <resource_address> <resource_id>
# Step 3: Verify with plan (should show no changes)
terraform plan
Example - Import AWS S3 Bucket:
# 1. Add to main.tf
resource "aws_s3_bucket" "existing" {
bucket = "my-existing-bucket"
}
# 2. Import
terraform import aws_s3_bucket.existing my-existing-bucket
# 3. Verify
terraform plan # Should show: No changes
Tips:
terraform show to see imported attributesWorkflow:
# Move resource to new address
terraform state mv <source> <destination>
# Verify
terraform plan # Should show: No changes
Example - Rename Resource:
# Before: aws_s3_bucket.bucket
# After: aws_s3_bucket.main
terraform state mv aws_s3_bucket.bucket aws_s3_bucket.main
Example - Move to Module:
# Move resource into module
terraform state mv aws_instance.web module.web_server.aws_instance.main
Example - Move Between Modules:
terraform state mv module.old.aws_db_instance.main module.new.aws_db_instance.main
Tips:
Workflow:
# Remove resource from state (keeps actual resource)
terraform state rm <resource_address>
# Verify resource still exists in cloud
# Update .tf files to remove resource definition
Example - Remove Resource:
# Remove from Terraform management
terraform state rm aws_s3_bucket.temp
# Resource still exists in AWS, just not managed by Terraform
Use cases:
Workflow for Local → S3:
# Step 1: Create S3 bucket and DynamoDB table for locking
# Step 2: Add backend configuration
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "project/terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "terraform-locks"
}
}
# Step 3: Initialize with migration
terraform init -migrate-state
# Step 4: Verify
terraform plan
Backend Migration Checklist:
terraform init -migrate-stateterraform planView all resources in state:
# List all resources
terraform state list
# Show specific resource details
terraform state show aws_s3_bucket.main
# Output state as JSON
terraform show -json
# If state is locked and shouldn't be
terraform force-unlock <lock-id>
# Only use if you're certain no other operation is running
# Check for drift between state and actual infrastructure
terraform plan -refresh-only
# Update state to match reality (doesn't change infrastructure)
terraform apply -refresh-only
# Terraform keeps backups automatically
ls terraform.tfstate.backup
# Restore from backup
cp terraform.tfstate.backup terraform.tfstate
# Or restore from remote backend version history (S3 versioning)
Before any state operation:
cp terraform.tfstate terraform.tfstate.backup)After any state operation:
terraform plan (should show expected changes or no changes)