Splat Expression
A splat expression is syntax that uses the
[*]
symbol in order to loop over the collection of objects and execute certain operations on them. It has been called "splat" expression because the symbol [*]
is known as a "splat" operator.
The syntax of a splat expression is:
<collection>[*].<attribute>
Here is the breakdown of syntax:
[*]
The splat operator, which tells the expression to iterate over each object in the collection.
<attribute>
(Optional) The attribute or property that you want to access or manipulate for each object in the collection. This can be a simple attribute like name, or a more complex attribute like address.street.
Suppose you have a list of object serviceList and you wanted to get the name attribute of every object. You can do a splat expression like this:
variable "serviceList" { default = [ { name = "Ec2" Env = "Production" }, { name = "SQS" Env = "Stage" }, { name = "MSK" Env = Production } ] } output "service-name" { value = var.serviceList[*].name }
Here, the var.serviceList[*].name splat expression iterates over each object in the serviceList collection and extracts the name attribute of each object. The result will be names like:
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
service-name = [
"Ec2",
"SQS",
"MSK",
]
Splat Expressions with Maps
variable "serviceList" { default = [ { name = "Ec2" Env = "Production" }, { name = "SQS" Env = "Stage" }, { name = "MSK" Env = Production } ] } output "service-name" { value = var.serviceList[*] }
When the serviceList is a map (object), the splat expression var.serviceList[*] will iterate over the keys in the map, not the values. This means that the resulting output will be a list of the keys, not the values.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
service-name = {
"service1" = [
name = "Ec2"
Env = "Production"
],
"service2" = [
name = "SQS"
Env = "Stage"
],
"service3" = [
name = "MSK"
Env = "Production"
]
}
To access the values in a map, you would need to specify the key you want to access something like var.serviceList[*].service1.name. This informs Terraform of the need to iterate over the keys in the map and then access the service1 object and its name attribute.
output "service-name" { value = var.serviceList[*].service1.name }
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
service-name = [
"Ec2"
]
In order to pull out the name from each key, you should use a for expression rather than the splat expression.
output "service-name" { value = [for i, v in var.serviceList:v.name] }
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
service-name = [
"Ec2",
"SQS",
"MSK",
]
The major difference between the splat expression and for expression is that splat expression iterates over the map's keys, while the for expression over the map's values. In regard to a map, if you want to get access to values, then you're supposed to do a for expression instead of this splat expression.
Splat Expressions with Single Values
- If the value is not null, the splat expression converts it into a single-element list (or tuple).
variable "map" {
default = {
name = "Ec2"
Env = "Staging"
}
}
output "tranformed-var" {
value = var.map[*].name
}
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
tranformed-var = {
"Ec2"
}
Here, the splat expression (
[*]
) is used to convert the single-element map variable into a one-element list that is used for accessing the name attribute.
- If the value is null, the splat expression will return an empty tuple.
variable "nullable" {
default = null
}
output "nullable-list" {
value = var.nullable[*]
}
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
nullable-list = []
In this example where the splat expression is applied to the null value of the nullable variable. Because the value is null, the expression does not contain any elements to expand and returns the empty tuple.
Related Pages
- Variables - Input value
- Simple Data types - String
Feedback
Was this page helpful?