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 type specification errors. Don't quote type names, use correct type constructors
# ❌ Don't quote type names
variable "name" {
type = "string" # Wrong — quotes make it a string literal
}
# ✅ Types are keywords, not strings
variable "name" {
type = string
}Error: Invalid type specification
on variables.tf line 2, in variable "name":
2: type = "string"
A type specification is either a primitive type keyword (bool, number,
string) or a complex type constructor call, like list(string)."string" instead of stringarray(string) instead of list(string)list string instead of list(string)type = "list" was valid before 0.12variable "name" { type = string } # "hello"
variable "count" { type = number } # 42, 3.14
variable "enabled" { type = bool } # true, falsevariable "names" { type = list(string) }
# ["web", "api", "db"]
variable "ports" { type = list(number) }
# [80, 443, 8080]
variable "tags" { type = map(string) }
# { Environment = "prod", Team = "platform" }
variable "unique_ids" { type = set(string) }
# ["a", "b", "c"] — duplicates removed# Object — fixed keys with types
variable "database" {
type = object({
engine = string
instance_class = string
port = number
multi_az = bool
})
}
# Tuple — fixed-length list with typed positions
variable "config" {
type = tuple([string, number, bool])
# ["web", 8080, true]
}variable "ingress_rules" {
type = list(object({
port = number
protocol = string
cidr_blocks = list(string)
}))
}
variable "environment_tags" {
type = map(map(string))
# { prod = { owner = "ops" }, dev = { owner = "dev" } }
}variable "instance" {
type = object({
name = string
instance_type = string
monitoring = optional(bool, false) # Default: false
tags = optional(map(string), {}) # Default: {}
ebs_size = optional(number) # Default: null
})
}variable "flexible" {
type = any # Accepts anything — use sparingly
}
variable "mixed_list" {
type = list(any) # List of anything
}| Wrong | Correct | Why |
|---|---|---|
type = "string" | type = string | Don't quote keywords |
type = "list" | type = list(string) | Old TF 0.11 syntax |
type = array(string) | type = list(string) | No array type in HCL |
type = hash(string) | type = map(string) | No hash type |
type = dict(string) | type = map(string) | No dict type |
type = int | type = number | No int — use number |
type = float | type = number | No float — use number |
type = boolean | type = bool | It's bool, not boolean |
Type specifications are keywords (string, number, bool) or constructors (list(string), map(number), object({...})), never quoted strings. If upgrading from Terraform 0.11, replace type = "list" with type = list(string). Use optional() for object attributes with defaults.
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.