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.
concat - Functions - Configuration Language | Terraform ... | The concat function combines two or more lists into a single list.
Validate list(object) variables - Terraform - HashiCorp Discuss | Hi i am trying to add validations for list of an object type: variable "rules" { type = list(object({ name = string access = string })) validation { condition = contains(["Allow", "Deny"], var.rules.access) error_message = "Invalid access, can be either Allow or Deny." } } I understand this won’t work, 'am trying to see if there is any way I can validate the object. Thank you !!
Terraform Strings: Interpolation, Multiline & Built-in Functions | Learn about Terraform multiline string, variable interpolation, concatenating simple strings and explore functions used for string manipulation.
How to combine all tuples in list of maps - Terraform - HashiCorp ... | Hi all, I have built a list of maps as follows: locals{ test = flatten([for key, env in var.api_gw_jwt_list_sa: [for key, info in env: { "${info.sa}" = { "flow" = "implicit" "authorizationUrl" = "" } ...
Produce maps from list of strings of a map - Terraform - HashiCorp ... | Hello, I have been pulling my hair on this for a couple of days now, I was wondering if someone could come up with a clever way to produce something like: Outputs: association-map = { "policy1_user1" = [ "policy1", "user1" ] "policy2_user1" = [ "policy2", "user1" ] "policy2_user2" = [ "policy2", "user2" ] } From: variable iam-policy-users-map { default = { "policy1" = [ "user1" ] "policy2" = [ "user1", "user2" ] } } The actual use case beh...
How do you do simple string concatenation in Terraform? | by ... | In Terraform, you can perform simple string concatenation using the ${} syntax or the join() function, depending on your specific…