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 default value for variable errors. Match default values to type constraints, handle list/map/object types
# ❌ Type mismatch: default is string, type is list
variable "cidrs" {
type = list(string)
default = "10.0.0.0/16" # Wrong: string, not list
}
# ✅ Fixed
variable "cidrs" {
type = list(string)
default = ["10.0.0.0/16"] # Correct: list of strings
}Error: Invalid default value for variable
on variables.tf line 1:
1: variable "cidrs" {
This default value is not compatible with the variable's type constraint:
string required, got list of string.The default value doesn't match the declared type. Terraform validates this at parse time.
# ❌
variable "allowed_ips" {
type = list(string)
default = "10.0.0.0/8"
}
# ✅
variable "allowed_ips" {
type = list(string)
default = ["10.0.0.0/8"]
}# ❌
variable "port" {
type = number
default = "8080" # String, not number
}
# ✅
variable "port" {
type = number
default = 8080 # No quotes
}# ❌
variable "enabled" {
type = bool
default = "true" # String, not bool
}
# ✅
variable "enabled" {
type = bool
default = true # No quotes
}# ❌ Object requires specific keys
variable "settings" {
type = object({
name = string
port = number
})
default = {
name = "web"
port = "8080" # Wrong: string, should be number
}
}
# ✅
variable "settings" {
type = object({
name = string
port = number
})
default = {
name = "web"
port = 8080
}
}# ✅ Empty list
variable "tags" {
type = list(string)
default = []
}
# ✅ Empty map
variable "labels" {
type = map(string)
default = {}
}
# ✅ Null (variable is optional but has no default value)
variable "description" {
type = string
default = null
}variable "ingress_rules" {
type = list(object({
port = number
protocol = string
cidr_blocks = list(string)
}))
default = [
{
port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
},
{
port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
]
}variable "database" {
type = object({
engine = string
instance_class = string
storage = optional(number, 20) # Defaults to 20
multi_az = optional(bool, false) # Defaults to false
tags = optional(map(string), {}) # Defaults to empty map
})
default = {
engine = "postgres"
instance_class = "db.t3.micro"
# storage, multi_az, tags use their optional defaults
}
}# Validate catches type mismatches
terraform validate
# Test in console
terraform console
> var.cidrsInvalid default value means the default doesn't match the type. Numbers don't get quotes, lists need brackets, bools are true/false without quotes. Use optional() for object attributes that should have their own defaults. Run terraform validate to catch these errors early.
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.