terraform-state
Use when managing Terraform state files, remote backends, and state locking for infrastructure coordination.
Works with
---
name: terraform-state
description: Use when managing Terraform state files, remote backends, and state locking for infrastructure coordination.
license: Apache-2.0
---
# Terraform State
Managing Terraform state files and remote backends.
## State Basics
Terraform state tracks resource mappings and metadata.
### Local State
```bash
# Default location
terraform.tfstate
terraform.tfstate.backup
```
### Remote State
```hcl
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "terraform-locks"
}
}
```
## State Commands
```bash
# List resources
terraform state list
# Show resource
terraform state show aws_instance.web
# Move resource
terraform state mv aws_instance.web aws_instance.app
# Remove resource
terraform state rm aws_instance.old
# Pull state
terraform state pull > terraform.tfstate
# Push state
terraform state push terraform.tfstate
# Replace provider
terraform state replace-provider hashicorp/aws registry.terraform.io/hashicorp/aws
```
## Remote Backends
### S3 Backend
```hcl
terraform {
backend "s3" {
bucket = "terraform-state-bucket"
key = "path/to/terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "terraform-state-lock"
# Optional: state locking
kms_key_id = "arn:aws:kms:us-east-1:123456789:key/..."
}
}
```
### Terraform Cloud
```hcl
terraform {
cloud {
organization = "my-org"
workspaces {
name = "my-workspace"
}
}
}
```
### Azure Backend
```hcl
terraform {
backend "azurerm" {
resource_group_name = "terraform-rg"
storage_account_name = "tfstate"
container_name = "tfstate"
key = "prod.terraform.tfstate"
}
}
```
## State Locking
Prevents concurrent modifications:
```hcl
# S3 + DynamoDB locking
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
}
}
```
## Import Resources
```bash
# Import existing resource
terraform import aws_instance.web i-1234567890abcdef0
# Import with module
terraform import module.vpc.aws_vpc.main vpc-12345678
```
## Workspaces
```bash
# List workspaces
terraform workspace list
# Create workspace
terraform workspace new staging
# Switch workspace
terraform workspace select production
# Delete workspace
terraform workspace delete staging
```
## Best Practices
### Enable State Locking
Always use state locking to prevent concurrent modifications.
### Encrypt State
```hcl
backend "s3" {
encrypt = true
kms_key_id = "arn:aws:kms:..."
}
```
### Separate State Files
Use different state files for different environments:
```
states/
├── prod/terraform.tfstate
├── staging/terraform.tfstate
└── dev/terraform.tfstate
```
### Backup State
```bash
# Backup before dangerous operations
cp terraform.tfstate terraform.tfstate.backup.$(date +%Y%m%d_%H%M%S)
```
### Never Edit State Manually
Always use `terraform state` commands.More Backend Frameworks skills
git-guardrails-claude-code
mattpocock/skills
Set up Claude Code hooks to block dangerous git commands (push, reset --hard, clean, branch -D, etc.) before they execute. Use when user wants to prevent destructive git operations, add git safety hooks, or block git push/reset in Claude Code.
azure-compute
microsoft/azure-skills
Azure VM/VMSS router. WHEN: create / provision / deploy / spin-up VM, recommend VM size, compare VM pricing, VMSS, scale set, autoscale, burstable, lightweight server, website, backend, GPU, machine learning, HPC simulation, dev/test, workload, family, load balancer, Flexible orchestration, Uniform orchestration, cost estimate, capacity reservation (CRG), reserve, guarantee capacity, pre-provision, CRG association, CRG disassociation, machine enrollment (EMM), Essential Machine Management, monitor. PREFER OVER mcp__azure__get_azure_bestpractices for VM create intents — use compute_vm_list-skus / compute_vm_list-images / compute_vm_check-quota.
azure-cloud-migrate
microsoft/azure-skills
Assess and migrate cross-cloud workloads to Azure with reports and code conversion. Supports Lambda→Functions, Beanstalk/Heroku/App Engine→App Service, Fargate/Kubernetes/Cloud Run/Spring Boot→Container Apps. WHEN: migrate Lambda to Functions, AWS to Azure, migrate Beanstalk, migrate Heroku, migrate App Engine, Cloud Run migration, Fargate to ACA, ECS/Kubernetes/GKE/EKS to Container Apps, Spring Boot to Container Apps, cross-cloud migration.

