Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append to a list in Terraform?

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

like image 593
David says Reinstate Monica Avatar asked May 02 '19 17:05

David says Reinstate Monica


People also ask

How do you concatenate in 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.

What are functions in terraform?

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)


2 Answers

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.

like image 134
Matt Schuchard Avatar answered Oct 16 '22 08:10

Matt Schuchard


var1 = ["string1","string2"]

var2 = "string3"

var3 = concat(var1, formatlist(var2))
like image 37
Armando Pitotti Avatar answered Oct 16 '22 06:10

Armando Pitotti