Fix Terraform Error: CloudWatch Log Group Already Exists
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
DevOps
Fix state version mismatch errors when switching Terraform remote backends. Handle state migration, version conflicts, and backend reconfiguration.
The state file was written by a newer version of Terraform than you're running. Upgrade your Terraform to match the version that last wrote the state, or use terraform init -migrate-state when switching backends.
Error: State snapshot was created by Terraform v1.8.0,
which is newer than current v1.5.7Or when switching backends:
Error: Backend configuration changed
A change in the backend configuration has been detected, which
may require migrating existing state.Someone ran terraform apply with v1.8 and wrote the state, then you tried to use v1.5.
Moving from local to S3, or S3 to Terraform Cloud, without proper migration.
CI pipeline uses a different Terraform version than your local machine.
# Check what version wrote the state
# (look at the error message for the required version)
# Install the matching version with tfenv
tfenv install 1.8.0
tfenv use 1.8.0
terraform version# When switching from local to S3:
terraform init -migrate-state
# When reconfiguring the same backend type:
terraform init -reconfigure
# When you want to keep both:
terraform init -migrate-state
# Terraform will ask if you want to copy state# If migration fails, reconfigure (doesn't copy state):
terraform init -reconfigure
# Then import or rebuild state from scratch if needed# Prevent version drift across your team
terraform {
required_version = "~> 1.8.0" # Everyone must use 1.8.x
}# .terraform-version file (used by tfenv)
1.8.0# Step 1: Backup current state
terraform state pull > backup.tfstate
# Step 2: Update backend configuration in .tf files
# e.g., change from local to S3
# Step 3: Initialize with migration
terraform init -migrate-state
# Step 4: Verify
terraform plan # Should show no changes| Scenario | Command |
|---|---|
| Local → S3 | terraform init -migrate-state |
| S3 → Terraform Cloud | terraform init -migrate-state |
| Change S3 bucket/key | terraform init -migrate-state |
| Fix corrupted init | terraform init -reconfigure |
| Version too old | Upgrade Terraform, then terraform init |
required_version)terraform plan show no changes after migration?required_version in all configs — prevents version drift.terraform-version file with tfenv — automatic version switchingterraform state pull > backup.tfstateState version mismatches happen when team members use different Terraform versions. Pin required_version, use .terraform-version files, match CI/CD versions, and always back up state before migrations. Use terraform init -migrate-state when switching backends.
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.