Terraform: using string in boolean conditional fails

Created on 21 Feb 2017  ยท  3Comments  ยท  Source: hashicorp/terraform

Terraform Version

0.8.7

Affected Resource(s)

any using count

Terraform Configuration Files

data "template_file" "trust_account" {
  template = "${file("${path.module}/trust-account-policy.json")}"
  count = "${var.trust_account_id && ! var.saml_idp_arn ? 1 : 0}"
}

Debug Output.

https://gist.github.com/tomdavidson/3dfffa47a99e59eb5861afd75d479617

Expected Behavior

the count interpolation would have resulted in behavior similar to an if-else - the resource is only created when var.trust_account_id is et and and var.saml_idp_arn is not set.

Actual Behavior

terraform plan errors

Error configuring: 3 error(s) occurred:

  • __builtin_StringToBool: strconv.ParseBool: parsing "1111111111111": invalid syntax in:
    ${var.saml_idp_arn || var.trust_account_id ? 0 : 1}

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:
Add count = "${var.trust_account_id && ! var.saml_idp_arn ? 1 : 0}" to a resource.
Set var.trust_account_id to "111111111111"
terraform plan

bug core

Most helpful comment

Hi @tomdavidson!

What you're seeing here is intended behavior but unfortunately it's surfacing with a bad error message that exposes some implementation details of how the interpolation language is implemented.

Conversion from string to boolean only works for strings that are directly representing boolean values, such as "true", "1", "false", and "0". This error is telling you that the string is not a valid boolean value and thus cannot be converted.

The correct way to write your expression is as follows:

    count = "${var.trust_account_id != "" && var.saml_idp_arn == "" ? 1 : 0}"

This way you don't try to use a string as a boolean, but instead compare two strings to produce a boolean result.


The bug here is that Terraform should've produced an error message at a more appropriate level of abstraction, such as:

Error configuring: 1 error(s) occurred:

* || operands must be boolean; string "1111111111111" cannot be converted to boolean in:
  ${var.saml_idp_arn || var.trust_account_id ? 0 : 1}

Ideally we could also do better and refer to the specific location in the expression, a suggested fix, etc but that's all more general interpolation language parser work that I'd like to tackle across all the evaluation error messages.

All 3 comments

Hi @tomdavidson!

What you're seeing here is intended behavior but unfortunately it's surfacing with a bad error message that exposes some implementation details of how the interpolation language is implemented.

Conversion from string to boolean only works for strings that are directly representing boolean values, such as "true", "1", "false", and "0". This error is telling you that the string is not a valid boolean value and thus cannot be converted.

The correct way to write your expression is as follows:

    count = "${var.trust_account_id != "" && var.saml_idp_arn == "" ? 1 : 0}"

This way you don't try to use a string as a boolean, but instead compare two strings to produce a boolean result.


The bug here is that Terraform should've produced an error message at a more appropriate level of abstraction, such as:

Error configuring: 1 error(s) occurred:

* || operands must be boolean; string "1111111111111" cannot be converted to boolean in:
  ${var.saml_idp_arn || var.trust_account_id ? 0 : 1}

Ideally we could also do better and refer to the specific location in the expression, a suggested fix, etc but that's all more general interpolation language parser work that I'd like to tackle across all the evaluation error messages.

Thank you @apparentlymart for taking a moment to convey an understanding and for a solution. I was just getting ready to post my much sillier and complicated work around (using replace) and ... well duh!

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings