How to fix Error: Unsuitable value type - map of string required in Terraform
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.
Quick fix
Fixes Error: Unsuitable value type - map of string required
variable "my_map" {
type = map(string) # This requires all values to be strings
}- Verify the variable or argument type declaration
- Convert all map values to strings
- Use tostring() function for dynamic values
- For templatefile(), ensure tags/variables are strings
variable "my_map" {type = map(string) # This requires all values to be strings}Error: Unsuitable value type - map of string required
What this error means
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.
Typical symptoms
- Terraform plan or apply fails with 'Unsuitable value type - map of string required' error
- Error message appears when passing variable values or local maps to functions like templatefile()
- Module input validation fails when passing map values that don't match the declared type
- Error occurs even though the values appear to be correct in your configuration
Common causes
- Passing a map with non-string values to a variable or argument expecting map(string)
- Using numeric values (123) instead of string values ("123") in map literals
- Using boolean values (true) instead of string values ("true") in map assignments
- Passing nested maps or objects when the function expects map(string)
- Variable default values containing mixed types (some strings, some numbers)
- Using templatefile() with tags or variables parameter that contains non-string values
How to fix "Error: Unsuitable value type - map of string required"
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)
})
}Advanced notes
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.
Frequently asked questions
- What does "Error: Unsuitable value type - map of string required" mean?
- 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. , {tags = ["tag1", "tag2"]}) Terraform's type system detects the mismatch and raises this error.
- How do I fix "Error: Unsuitable value type - map of string required"?
- 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.
- What causes "Error: Unsuitable value type - map of string required"?
- Passing a map with non-string values to a variable or argument expecting map(string) Using numeric values (123) instead of string values ("123") in map literals Using boolean values (true) instead of string values ("true") in map assignments
- Is the "Error: Unsuitable value type - map of string required" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐Terraform Official Documentation
- ๐Terraform: Type Constraints documentation(Official Docs)
- ๐Terraform: Types and Values documentation(Official Docs)
- ๐