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 workspace does not exist errors. Create missing workspaces, auto-create in CI/CD, handle backend workspace config, and migrate between workspaces.
# Create the workspace
terraform workspace new production
# Or auto-create if it doesn't exist
terraform workspace select production 2>/dev/null || terraform workspace new productionError: Currently selected workspace "production" does not exist.
Available workspaces:
default
dev
stagingOr after changing backends:
Error: Workspace "production" doesn't exist.
You can create this workspace with the "new" subcommand
or include the "-or-create" flag with the "select" subcommand..terraform/ is empty, workspace selection saved thereterraform workspace list # See what exists
terraform workspace new production
terraform workspace select production# Idempotent — works whether workspace exists or not
terraform workspace select $ENVIRONMENT 2>/dev/null || terraform workspace new $ENVIRONMENTOr with Terraform 1.8+:
terraform workspace select -or-create $ENVIRONMENTFull CI/CD script:
# GitLab CI
.deploy:
script:
- terraform init
- terraform workspace select -or-create ${CI_ENVIRONMENT_NAME}
- terraform plan -out=plan.tfplan
- terraform apply plan.tfplan
deploy-dev:
extends: .deploy
environment:
name: dev
deploy-prod:
extends: .deploy
environment:
name: productionWhen changing from local to remote backend, workspaces don't automatically migrate:
# Migrate state to new backend
terraform init -migrate-state
# Then create workspaces in the new backend
terraform workspace new dev
terraform workspace new staging
terraform workspace new productionTerraform Cloud workspace names may differ from local:
terraform {
cloud {
organization = "myorg"
workspaces {
# Option 1: exact name
name = "myapp-production"
# Option 2: tag-based (select at init)
tags = ["myapp"]
}
}
}With tags, select at init:
terraform init
# Terraform prompts: Which workspace? → select "myapp-production"terraform workspace list # Show all workspaces (* = current)
terraform workspace show # Show current workspace name
terraform workspace select dev # Switch to devlocals {
config = {
dev = { instance_type = "t3.micro", count = 1 }
staging = { instance_type = "t3.small", count = 2 }
production = { instance_type = "t3.large", count = 3 }
}
env = local.config[terraform.workspace]
}
resource "aws_instance" "app" {
count = local.env.count
instance_type = local.env.instance_type
tags = {
Environment = terraform.workspace
}
}defaultlocals {
valid_workspaces = ["dev", "staging", "production"]
}
resource "null_resource" "workspace_check" {
count = contains(local.valid_workspaces, terraform.workspace) ? 0 : 1
provisioner "local-exec" {
command = "echo 'ERROR: Invalid workspace ${terraform.workspace}' && exit 1"
}
}Create the workspace with terraform workspace new NAME or use terraform workspace select -or-create NAME in CI/CD. After backend migrations, workspaces need to be recreated in the new backend. In Terraform Cloud, ensure workspace names match your configuration.
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.