How to Append to a List in Terraform?
TLDR
To append elements to a list in Terraform, use the concat
function or dynamic blocks for more complex scenarios. These methods allow you to manage lists dynamically and efficiently.
Terraform provides multiple ways to append elements to a list, depending on your use case. This guide explains how to achieve this with practical examples.
Step 1: Use the concat Function
The concat
function is the simplest way to append elements to a list.
Example
variable "example_list" {
default = ["apple", "banana"]
}
locals {
updated_list = concat(var.example_list, ["cherry"])
}
output "result" {
value = local.updated_list
}
Explanation
concat(var.example_list, ["cherry"])
: Appends the elementcherry
to the existing list.local.updated_list
: Stores the updated list.
Step 2: Use Dynamic Blocks for Complex Scenarios
Dynamic blocks can be used to append elements to a list when working with resources or modules.
Example
variable "example_list" {
default = ["apple", "banana"]
}
resource "null_resource" "example" {
count = length(var.example_list)
triggers = {
item = var.example_list[count.index]
}
}
output "result" {
value = [for r in null_resource.example : r.triggers.item] ++ ["cherry"]
}
Explanation
null_resource
: Iterates over the existing list.++ ["cherry"]
: Appends the elementcherry
to the list.
Step 3: Combine with Conditional Logic
You can conditionally append elements to a list based on specific criteria.
Example
variable "example_list" {
default = ["apple", "banana"]
}
locals {
updated_list = var.example_list
final_list = var.example_list == [] ? ["default"] : concat(local.updated_list, ["cherry"])
}
output "result" {
value = local.final_list
}
Explanation
var.example_list == []
: Checks if the list is empty.concat(local.updated_list, ["cherry"])
: Appendscherry
if the list is not empty.
Best Practices
- Use Descriptive Variable Names: Clearly indicate the purpose of each variable.
- Validate Inputs: Ensure lists are in the expected format before appending elements.
- Document Logic: Provide clear comments for complex expressions.
By following these steps, you can effectively append elements to a list in Terraform, enabling dynamic and flexible configurations.
Found an issue?