Reputation: 107
Im new to Terraform. I am going through a tutorial and cant determine what this does " => "
Below is an example output utilizing this feature.
output "for_output_map2" {
description = "For Loop with Map - Advanced"
value = {for c, instance in aws_instance.myec2vm: c => instance.public_dns}
}
What is the name of this feature ? What does it do ? Thank you.
Upvotes: 9
Views: 11568
Reputation: 665
persons = {
tom: { name: "Anthony" },
amy: { name: "Amily" }
}
=>
is the symbol to separate key and value for
transformation purpose.
{ for k, v in persons: v.name => k }
Output:
{
Anthony: "tom",
Amily: "amy"
}
Upvotes: 1
Reputation: 1406
Simply put, the =>
is kind of "map element separator" in Terraform. It is used to define key-value pairs within a map.
Using =>
you transform terraform map into another array or map for some purpose (e.g. using them for for_each
Meta-Argument).
In snippet below, the for_each
loop expression "${employee.name}${employee.id}@gmail.com" => employee
is generating a map where the key is "${employee.name}${employee.id}@gmail.com"
and the value is the employee
object itself.
locals {
# Array of Objects
employees = [
{
name = "Alice"
id = 1234
age = 32
},
{
name = "Bob"
id = 5678
age = 40
}
]
}
resource "null_resource" "emp_directory" {
for_each = { for employee in local.employees : "${employee.name}${employee.id}@gmail.com" => employee }
provisioner "local-exec" {
command = "echo Email ID ${each.key} belongs to ${each.value.name}"
}
}
Output will be a map of maps. Notice how the key and value is transformed:
{
"[email protected]" = {
"age" = 32
"id" = 1234
"name" = "Alice"
}
"[email protected]" = {
"age" = 40
"id" = 5678
"name" = "Bob"
}
}
Terraform will perform the following actions:
# null_resource.emp_directory["[email protected]"] will be created
+ resource "null_resource" "emp_directory" {
+ id = (known after apply)
}
# null_resource.emp_directory["[email protected]"] will be created
+ resource "null_resource" "emp_directory" {
+ id = (known after apply)
}
Upvotes: 5
Reputation: 281
"=>" is a separator for one key and value pair inside an object.
For example, given a source object:
source = {
1: {name: "foo", id: 1212},
2: {name: "bar", id: 5656}
}
After this expression:
output "sample" {
value = {for k, v in source: k => v.name}
}
The output will be:
{1: "foo", 2: " bar"}
Upvotes: 10
Reputation: 74789
This =>
symbol is not a distinct operator in its own right, but rather just one of the delimiters for the parts of this overall for
expression:
for
introduces the for expression, telling Terraform that this isn't just a normal object expression.in
separates the symbol declarations from the source collection value.:
separates the source collection value from the key expression.=>
separates the key expression from the value expression.for
expressions can also have an if
clause which follows the value expression, but there isn't one in the example you shared here. You can learn more about the if
clause in Filtering Elements.
Upvotes: 8