Terraform: local and output evaluation fails when referencing a destroy node

Created on 24 Feb 2018  ยท  2Comments  ยท  Source: hashicorp/terraform

The pattern users have used to create "optional" resources is to dynamically set a count value to 0. In order to prevent interpolation errors in outputs (and locals), the suggested pattern has been to use the list from the splat (*) operator, along with an additional element to ensure there is always a value for interpolation:

"${element(concat(resource.name.*.attr, list("")), 0)}"

This works when the resource remains present in the graph, but can fail when the count is later changed to 0.

Using the following config:

variable "count" {}


resource "null_resource" "one" {
  count = "${var.count}"
}

locals {
  "one_id" = "${element(concat(null_resource.one.*.id, list("")), 0)}"
}

resource "null_resource" "two" {
  triggers = {
    required = "${local.one_id}"
  }
}

We can reproduce this with

$ terraform apply -auto-approve -var count=1
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

$ terraform apply -auto-approve -var count=0
Error: Error applying plan:

1 error(s) occurred:

* local.one_id: local.one_id: Resource 'null_resource.one' does not have attribute 'id' for variable 'null_resource.one.*.id'
bug core

Most helpful comment

I am seeing the exact same issue, not mention how hard was to catch this error.

All 2 comments

I am seeing the exact same issue, not mention how hard was to catch this error.

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