Skip to content

Instantly share code, notes, and snippets.

@justinclayton
Created May 1, 2018 19:02
Show Gist options
  • Save justinclayton/d2935f0092618ca7d231f085e0f003a3 to your computer and use it in GitHub Desktop.
Save justinclayton/d2935f0092618ca7d231f085e0f003a3 to your computer and use it in GitHub Desktop.
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
Copy link

pkoraca commented Feb 28, 2020

thanks <3

@Raghav2211
Copy link

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