Fix Terraform Error: CloudWatch Log Group Already Exists
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
DevOps
Fix null_resource provisioner failures in Terraform. Handle script errors, connection issues, triggers, and migrate to terraform_data for Terraform 1.4+.
The script or command in your null_resource provisioner returned a non-zero exit code. Check the script for errors, verify connection settings for remote-exec, and consider using terraform_data (Terraform 1.4+) as a modern replacement.
Error: local-exec provisioner error
Error running command './deploy.sh': exit status 1Error: null_resource.deploy (remote-exec): dial tcp 10.0.1.50:22:
connection refusednull_resource runs on create only; changes don't trigger re-runresource "null_resource" "deploy" {
provisioner "local-exec" {
command = "bash -x ./deploy.sh" # -x enables debug tracing
}
}Or test the script manually:
# Run the script directly to see the error
chmod +x deploy.sh
./deploy.shresource "null_resource" "configure" {
depends_on = [aws_instance.web]
connection {
type = "ssh"
host = aws_instance.web.public_ip
user = "ubuntu"
private_key = file("~/.ssh/deploy.pem")
timeout = "5m" # Wait for instance to boot
}
provisioner "remote-exec" {
inline = [
"sudo apt-get update",
"sudo apt-get install -y nginx",
]
}
}resource "null_resource" "deploy" {
triggers = {
app_version = var.app_version # Re-runs when version changes
script_hash = filemd5("deploy.sh") # Re-runs when script changes
}
provisioner "local-exec" {
command = "./deploy.sh ${var.app_version}"
}
}# Modern replacement for null_resource
resource "terraform_data" "deploy" {
triggers_replace = [var.app_version]
provisioner "local-exec" {
command = "./deploy.sh ${var.app_version}"
}
}resource "null_resource" "optional_step" {
provisioner "local-exec" {
command = "./optional-cleanup.sh || true" # Don't fail on error
on_failure = continue # Or use this
}
}| Problem | Fix |
|---|---|
| "command not found" | Use full path or set PATH in environment |
| "permission denied" | chmod +x script.sh |
| "connection refused" | Wait for instance boot, check security groups |
| Script runs on create only | Add triggers to re-run on changes |
| Resource tainted after failure | Fix script, then terraform apply |
Null resource provisioner failures are script/connection problems. Debug with bash -x, test scripts manually, use terraform_data for Terraform 1.4+, and add triggers so changes re-run the provisioner. For remote-exec, ensure SSH is accessible and the instance is fully booted.
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.