Reputation: 1723
I need to create a condition in terraform 11 where I check if a local list variable exists, if it does then use that list and if not use a different one.
For example, I have:
var.localList = ["apples", "oranges"]
var.remoteList = ["bananas", "carrots"]
Now I want to do something like this (which didn't work):
myVar = ["${length(var.localList) > 0 ? var.localList : var.remoteList}"]
So the idea is that if var.localList
is not empty, assign that list to myVar
, otherwise use var.remoteList
.
Not sure if this is possible in Terraform 11.
EDIT: Forgot to mention that I'm using terraform 11.
Upvotes: 0
Views: 11017
Reputation: 8947
The function coalescelist
seems to do exactly what you want.
From the docs
coalescelist takes any number of list arguments and returns the first one that isn't empty.
So you would write something like
myVar = coalescelist(var.localList, var.localList)
Upvotes: 2
Reputation: 1723
I found the answer here https://github.com/hashicorp/terraform/issues/18259
Basically it's a work-around at this point
myVar = "${split(",", length(var.localList) > 0 ? join(",", var.localList) : join(",", var.remoteList))}"
Upvotes: 0
Reputation: 34416
You could do this with Local Values:
variable "local_list" {
default = ["foo"]
}
variable "remote_list" {
default = ["bar"]
}
locals {
myvar = length(var.local_list) > 0 ? var.local_list : var.remote_list
}
# myvar value is "foo"
output "myvar" {
value = local.myvar
}
Upvotes: 3