Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Terraform: conditionally reference a data source that may or may not exist
variable "yes" { default = true }
data "template_file" "maybe" {
count = "${ var.yes == false ? 0 : 1 }"
template = "YES"
}
output "maybe" {
value = "${ var.yes == false ? "" : element(concat(data.template_file.maybe.*.rendered, list("")), 0) }" # <-- :_(
}
@pkoraca

This comment has been minimized.

Copy link

@pkoraca pkoraca commented Feb 28, 2020

thanks <3

@Raghav2211

This comment has been minimized.

Copy link

@Raghav2211 Raghav2211 commented Dec 31, 2020

it'll fail because Terraform will evaluate all variable in ternary operation before use
So if var.yes = true then it'll generate error because data.template_file. maybe is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment