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 managed by newer provider version errors. Upgrade with terraform init -upgrade, pin versions in required_providers
# Upgrade provider to match state
terraform init -upgradeError: Resource instance managed by newer provider version
The current state of aws_instance.web was created by a newer provider version
than is currently selected. Upgrade the hashicorp/aws provider to work with
this resource.
Current version: 5.40.0
Required version: >= 5.60.0Or:
Error: state snapshot was created by Terraform v1.9.0, which is newer than
current v1.7.0.terraform.lock.hcl pins an older version# Upgrade to latest matching version
terraform init -upgrade
# Check what version you now have
terraform version
terraform providersPrevent this from happening again:
terraform {
required_version = ">= 1.7.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.60" # Everyone uses 5.60+
}
}
}Then update the lock file for all platforms:
terraform providers lock \
-platform=linux_amd64 \
-platform=darwin_arm64
git add .terraform.lock.hcl
git commit -m "chore: update provider lock file"If the error is about the Terraform binary version (not provider):
Error: state snapshot was created by Terraform v1.9.0,
which is newer than current v1.7.0# Check your version
terraform version
# Upgrade Terraform
# macOS
brew upgrade terraform
# Linux
curl -LO https://releases.hashicorp.com/terraform/1.9.0/terraform_1.9.0_linux_amd64.zip
unzip terraform_1.9.0_linux_amd64.zip
sudo mv terraform /usr/local/bin/If you need an older version (rare):
# 1. Remove state reference and reimport
terraform state rm aws_instance.web
terraform import aws_instance.web i-0abc123
# 2. Or pull state, edit version (risky, last resort)
terraform state pull > state.json
# Edit the "terraform_version" field
terraform state push state.jsonWarning: Downgrading state is risky. Only do this if you understand the schema differences.
# GitLab CI
image:
name: hashicorp/terraform:1.9 # Exact version
entrypoint: [""]
before_script:
- terraform init # Uses lock file versions# Install tfenv
brew install tfenv
# Set project version
echo "1.9.0" > .terraform-version
# Auto-switches when you cd into the project
tfenv useAlways commit .terraform.lock.hcl — it ensures everyone uses the same provider versions:
git add .terraform.lock.hclState created by a newer provider means someone on your team (or CI/CD) used a newer version. Run terraform init -upgrade to match. Prevent future issues by pinning versions in required_providers, committing .terraform.lock.hcl, and using the same Terraform version in CI/CD as locally (tfenv + .terraform-version).
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.