Terraform: coalesce used in output value does not behave as expected

Created on 17 Mar 2017  ยท  3Comments  ยท  Source: hashicorp/terraform

Hi there,

_Terraform version: 0.8.8 (at the moment we cannot use 0.9.0 as we rely on the remote option)._

I have created a module that creates an aws_autoscaling_group instance chosen between 2 possible configurations.

resource "aws_autoscaling_group" "a" {
  launch_configuration = "ANY"
  max_size = 1
  min_size = 2
  count = "${var.condition ? 1 : 0}"
}

resource "aws_autoscaling_group" "b" {
  launch_configuration = "ANY"
  max_size = 2
  min_size = 3
  count = "${var.condition ? 0 : 1}"
}

I expected that the value returned in output could be:

output "id" {
  value = "${coalesce(aws_autoscaling_group.a.id, aws_autoscaling_group.b.id)}"
}

but no matter which resource I initialise, the output is always empty.

Thank you in advance.

bug core

Most helpful comment

Hi @gurdulu! Sorry for the problem here.

I believe what's going on here is that your value expression here is returning an error, but because of the long-standing bug #9080 this error is not shown to you.

If you add the following code temporarily you should see the underlying error here:

resource "null_resource" "temp" {
  triggers = {
    output = "${coalesce(aws_autoscaling_group.a.id, aws_autoscaling_group.b.id)}"
  }
}

My guess is that you're also getting tripped up by #11566 here: Terraform evaluates both "sides" of that conditional, even though only one of them will eventually be used. It's an error to interpolate an attribute from a resource block with count = 0, so this fails.

Someone in that other issue found a workaround that works until we get that fixed:

  value = "${coalesce(join("", aws_autoscaling_group.a.*.id), join("", aws_autoscaling_group.b.*.id))}"

This clever trick exploits the fact that this "splat syntax" produces an empty list if count = 0 and so the join will produce an empty string in that case, getting the behavior you were looking for.

Both of these things we'd like to fix, but since they are tracked by existing issues I'm going to close this to consolidate discussion over there. Thanks for taking the time to report this!

(If it turns out that my guess was incorrect about what error you'll get here, do let me know... it's possible that there's a different bug here, masked by the error being hidden.)

All 3 comments

Hi @gurdulu! Sorry for the problem here.

I believe what's going on here is that your value expression here is returning an error, but because of the long-standing bug #9080 this error is not shown to you.

If you add the following code temporarily you should see the underlying error here:

resource "null_resource" "temp" {
  triggers = {
    output = "${coalesce(aws_autoscaling_group.a.id, aws_autoscaling_group.b.id)}"
  }
}

My guess is that you're also getting tripped up by #11566 here: Terraform evaluates both "sides" of that conditional, even though only one of them will eventually be used. It's an error to interpolate an attribute from a resource block with count = 0, so this fails.

Someone in that other issue found a workaround that works until we get that fixed:

  value = "${coalesce(join("", aws_autoscaling_group.a.*.id), join("", aws_autoscaling_group.b.*.id))}"

This clever trick exploits the fact that this "splat syntax" produces an empty list if count = 0 and so the join will produce an empty string in that case, getting the behavior you were looking for.

Both of these things we'd like to fix, but since they are tracked by existing issues I'm going to close this to consolidate discussion over there. Thanks for taking the time to report this!

(If it turns out that my guess was incorrect about what error you'll get here, do let me know... it's possible that there's a different bug here, masked by the error being hidden.)

Hi @apparentlymart

Thank you

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

Related issues

cpoole picture cpoole  ยท  3Comments

rjinski picture rjinski  ยท  3Comments

zeninfinity picture zeninfinity  ยท  3Comments

c4milo picture c4milo  ยท  3Comments

ronnix picture ronnix  ยท  3Comments