Thursday, May 12, 2022

[FIXED] How to append to a list in Terraform?

Issue

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


Solution

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.



Answered By - Matt Schuchard
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.