Terraform conditional expressions require both branches to return the same type. This error occurs when your ternary operator returns different types (e.g., string vs number), and Terraform's type system won't allow implicit conversion.
In Terraform, conditional expressions use the syntax `condition ? true_value : false_value`. Both the `true_value` and `false_value` branches must have the exact same type. When Terraform detects type mismatches between branches—such as a string in one branch and a number in another, or objects with different structures—it raises this error to prevent type ambiguity. This is a strict type-safety feature. Unlike some languages that allow implicit type coercion, Terraform requires explicit type compatibility. The error message typically specifies what types were detected in each branch, helping you identify the mismatch.
Read the full error message carefully. Terraform tells you exactly which branch has which type:
Error: Inconsistent conditional result types
The true and false result expressions must have consistent types.
The 'true' value is string, but the 'false' value is number.Locate the conditional expression in your code that triggered this error.
Use Terraform's type conversion functions to ensure both branches return the same type. The most common conversions are:
Convert to string:
value = var.enabled ? "yes" : tostring(0)Convert to number:
value = var.enabled ? tonumber("42") : 0Convert to list:
value = var.enabled ? [var.item] : []Convert to map:
value = var.enabled ? { key = "value" } : {}Choose the conversion function that matches your desired output type.
If one branch returns a list and the other a single value, wrap both in lists:
# ❌ Wrong: mismatch between list and single value
items = var.use_list ? var.item_list : var.single_item
# ✅ Correct: both sides return lists
items = var.use_list ? var.item_list : [var.single_item]This ensures both branches produce the same type structure.
When objects have different attributes, explicitly define the same structure in both branches:
# ❌ Wrong: objects have different attributes
config = var.use_advanced ? {
name = "prod"
region = "us-east-1"
tags = { env = "prod" }
} : {
name = "dev"
}
# ✅ Correct: both have the same attributes
config = var.use_advanced ? {
name = "prod"
region = "us-east-1"
tags = { env = "prod" }
} : {
name = "dev"
region = "us-west-2"
tags = { env = "dev" }
}For more complex conditional logic with resource references, consider using splat expressions or try() to handle null values gracefully:
# Using splat for optional resource references
resource_ids = var.create_resources ? aws_instance.main[*].id : []
# Using try() to handle potential missing attributes
config = try(var.custom_config, var.default_config)These approaches help unify types when dealing with potentially missing resources or attributes.
Type Inference in Terraform: Terraform's type system is stricter than many programming languages. It performs type inference but doesn't allow implicit coercion between incompatible types. This design choice prevents silent bugs from type mismatches.
Empty Collections and Ambiguity: Empty collections like {} or [] can be ambiguous. An empty object could be map(string) or map(any). When used in conditionals, Terraform may struggle to unify types. In such cases, use explicit type constructors or splat expressions.
Null Handling: Mixing null with other types in conditionals can cause issues because null is compatible with any type at the value level, but Terraform's type checker is strict. Use try(), coalesce(), or wrap values in collections to handle optional values.
Dynamic Blocks and Splat: When working with conditional resource creation, prefer splat expressions (resource[*].id) over conditionals, as they naturally handle the zero or many case and return consistent list types.
Error: Error rendering template: template not found
How to fix "template not found" error in Terraform
Error: Error generating private key
How to fix 'Error generating private key' in Terraform
Error creating Kubernetes Service: field is immutable
How to fix "field is immutable" errors in Terraform
Error: Error creating local file: open: permission denied
How to fix "Error creating local file: permission denied" in Terraform
Error: line endings have changed from CRLF to LF
Line endings have changed from CRLF to LF in Terraform