This error occurs when Terraform encounters a value with the wrong type in a context that requires a number. Common causes include using strings or booleans where numbers are expected, particularly in count expressions, arithmetic operations, or module arguments.
The 'Unsuitable value type - number required' error means that a Terraform expression is receiving a value that is not a number (such as a string, boolean, list, or null) in a location where a numeric type is required. This happens in several contexts: count and for_each meta-arguments must receive numeric indices, arithmetic operations require numeric operands, and certain function arguments expect specific numeric types. Type checking occurs during the planning phase, before any resources are created.
Read the error message carefully to locate which line and expression caused the issue. The error will typically point to a count expression, arithmetic operation, or function argument that received a non-numeric value.
Convert boolean values to 0 or 1 in count expressions:
# Incorrect: boolean in count
resource "aws_instance" "example" {
count = var.create_instance # true or false
}
# Correct: convert to 0 or 1
resource "aws_instance" "example" {
count = var.create_instance ? 1 : 0
}
# Also correct: use ternary with conditions
resource "aws_instance" "example" {
count = var.environment == "prod" ? 1 : 0
}Ensure all operands in arithmetic expressions are numbers. Use tonumber() to convert strings:
# Incorrect: string addition
port = var.base_port + 8080
# Correct: convert to number first
port = tonumber(var.base_port) + 8080
# Or define variable as number type
variable "base_port" {
type = number
}When using multiple conditions in count, ensure the final expression evaluates to a number:
# Incorrect: complex expression may not return a number
count = var.create_vpc && length(var.subnets) > 0
# Correct: explicitly use ternary to return 0 or 1
count = var.create_vpc && length(var.subnets) > 0 ? 1 : 0Define variables with explicit number types to prevent type confusion:
variable "instance_count" {
type = number
default = 1
}
variable "port" {
type = number
}
# Not just
variable "instance_count" {
default = 1 # Could be interpreted as string
}Check the actual type of expressions using terraform console:
terraform console
# Check type of a variable
type(var.base_port)
# Check type of an expression
type(var.create_instance ? 1 : 0)
# Evaluate complex expressions
var.create_vpc && length(var.subnets) > 0 ? 1 : 0After making corrections, validate and plan:
terraform validate
terraform planIf validation passes, the type error should be resolved.
This error commonly occurs when migrating configurations from Terraform 0.11 to 0.12+, which introduced strict type checking. The count meta-argument specifically expects an integer, so boolean values always fail. In Terraform 1.0+, dynamic blocks provide an alternative to count for some use cases, which can simplify type handling. When working with third-party modules, ensure you're passing variables with the correct types as specified in the module's variable definitions. Type mismatches can also occur when variable defaults are omitted and Terraform infers types from usage patterns.
Error: Error installing helm release: cannot re-use a name that is still in use
How to fix "release name in use" error in Terraform with Helm
Error: Error creating GKE Cluster: BadRequest
BadRequest error creating GKE cluster in Terraform
Error: External program failed to produce valid JSON
External program failed to produce valid JSON
Error: Unsupported argument in child module call
How to fix "Unsupported argument in child module call" in Terraform
Error: network is unreachable
How to fix "network is unreachable" in Terraform