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 elementcherryto 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 elementcherryto 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"]): Appendscherryif 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.
We earn commissions when you shop through the links below.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?
Related Posts
Also worth your time on this topic
How to Get an Object from a List of Objects in Terraform?
Learn how to retrieve a specific object from a list of objects in Terraform using filters and expressions.
AWS VPC with Terraform
Build a complete AWS VPC infrastructure using Terraform with public/private subnets, NAT gateway, and security groups.
120 minutes
Infrastructure as Code Patterns
What are the key principles and patterns of Infrastructure as Code? How do you structure IaC for multiple environments?
mid