Learn how to efficiently append a string to each element of a list in Terraform using concise code examples.
In Terraform, you can easily append a string to each element of a list using a for
expression combined with string interpolation. This technique allows you to dynamically modify list elements, such as adding a suffix or prefix to each item. Let's explore how to achieve this with a simple example.
To append a string to each element of a list in Terraform, you can use a for
expression with string interpolation.
variable "names" {
default = ["Alice", "Bob", "Charlie"]
}
output "greetings" {
value = [for name in var.names : "${name}!"]
}
This code will output a list of strings where each element is the corresponding name from the names
variable with an exclamation mark appended to it:
greetings = [
"Alice!",
"Bob!",
"Charlie!",
]
Here's how it works:
for name in var.names
: This iterates over each element in the names
list, assigning each element to the name
variable in turn.${name}!
: This performs string interpolation. It takes the current value of the name
variable and concatenates it with an exclamation mark.[...]
: The square brackets enclose the entire expression, creating a new list from the results of each iteration.This approach leverages Terraform's built-in string interpolation and list manipulation capabilities to efficiently modify each element in a list.
This Terraform code defines a variable with a list of names and outputs a new list where each name is followed by an exclamation mark.
# Define a variable with a list of names
variable "names" {
default = ["Alice", "Bob", "Charlie"]
}
# Append a string to each element in the list
output "greetings" {
value = [for name in var.names : "${name}!"]
}
Explanation:
variable "names"
: This line defines a variable named "names" and sets its default value to a list containing three strings: "Alice", "Bob", and "Charlie".
output "greetings"
: This line defines an output variable named "greetings". Output variables are used to display values after Terraform has applied the configuration.
value = [for name in var.names : "${name}!"]
: This line uses a for
expression to iterate over each element in the var.names
list.
for name in var.names
: This part iterates through each element in the var.names
list, assigning each element to the temporary variable name
in each iteration."${name}!"
: This part performs string interpolation. It takes the current value of the name
variable (which represents each name from the list) and concatenates it with an exclamation mark ("!").[ ... ]
: The square brackets enclose the entire expression, creating a new list from the results of each iteration.Output:
When you run terraform apply
, the output will show the following:
greetings = [
"Alice!",
"Bob!",
"Charlie!",
]
This demonstrates that the code successfully appended an exclamation mark to each element in the original names
list.
concat
: While concat
can combine lists, it doesn't modify individual elements. This for
expression approach is specifically for modifying each element in a list.This document summarizes how to append a string to each element of a list in Terraform using a for
expression and string interpolation.
Feature | Description | Example |
---|---|---|
Goal | Append a specific string to the end of each element in a list. | Add "!" to each name in a list of names. |
Method | Use a for expression within square brackets [...] to iterate through the list. Inside the loop, use string interpolation ${...} to concatenate the current element with the desired string. |
[for name in var.names : "${name}!"] |
Example Input | ["Alice", "Bob", "Charlie"] |
|
Example Output | ["Alice!", "Bob!", "Charlie!"] |
Explanation:
for name in var.names
part loops through each element (name
) in the input list (var.names
).name
, ${name}!
combines the name with "!" using string interpolation.[...]
, creating a new list with the modified elements.This method effectively appends a string to each element of a list in Terraform, providing a concise and readable solution for various use cases. By understanding for
expressions and string interpolation, you can leverage this technique to manipulate lists dynamically and efficiently in your Terraform configurations.