Terraform Associate Exam QuestionsBrowse all questions from this exam

Terraform Associate Exam - Question 38


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.)

Show Answer
Correct Answer: BD

To produce a list of the IDs from a list of objects that have an id attribute, you can use the following options: var.list[*].id and [ for o in var.list : o.id ]. The first option uses the splat operator [*] to iterate over all elements of the list and select the id attribute, resulting in a list of IDs. The second option uses a for-expression to iterate over each object in the list and collect the id attribute of each object into a new list. Both methods will correctly produce a list of IDs from var.list.

Discussion

35 comments
Sign in to comment
bigboi23Options: BD
May 18, 2022

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.

BereOptions: BD
Jul 29, 2023

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"].

vitasacOptions: BD
May 3, 2022

BD for sure

ItaloVinodiOptions: BD
May 13, 2022

I'm wrong sorry according to documentation https://www.terraform.io/language/expressions/for BD is the right

petliura
May 25, 2022

B, D - straight from the documentation: https://www.terraform.io/language/expressions/splat

fsdgrtsdfcjmu
May 26, 2022

I choose BD

koneba1309Options: BD
May 2, 2022

B is splat, and D is for in expression

rajje
May 10, 2022

https://www.terraform.io/language/expressions/splat

Zam88Options: BD
Jun 20, 2022

BD correct

Ni33Options: AB
May 1, 2023

A and B is the correct answer

dumbu
May 7, 2022

AB is correct as D is already list and it has for loop inside a list, which is not required

ParthasarathiOptions: AB
May 13, 2022

AB is correct

ItaloVinodiOptions: AB
May 13, 2022

I think AB is correct also looking at brackets

najslejdiOptions: BD
Aug 24, 2022

B & D are correct

nick7513Options: BD
Sep 14, 2022

B&D are correct both [for o in var.list : o.id] This is equivalent to the following splat expression: var.list[*].id

sahara99Options: BD
Feb 15, 2023

A is wrong as the question asked which one creates a "list"

campsOptions: BD
Mar 31, 2023

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.

ndiichie
Jul 30, 2023

Answer is BD. https://developer.hashicorp.com/terraform/language/expressions/splat

vibzr2023
Mar 27, 2024

Apart from B and D , C is also correct > [for o in var.list : o.id] [ "1", "2", "3", ]

BlackZerosOptions: BD
Jun 25, 2022

b and d seems correct

EltoothOptions: BD
Jun 27, 2022

B and D are correct answers. https://www.terraform.io/language/expressions/splat

yuvifoseOptions: BD
Jul 18, 2022

B and D. {} are not lists but dicts/maps/objects

asudhinOptions: BD
Oct 3, 2022

It is B&D

niccsm
Oct 21, 2022

Definitely D not A https://developer.hashicorp.com/terraform/language/expressions/for

InformationOverloadOptions: BD
Dec 30, 2022

B and D is correct

Mohammed52Options: BD
Jan 24, 2023

B and D is corrrect

TanacetOptions: BD
Jan 28, 2023

The splat and interpolation-style

Mal_8Options: BD
Feb 18, 2023

BD. Tried each expression

thor7Options: BD
Mar 29, 2023

B and D are correct, checked them. Reference: https://developer.hashicorp.com/terraform/language/expressions/for

Power123
Mar 30, 2023

The splat and interpolation. Ans is B & D

AzRNoob
Apr 7, 2023

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.

krishna2802Options: BD
Jun 29, 2023

https://www.terraform.io/language/expressions/splat

srajvanshi
Jul 19, 2023

https://developer.hashicorp.com/terraform/language/expressions/splat B and D

zimomarOptions: BD
Jan 25, 2024

BD for sure

anand0310Options: BD
Aug 31, 2024

BD for sure