Diagnoses and resolves Terraform infrastructure-as-code issues including state lock conflicts, provider authentication failures, resource dependency cycles, state drift, import failures, module version conflicts, and plan/apply errors. Provides TF_LOG debugging, terraform console usage, state manipulation commands, and CI/CD best practices.
Ships in debug-and-refactor. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Debug Terraform configurations and state issues using a systematic four-phase approach with provider-specific considerations.
Terraform errors fall into four categories:
# Check Terraform and provider versions
terraform version
# Validate configuration syntax
terraform validate
# Review current state
terraform state list
terraform state show <resource_address>
# Check for state drift
terraform plan -refresh-only
# Set log level (TRACE, DEBUG, INFO, WARN, ERROR)
export TF_LOG=DEBUG
# Write logs to file (recommended for large outputs)
export TF_LOG_PATH="./terraform-debug.log"
# For provider-specific debugging
export TF_LOG_PROVIDER=DEBUG
# Run command with logging
terraform plan
Log Levels:
TRACE - Maximum verbosity, every action loggedDEBUG - Detailed debugging for complex issuesINFO - General informative messagesWARN - Non-critical warningsERROR - Critical errors only# Target specific resource
terraform plan -target=aws_instance.example
# Check specific module
terraform plan -target=module.networking
# Validate single file
terraform validate -json
Error: Error acquiring the state lock
Diagnosis:
# Check if another process is running
ps aux | grep terraform
# View lock info (for S3 backend)
aws dynamodb get-item --table-name terraform-locks --key '{"LockID":{"S":"your-state-path"}}'
Solution:
# Force unlock (use with caution!)
terraform force-unlock <LOCK_ID>
# For S3/DynamoDB backend
aws dynamodb delete-item --table-name terraform-locks --key '{"LockID":{"S":"your-state-path"}}'
Error: error configuring Terraform AWS Provider: no valid credential sources found
Diagnosis:
# Check AWS credentials
aws sts get-caller-identity
# Verify environment variables
env | grep AWS
# Check credential file
cat ~/.aws/credentials
Solution:
# Explicit provider configuration
provider "aws" {
region = "us-east-1"
profile = "my-profile"
# Or use assume_role
assume_role {
role_arn = "arn:aws:iam::123456789012:role/TerraformRole"
}
}
Error: Cycle: aws_security_group.a, aws_security_group.b
Diagnosis:
# Generate dependency graph
terraform graph | dot -Tpng > graph.png
# Or view in text format
terraform graph
Solution:
# Break cycle with explicit dependencies
resource "aws_security_group" "a" {
name = "sg-a"
# Remove circular reference
}
resource "aws_security_group_rule" "a_to_b" {
security_group_id = aws_security_group.a.id
source_security_group_id = aws_security_group.b.id
# ...
}
Note: Objects have changed outside of Terraform
Diagnosis:
# Detect drift
terraform plan -refresh-only
# Show current state
terraform show
# Pull remote state for inspection
terraform state pull > state.json
Solution:
# Accept external changes into state
terraform apply -refresh-only
# Or reimport drifted resource
terraform import aws_instance.example i-1234567890abcdef0
# Or replace resource to match config
terraform apply -replace=aws_instance.example
Error: Cannot import non-existent remote object
Diagnosis:
# Verify resource exists
aws ec2 describe-instances --instance-ids i-1234567890abcdef0
# Check import syntax for resource type
terraform providers schema -json | jq '.provider_schemas["registry.terraform.io/hashicorp/aws"].resource_schemas["aws_instance"]'
Solution:
# Correct import command
terraform import aws_instance.example i-1234567890abcdef0
# For modules
terraform import module.ec2.aws_instance.example i-1234567890abcdef0
# Generate import blocks (Terraform 1.5+)
terraform plan -generate-config-out=generated.tf
Error: Module version requirements have changed
Diagnosis:
# Check current module versions
terraform providers lock -platform=linux_amd64
# View dependency tree
cat .terraform.lock.hcl
Solution:
# Upgrade modules
terraform init -upgrade
# Clear cache and reinitialize
rm -rf .terraform
terraform init
# Pin specific version
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.0"
}
Error: Error creating S3 bucket: BucketAlreadyExists
Solution:
# Import existing resource
terraform import aws_s3_bucket.example my-bucket-name
# Or use data source to reference
data "aws_s3_bucket" "existing" {
bucket = "my-bucket-name"
}
# Open Terraform console for expression testing
terraform console
# Test expressions
> var.instance_type
> aws_instance.example.id
> length(var.subnets)
> jsonencode(local.tags)
# Remove resource from state (doesn't destroy)
terraform state rm aws_instance.orphaned
# Move resource in state
terraform state mv aws_instance.old aws_instance.new
# Move to different state file
terraform state mv -state-out=other.tfstate aws_instance.example aws_instance.example
# Replace provider in state
terraform state replace-provider hashicorp/aws registry.terraform.io/hashicorp/aws
# Preview changes
terraform plan -out=tfplan
# Apply with plan file (recommended)
terraform apply tfplan
# Apply specific resource only
terraform apply -target=aws_instance.example
# Destroy and recreate
terraform apply -replace=aws_instance.problematic
# Validate syntax
terraform validate
# Format check
terraform fmt -check -recursive
# Use tflint for best practices
tflint --init
tflint
# Security scanning with checkov
checkov -d .
# Cost estimation
infracost breakdown --path=.
# CI-friendly commands
terraform init -input=false
terraform plan -input=false -no-color -out=tfplan
terraform apply -input=false -no-color tfplan
# Lock provider versions
terraform providers lock -platform=linux_amd64 -platform=darwin_amd64
# Pin Terraform version
terraform {
required_version = ">= 1.5.0, < 2.0.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
# Use variables with validation
variable "environment" {
type = string
validation {
condition = contains(["dev", "staging", "prod"], var.environment)
error_message = "Environment must be dev, staging, or prod."
}
}
# Add lifecycle rules for critical resources
resource "aws_instance" "critical" {
# ...
lifecycle {
prevent_destroy = true
create_before_destroy = true
}
}
| Command | Purpose |
|---|---|
terraform validate | Check syntax and configuration |
terraform plan | Preview changes |
terraform plan -refresh-only | Detect state drift |
terraform state list | List all resources in state |
terraform state show <addr> | Show resource details |
terraform state pull | Download remote state |
terraform state rm <addr> | Remove from state |
terraform import <addr> <id> | Import existing resource |
terraform force-unlock <id> | Release state lock |
terraform console | Interactive expression testing |
terraform graph | Generate dependency graph |
terraform providers | Show required providers |
terraform output | Show output values |
terraform taint <addr> | Mark for recreation (deprecated) |
terraform apply -replace=<addr> | Force resource replacement |
| Variable | Purpose |
|---|---|
TF_LOG | Set log level (TRACE/DEBUG/INFO/WARN/ERROR) |
TF_LOG_PATH | Write logs to file |
TF_LOG_PROVIDER | Provider-specific logging |
TF_INPUT | Disable interactive prompts (0/false) |
TF_VAR_name | Set variable value |
TF_CLI_ARGS | Additional CLI arguments |
TF_DATA_DIR | Custom data directory (default: .terraform) |
TF_WORKSPACE | Set workspace |
TF_IN_AUTOMATION | Adjust output for automation |
TF_PLUGIN_CACHE_DIR | Share providers across projects |
terraform validate for syntax issuesTF_LOG=DEBUGterraform state listterraform graph.terraform.lock.hcl for version conflictsterraform console.tfvars with secrets to version controlTF_LOG in production to prevent sensitive data exposureWhat SKILL.md sets; muted values are Claude Code's defaults
/debug-and-refactor:debug:terraformfrom debug-and-refactor
npx claudepluginhub snakeo/claude-debug-and-refactor-skills-plugin --plugin debug-and-refactor45 skills in this plugin
/debug:angularDebug Angular applications systematically with expert-level diagnostic techniques. This skill provides comprehensive guidance for troubleshooting dependency injection errors, change detection issues (NG0100), RxJS subscription leaks, lazy loading failures, zone.js problems, and common Angular runtime errors. Includes structured four-phase debugging methodology, Angular DevTools usage, console debugging utilities (ng.probe), and performance profiling strategies for modern Angular applications./debug:djangoDebug Django web applications with systematic diagnostic approaches. This skill covers troubleshooting Django-specific errors including TemplateDoesNotExist, ImproperlyConfigured, IntegrityError, migration conflicts, CSRF failures, N+1 query problems, and circular imports. Includes Django Debug Toolbar setup, ORM query logging, pdb/ipdb usage, shell_plus debugging, and comprehensive logging configuration. Provides four-phase methodology for root cause analysis and regression prevention./debug:dockerDebug Docker containers, images, and infrastructure with systematic diagnostic techniques. This skill provides comprehensive guidance for troubleshooting container exit codes, OOM kills, image build failures, networking issues, volume mount problems, and permission errors. Covers four-phase debugging methodology from quick assessment to deep analysis, essential Docker commands, debug container techniques for minimal images, and platform-specific troubleshooting for Windows, Mac, and Linux.Other plugins' skills for the same job