Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional expression evaluates unreachable substitution #17960

Closed
wschult23 opened this issue Apr 27, 2018 · 5 comments
Closed

Conditional expression evaluates unreachable substitution #17960

wschult23 opened this issue Apr 27, 2018 · 5 comments

Comments

@wschult23
Copy link

wschult23 commented Apr 27, 2018

Terraform Version 0.11.5

I have a conditional data resource and I want to obtain a value from this resource. If the ressource doesn't exist, I want to set the value to an empty string.

data "aws_instance" "not_exist" {
 
  filter {
    name   = "tag:Name"
    values = ["non_existing"]
  }
}

locals {
  name = "${data.aws_instance.not_exist.count==0?"":data.aws_instance.not_exist.tags["Name"]}"
}

Expected Behavior

Due to the fact that the expression evaluates to true, I would expect, that the second term

data.aws_instance.not_exist.tags["Name"]

would not be evaluated and the value of locals.name is "".

Actual Behavior

Terraform produces a error message, that it can't find the ressource after applying the plan:

* local.name: local.name: Resource 'data.aws_instance.some_ressource_that_does_not_exist' not found for variable 'data.aws_instance.not_exist.tags'

Steps to Reproduce

terraform apply

@jbardin
Copy link
Member

jbardin commented Apr 27, 2018

Hi @wschult23,

Thanks for filing the issue. This is correct that conditional statements don't short-circuit.
We are working on including this in a suite of changes to the configuration language that will come in a future release.

@jbardin jbardin closed this as completed Apr 27, 2018
@jbardin
Copy link
Member

jbardin commented Apr 27, 2018

We're leaving the earlier issue #15605 open to track this feature.

@wschult23
Copy link
Author

wschult23 commented May 2, 2018 via email

@jbardin
Copy link
Member

jbardin commented May 2, 2018

Hi @wschult23,

There's no workaround if the resource does not exist in the configuration at all (I'm actually not sure yet if that will work in the next version, but it's not something that should be counted on).

If the resource doesn't exist because the count is 0, you can reference it as an empty list using the * operator. There's an example in the 0.11 upgrade guide here: https://www.terraform.io/upgrade-guides/0-11.html#referencing-attributes-from-resources-with-count-0

@ghost
Copy link

ghost commented Apr 3, 2020

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.

@ghost ghost locked and limited conversation to collaborators Apr 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants