This error occurs when you assign a value to a Terraform variable or argument that expects a map(string) type, but you're providing a map with non-string values (numbers, booleans, lists, etc.). Terraform requires all values in a map(string) to be strings, and automatically converting incompatible types fails validation.
Fixes Error: Unsuitable value type - map of string required
variable "my_map" {
type = map(string) # This requires all values to be strings
}variable "my_map" {type = map(string) # This requires all values to be strings}Error: Unsuitable value type - map of string required
Terraform uses strict type checking for variables and resource arguments. When a variable or argument is declared with type map(string), it means the map must contain only string key-value pairs. If you attempt to assign: - A map with numeric values (e.g., {port = 8080}) - A map with boolean values (e.g., {enabled = true}) - A map with nested structures (e.g., {config = {nested = "value"}}) - A map with lists (e.g., {tags = ["tag1", "tag2"]}) Terraform's type system detects the mismatch and raises this error. This is a validation error that occurs during plan or apply phases, preventing invalid configurations from being applied.
Check the variable definition or resource/module argument that's causing the error. Look for the type constraint:
variable "my_map" {
type = map(string) # This requires all values to be strings
}If the type is explicitly declared as map(string), then all values must be strings. If you need to store different types, consider using map(any) or a more specific object type instead.
Wrap all non-string values in quotes to convert them to strings:
# Before (causes error):
variable "config" {
type = map(string)
default = {
port = 8080 # number, not string
enabled = true # boolean, not string
}
}
# After (works correctly):
variable "config" {
type = map(string)
default = {
port = "8080" # converted to string
enabled = "true" # converted to string
}
}All values must be enclosed in quotes to be treated as strings.
If you're assigning computed or variable values, use the tostring() function to ensure conversion:
variable "port_number" {
type = number
}
variable "config" {
type = map(string)
default = {
port = tostring(var.port_number) # Convert number to string
}
}
locals {
enabled_flag = true
config_map = {
enabled = tostring(local.enabled_flag) # Convert boolean to string
}
}This explicitly converts values to their string representation.
When using templatefile(), the variables parameter must be a map(string):
# Before (may cause error):
content = templatefile("${path.module}/template.tpl", {
port = 8080 # This is a number
})
# After (correct):
content = templatefile("${path.module}/template.tpl", {
port = "8080" # Explicitly a string
})This applies to local.file, template_file data source, and similar functions.
If you genuinely need to store multiple types in a map, change the type declaration:
# Instead of:
variable "config" {
type = map(string)
}
# Use:
variable "config" {
type = map(any) # Allows any type as values
}However, use this sparingly as it reduces type safety. For known structures, prefer explicit object types:
variable "config" {
type = object({
port = number
enabled = bool
tags = list(string)
})
}Type strictness in Terraform 0.12+: Terraform enforces strict type checking to prevent configuration errors. Unlike earlier versions, Terraform 0.12 introduced proper type inference and validation.
Implicit conversions: While Terraform automatically converts between some types (e.g., number to string in certain contexts), this doesn't apply to map values. All map values must explicitly match their declared type.
Working with modules: This error often appears when passing local values or variables to module inputs. Always check the module's variable type declarations in the module's variables.tf file.
Type inference with default values: If a variable has no explicit type but has a default value, Terraform infers the type. Mixed-type defaults will trigger this error.