Terraform boolean gotcha

Terraform coerces the literal booleans false and true (e.g., when used as variable values) to 0 and 1 respectively. As a result, this ternary expression:

count = "${var.myboolean == true ? 1 : 0}"

unexpectedly always evaluates to 0. You want to use

count = "${var.myboolean ? 1 : 0}"

Or even just

count = "${var.myboolean}"

if that meets your readability standards.

Being loose with type coercion has its drawbacks...

Show Comments