Fix Terraform Error: CloudWatch Log Group Already Exists
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
DevOps
Fix HCL syntax errors: unexpected tokens, missing brackets, block definitions, and JSON vs HCL confusion. Common mistakes with code examples and fixes.
HCL syntax errors come from missing brackets, extra commas, = signs in block headers, or mixing JSON and HCL syntax. Run terraform fmt to auto-fix formatting, and terraform validate to catch structural errors. Use an editor with HCL support for real-time feedback.
Error: Invalid expression
on main.tf line 12:
Expected the start of an expression, but found an invalid expression token.Error: Argument or block definition required
on main.tf line 5, in resource "aws_instance" "web":
An argument definition must not be given as a block.Error: Unexpected token
on main.tf line 8:
Expected a comma or closing bracket, but got newline.# BAD — = sign in resource block header
resource = "aws_instance" "web" {
}
# GOOD — no = sign
resource "aws_instance" "web" {
}# BAD — missing closing brace
resource "aws_instance" "web" {
ami = "ami-12345"
instance_type = "t3.micro"
tags = {
Name = "web"
# Missing } for tags AND resource
# GOOD
resource "aws_instance" "web" {
ami = "ami-12345"
instance_type = "t3.micro"
tags = {
Name = "web"
}
}# BAD in older Terraform (OK in 1.0+, but can confuse)
cidr_blocks = [
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16", # Trailing comma — usually OK but check version
]
# Object keys with commas (BAD)
tags = {
Name = "web", # BAD — no commas between HCL attributes
Environment = "prod"
}
# GOOD — no commas in HCL blocks
tags = {
Name = "web"
Environment = "prod"
}# BAD — JSON syntax in .tf file
{
"resource": {
"aws_instance": {
"web": {
"ami": "ami-12345"
}
}
}
}
# GOOD — HCL syntax in .tf file
resource "aws_instance" "web" {
ami = "ami-12345"
}
# JSON is valid in .tf.json files only# BAD — argument outside any block
ami = "ami-12345"
resource "aws_instance" "web" {
instance_type = "t3.micro"
}
# GOOD — all arguments inside their block
resource "aws_instance" "web" {
ami = "ami-12345"
instance_type = "t3.micro"
}# BAD — unquoted string
resource "aws_instance" "web" {
ami = ami-12345 # Missing quotes
}
# GOOD
resource "aws_instance" "web" {
ami = "ami-12345"
}# Format all files in the current directory
terraform fmt
# Format recursively (all subdirectories)
terraform fmt -recursive
# Check which files need formatting (dry run)
terraform fmt -check -recursive# Check for syntax and configuration errors
terraform validate
# If validate fails, fix the reported line first
# Errors cascade — fixing line 5 often fixes lines 10, 15, etc.# VS Code — best HCL support
code --install-extension hashicorp.terraform
# JetBrains IDEs — built-in HCL support
# Install the "Terraform and HCL" plugin
# Vim/Neovim
# Use vim-terraform or nvim-lspconfig with terraform-ls# Count opening vs closing braces
grep -c '{' main.tf
grep -c '}' main.tf
# Numbers should match
# Find unmatched brackets with your editor's bracket matching feature
# VS Code: Ctrl+Shift+\ (jump to matching bracket)| Error | Cause | Fix |
|---|---|---|
Expected the start of an expression | Missing value after = | Add the value: ami = "ami-12345" |
Argument or block definition required | Stray text outside blocks | Move inside a block or delete |
Expected a comma or closing bracket | Missing ] or } | Close the collection |
Invalid character | Smart quotes from copy-paste | Replace " " with " |
Unexpected token after block definition | = in block header | Remove the = |
terraform fmt — fixes most formatting issuesterraform validate — check the specific line number{ and } — do they match?"") from copy-paste?.tf file?= in a resource or data block header?terraform fmt in CI — reject PRs with formatting issuesterraform validate as a pre-commit hook — catches errors before they're committedHCL syntax errors are usually simple: missing brackets, wrong quotes, or JSON-style commas in HCL blocks. Run terraform fmt to fix formatting, terraform validate to find structural issues, and use an editor with HCL support to prevent them in the first place. Fix the first error reported — later errors often cascade from the first.
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.