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 backend initialization required errors after backend changes. Covers terraform init, -reconfigure, -migrate-state
# Backend config changed — reinitialize
terraform init -reconfigure
# Backend config changed AND you want to keep existing state
terraform init -migrate-stateError: Backend initialization required, please run "terraform init".
Reason: Initial configuration of the requested backend "s3"
The "backend" is the interface that Terraform uses to store state,
perform operations, etc. If this message is shown, no Terraform
operations should be performed until it is resolved.Or:
Error: Backend configuration changed
A change in the backend configuration has been detected, which may
require migrating existing state..terraform/ directory.terraform/ deleted — cleaned cache, CI/CD fresh workspace-backend-config flags differ from last initterraform initThis is enough for fresh clones and CI/CD runners.
Use when you changed the backend and don't need to migrate state:
terraform init -reconfigureThis resets the backend without migrating. Existing remote state is untouched; only the local .terraform/ config is updated.
Use when moving from one backend to another:
terraform init -migrate-stateTerraform prompts:
Do you want to copy existing state to the new backend?
Pre-existing state was found while migrating the previous "local" backend
to the newly configured "s3" backend.
Enter "yes" to copy and "no" to start with an empty state.Local → S3:
# Before: no backend block (uses local)
# After:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}terraform init -migrate-state
# Answer "yes" to copy stateS3 → Terraform Cloud:
terraform {
cloud {
organization = "myorg"
workspaces { name = "myapp-prod" }
}
}terraform init -migrate-stateIf you use -backend-config flags, they must be consistent:
# These must match every time
terraform init \
-backend-config="bucket=my-state-bucket" \
-backend-config="key=prod/terraform.tfstate" \
-backend-config="region=us-east-1"Or use a backend config file:
# backend-prod.hcl
bucket = "my-state-bucket"
key = "prod/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"terraform init -backend-config=backend-prod.hcl# Nuclear option — clean everything and reinitialize
rm -rf .terraform .terraform.lock.hcl
terraform init# GitLab CI — always init with backend config
.terraform-init:
before_script:
- terraform init -backend-config=backends/${CI_ENVIRONMENT_NAME}.hcl -reconfigure
deploy-dev:
extends: .terraform-init
environment:
name: dev
script:
- terraform apply -auto-approve
deploy-prod:
extends: .terraform-init
environment:
name: production
script:
- terraform plan -out=plan.tfplan
- terraform apply plan.tfplanBackend initialization required means terraform init needs to run. Use -reconfigure when the backend changed and you don't need state migration. Use -migrate-state when moving between backends and you want to keep your state. In CI/CD, always run terraform init with consistent -backend-config flags.
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.