Fix Terraform Error: CloudWatch Log Group Already Exists
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
DevOps
Fix terraform state show no instance found errors. Correct resource address format for count, for_each, modules, and data sources. List resources
# List all resources in state
terraform state list
# Then use the exact address shown
terraform state show 'aws_instance.web[0]'No instance found for the given address!
This command requires that the address references one specific instance.Or:
Error: No matching resource found.# List everything
terraform state list
# Filter by resource type
terraform state list | grep aws_instance
# Filter by module
terraform state list | grep "module.vpc"Output:
aws_instance.web[0]
aws_instance.web[1]
module.vpc.aws_vpc.main
module.vpc.aws_subnet.public[0]
module.vpc.aws_subnet.public[1]
module.vpc.aws_subnet.private[0]Use the exact address from the list.
terraform state show aws_instance.web# Must include the index in quotes
terraform state show 'aws_instance.web[0]'
terraform state show 'aws_instance.web[1]'
# Without quotes, the shell interprets brackets
terraform state show aws_instance.web[0] # ❌ Shell glob
terraform state show 'aws_instance.web[0]' # ✅ Quoted# String keys in quotes
terraform state show 'aws_instance.web["primary"]'
terraform state show 'aws_instance.web["secondary"]'
# Escape for shell
terraform state show 'aws_instance.web["us-east-1a"]'terraform state show 'module.vpc.aws_vpc.main'
terraform state show 'module.vpc.aws_subnet.public[0]'
terraform state show 'module.database["prod"].aws_db_instance.main'terraform state show 'data.aws_ami.ubuntu'
terraform state show 'data.aws_availability_zones.available'# Show current workspace
terraform workspace show
# List all workspaces
terraform workspace list
# Switch if needed
terraform workspace select production
terraform state listIf you renamed a resource in code:
# Old: resource "aws_instance" "web" { ... }
# New: resource "aws_instance" "app" { ... }The old name still exists in state. Move it:
terraform state mv aws_instance.web aws_instance.appFor moved blocks (Terraform 1.1+):
moved {
from = aws_instance.web
to = aws_instance.app
}Resource might have been deleted outside Terraform:
# Refresh state to match reality
terraform refresh
# Or plan with refresh
terraform plan -refresh-only| Command | Use Case |
|---|---|
terraform state list | List all resources |
terraform state show ADDRESS | Show resource details |
terraform state mv OLD NEW | Rename/move resource |
terraform state rm ADDRESS | Remove from state (keep infra) |
terraform state pull | Download remote state as JSON |
terraform state push | Upload local state to remote |
Always run terraform state list first and use the exact address shown. Quote addresses with brackets ('aws_instance.web[0]') to prevent shell interpretation. Check that you're in the right workspace and use terraform state mv when resources are renamed.
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
Fix terraform import errors when a resource already exists in state. Covers state rm, state show, reimport workflow, import blocks
Fix terraform too many command line arguments errors. Correct -var syntax, quote values with spaces, and learn proper Terraform CLI argument format for plan
Fix terraform invalid escape sequence errors. Double backslashes for Windows paths, use heredocs for regex, and learn all valid HCL escape sequences.