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 invalid escape sequence errors. Double backslashes for Windows paths, use heredocs for regex, and learn all valid HCL escape sequences.
# ❌ Single backslash — invalid
path = "C:\Users\admin\scripts"
# ✅ Double backslash
path = "C:\\Users\\admin\\scripts"
# ✅ Or use forward slashes (works on Windows too)
path = "C:/Users/admin/scripts"Error: Invalid escape sequence
on main.tf line 5, in locals:
5: path = "C:\Users\admin"
The symbol "U" is not a valid escape sequence selector.HCL (Terraform's language) only recognizes these escape sequences:
| Escape | Meaning |
|---|---|
\\ | Literal backslash |
\" | Literal quote |
\n | Newline |
\r | Carriage return |
\t | Tab |
\uNNNN | Unicode character |
\UNNNNNNNN | Unicode character (8 hex digits) |
Any other \ followed by a character is an error. \U in \Users isn't valid.
locals {
# Windows paths
script_path = "C:\\Users\\admin\\scripts\\deploy.ps1"
share_path = "\\\\server\\share\\folder"
}Most Windows tools accept forward slashes:
locals {
script_path = "C:/Users/admin/scripts/deploy.ps1"
}
provisioner "local-exec" {
command = "powershell -File C:/Users/admin/scripts/deploy.ps1"
}locals {
# Heredoc — no escape processing
script = <<-EOT
$path = "C:\Users\admin\scripts"
Get-ChildItem $path | ForEach-Object { $_.Name }
EOT
}Note: Heredocs still process ${...} interpolation. Use <<-'EOT' for completely raw strings (Terraform 1.7+):
locals {
raw_script = <<-'EOT'
No interpolation: ${this.is.literal}
Backslashes work: C:\Users\admin
EOT
}Regex in Terraform often needs backslashes:
# ❌ Invalid
locals {
pattern = regex("\\d+\\.\\d+", var.version)
}
# ✅ Double-escape: \\ for HCL → \ for regex
locals {
# Match digits.digits (e.g., "5.60")
pattern = regex("\\d+\\.\\d+", var.version)
}Common regex escapes in HCL:
| Regex | In HCL string |
|---|---|
\d (digit) | "\\d" |
\. (literal dot) | "\\." |
\s (whitespace) | "\\s" |
\w (word char) | "\\w" |
\\ (literal backslash) | "\\\\\\\\" |
For complex templates with many backslashes, use templatefile():
# template.ps1
$path = "C:\Users\admin"
Get-Process | Where-Object { $_.CPU -gt 100 }
# main.tf — templatefile reads the file as-is (no HCL escaping)
locals {
script = templatefile("${path.module}/template.ps1", {
admin_user = var.admin_user
})
}HCL only allows \\, \", \n, \r, \t, and \uNNNN as escape sequences. For Windows paths, double the backslashes or use forward slashes. For complex strings with many special characters, use heredocs or templatefile().
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 module version constraint errors. Run terraform init -upgrade, understand version constraint syntax, pin versions for production