I have some code in the general form:
variable "foo" {
type = "list"
default = [ 1,2,3 ]
}
resource "bar_type" "bar" {
bar_field = "${var.foo}"
}
I want to append an addition value to bar_field
without modifying foo
. How can I do this? I don't see any sort of contacting or appending functions in their docs.
This is 0.11.x Terraform
For Terraform 0.12 and later, you can use the join() function, to allow you to join or concatenate strings in your Terraform plans. The terraform join function has two inputs, the separator character and a list of strings we wish to join together.
The Terraform language includes a number of built-in functions that you can call from within expressions to transform and combine values. The general syntax for function calls is a function name followed by comma-separated arguments in parentheses: max(5, 12, 9)
You can use the concat function for this. Expanding upon the example in your question:
variable "foo" {
type = "list"
default = [ 1,2,3 ]
}
# assume a value of 4 of type number is the additional value to be appended
resource "bar_type" "bar" {
bar_field = "${concat(var.foo, [4])}"
}
which appends to the value assigned to bar_field
while ensuring var.foo
remains unchanged.
var1 = ["string1","string2"]
var2 = "string3"
var3 = concat(var1, formatlist(var2))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With