You have declared a variable called var.list which is a list of objects that all have an attribute id. Which options will produce a list of the IDs? (Choose two.)
https://www.terraform.io/language/expressions/splat
A splat expression provides a more concise way to express a common operation that could otherwise be performed with a for expression.
Here's an example:
Assume you have the following variable declaration:
variable "users" {
default = [
{
id = "id1"
name = "name1"
},
{
id = "id2"
name = "name2"
},
{
id = "id3"
name = "name3"
}
]
}
You can retrieve the list of IDs in your Terraform configuration using either of these options:
output "users_splat" {
value = var.users[*].id
}
output "users_for" {
value = [for user in var.users : user.id]
}
Both these outputs will produce the same list of IDs: ["id1", "id2", "id3"].
he correct options that will produce a list of the IDs are B and D:
Option B, var.list[*].id, uses the splat operator [*] to iterate over all elements of the var.list list and then accesses the id attribute of each object. The result is a list of all the id values.
Option D, [ for o in var.list : o.id ], uses a list comprehension to iterate over each object in the var.list list and create a new list that contains only the id attribute of each object.
B. var.list[*].id and D. [ for o in var.list : o.id ].
To produce a list of IDs from a list of objects with an id attribute, you can use either of the following options:
var.list[*].id: This uses the [*] wildcard to select all elements of the var.list list, and the .id syntax to select the id attribute of each element. This will produce a list of IDs.
[ for o in var.list : o.id ]: This uses a for expression to iterate over each element of var.list, selecting the id attribute of each element. This will produce a list of IDs.
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
bigboi23
Highly Voted 2 years, 6 months agoBere
Highly Voted 1 year, 3 months agoanand0310
Most Recent 2 months, 3 weeks agovibzr2023
7 months, 4 weeks agozimomar
10 months agondiichie
1 year, 3 months agosrajvanshi
1 year, 4 months agokrishna2802
1 year, 4 months agoNi33
1 year, 6 months agoAzRNoob
1 year, 7 months agocamps
1 year, 7 months agoPower123
1 year, 7 months agothor7
1 year, 7 months agoMal_8
1 year, 9 months agosahara99
1 year, 9 months agoTanacet
1 year, 9 months agoMohammed52
1 year, 10 months ago