Skip to main content

Fix Terraform Error: Invalid Default Value for Variable

Key Takeaway

Fix terraform invalid default value for variable errors. Match default values to type constraints, handle list/map/object types, and use optional() for flexible objects.

Table of Contents

Quick Answer

# ❌ 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
}

The Error

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.

What Causes This

The default value doesn’t match the declared type. Terraform validates this at parse time.

Common Mistakes and Fixes

String vs List

# ❌ 
variable "allowed_ips" {
  type    = list(string)
  default = "10.0.0.0/8"
}

# ✅
variable "allowed_ips" {
  type    = list(string)
  default = ["10.0.0.0/8"]
}

String vs Number

# ❌
variable "port" {
  type    = number
  default = "8080"    # String, not number
}

# ✅
variable "port" {
  type    = number
  default = 8080      # No quotes
}

String vs Bool

# ❌
variable "enabled" {
  type    = bool
  default = "true"    # String, not bool
}

# ✅
variable "enabled" {
  type    = bool
  default = true      # No quotes
}

Map vs Object

# ❌ 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 Defaults

# ✅ 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
}

Complex Type Examples

List of Objects

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"]
    }
  ]
}

Optional Attributes (Terraform 1.3+)

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
  }
}

Debugging

# Validate catches type mismatches
terraform validate

# Test in console
terraform console
> var.cidrs

Hands-On Courses

Conclusion

Invalid 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.

🚀

Level Up Your Terraform Skills

Hands-on courses, books, and resources from Luca Berton

Luca Berton
Written by

Luca Berton

DevOps Engineer, AWS Partner, Terraform expert, and author. Creator of Ansible Pilot, Terraform Pilot, and CopyPasteLearn.