Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.
exam questions

Exam Terraform Associate All Questions

View all questions & answers for the Terraform Associate exam

Exam Terraform Associate topic 1 question 146 discussion

Actual exam question from HashiCorp's Terraform Associate
Question #: 146
Topic #: 1
[All Terraform Associate Questions]

You can reference a resource created with for_each using a Splat (*) expression.

  • A. True
  • B. False
Show Suggested Answer Hide Answer
Suggested Answer: B 🗳️

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
Nunyabiznes
Highly Voted 1 year, 7 months ago
Selected Answer: A
resource "aws_instance" "example" { for_each = { "web-1" = "ami-0c55b159cbfafe1f0" "web-2" = "ami-0c55b159cbfafe1f0" } instance_type = "t2.micro" ami = each.value } # Accessing a single instance output "web_1_id" { value = aws_instance.example["web-1"].id } # Accessing all instances using a Splat expression output "instance_ids" { value = aws_instance.example.*.id } In this example, aws_instance.example creates two EC2 instances using the same AMI, and for_each is used to create the instances with distinct identifiers. The first output references a single instance by its identifier, while the second output uses a Splat expression (aws_instance.example.*) to reference all instances and return a list of their IDs.
upvoted 9 times
...
chxnedu
Highly Voted 8 months, 1 week ago
B "Resources that use the for_each argument will appear in expressions as a map of objects, so you can't use splat expressions with those resources." https://developer.hashicorp.com/terraform/language/expressions/splat
upvoted 5 times
...
Kaname93
Most Recent 9 months ago
Selected Answer: B
From the documentation : Resources that use the for_each argument will appear in expressions as a map of objects, so you can't use splat expressions with those resources. For more information, see Referring to Resource Instances.
upvoted 3 times
...
NashP
9 months, 3 weeks ago
When using the for_each argument to create multiple instances of a resource in Terraform, you can reference the attributes of those instances using a Splat expression (*). The Splat expression is used to extract values from a collection of resources. variable "instance_names" { type = set(string) default = ["web-1", "web-2", "web-3"] } resource "aws_instance" "example" { for_each = var.instance_names ami = "ami-0123456789abcdef0" instance_type = "t2.micro" key_name = "your-key-pair-name" tags = { Name = each.key } } # Reference the attributes using Splat expression output "instance_ids" { value = aws_instance.example[*].id } A:TURE
upvoted 2 times
...
TigerInTheCloud
11 months, 1 week ago
Selected Answer: B
$ terraform validate ╷ │ Error: Missing resource instance key │ │ on main.tf line 15, in output "ec2s": │ 15: value = aws_instance.example.ebs_block_device.*.id │ │ Because aws_instance.example has "for_each" set, its attributes must be accessed on specific instances. │ │ For example, to correlate with indices of a referring resource, use: │ aws_instance.example[each.key]
upvoted 1 times
...
BaburTurk
1 year, 2 months ago
Selected Answer: B
You cannot reference a resource created with for_each using a splat (*) expression. Resources that use the for_each argument will appear in expressions as a map of objects, so you can't use splat expressions and you have to use a for expression loop. For example, the following code will create two EC2 instances: resource "aws_instance" "web" { for_each = [1, 2] ami = "ami-0123456789abcdef0" instance_type = "t2.micro" } You cannot reference the EC2 instances created by this code using a splat expression, such as *aws_instance.web. Instead, you would have to use a for expression loop, such as: for i, instance in aws_instance.web.items() { # Do something with the instance }
upvoted 3 times
...
kiran15789
1 year, 6 months ago
aws_instance.example[*].id this is valid
upvoted 1 times
...
gspb
1 year, 6 months ago
Selected Answer: B
Option is the correct answer. "You can’t refer to a resource using the splat operator [*] if the resource uses a for_each argument. This is because in this case the resource is a map of objects rather than a list of objects. The splat operator applies only to lists." https://www.terraformbyexample.com/splat/
upvoted 4 times
gspb
1 year, 6 months ago
*option B is the correct answer
upvoted 1 times
...
...
FarziWaliMarzi
1 year, 7 months ago
Selected Answer: B
https://developer.hashicorp.com/terraform/language/expressions/splat#legacy-attribute-only-splat-expressions A splat expression provides a more concise way to express a common operation that could otherwise be performed with a for expression. Hence B
upvoted 2 times
...
Fliyd
1 year, 7 months ago
Selected Answer: A
There is a difference between splat and LEGACY splat. in this case, a legacy splat can be used, even if depreciated. source : https://developer.hashicorp.com/terraform/language/expressions/splat#legacy-attribute-only-splat-expressions
upvoted 1 times
...
David_C_90
1 year, 8 months ago
Selected Answer: B
The splat expression patterns shown above apply only to lists, sets, and tuples. To get a similar result with a map or object value you must use for expressions. Resources that use the for_each argument will appear in expressions as a map of objects, so you can't use splat expressions with those resources. For more information, see Referring to Resource Instances. https://developer.hashicorp.com/terraform/language/expressions/splat#splat-expressions-with-maps
upvoted 1 times
...
Daro_
1 year, 9 months ago
Selected Answer: B
B https://www.terraformbyexample.com/splat/ You can’t refer to a resource using the splat operator [*] if the resource uses a for_each argument. This is because in this case the resource is a map of objects rather than a list of objects. The splat operator applies only to lists.
upvoted 3 times
...
lezgino
1 year, 9 months ago
Selected Answer: A You can reference a resource created with for_each using a Splat () expression, also known as the "splat operator". The Splat operator allows you to reference all of the instances of a resource created with the for_each argument in a single reference, by using an asterisk () in the reference. This can be useful when you need to reference all instances of a resource in a single expression, such as when setting up dependencies between resources or referencing outputs from multiple resources.
upvoted 1 times
...
Abuu
1 year, 10 months ago
Selected Answer: A
A Splat (*) expression allows you to reference a resource created with for_each by expanding the expression into a list of individual resource references. This makes it easier to access the resources, as you don't have to reference each one individually.
upvoted 1 times
...
dinesh198728
2 years, 2 months ago
Selected Answer: B
Splat Expressions with Maps The splat expression patterns shown above apply only to lists, sets, and tuples. To get a similar result with a map or object value you must use for expressions. Resources that use the for_each argument will appear in expressions as a map of objects, so you can't use splat expressions with those resources. For more information, see Referring to Resource Instances. https://www.terraform.io/language/meta-arguments/for_each#referring-to-instances
upvoted 4 times
...
[Removed]
2 years, 2 months ago
Selected Answer: B
Note that unlike count, splat expressions are not directly applicable to resources managed with for_each, as splat expressions must act on a list value. However, you can use the values() function to extract the instances as a list and use that list value in a splat expression: values(aws_instance.example)[*].id https://www.terraform.io/language/expressions/references
upvoted 3 times
...
depal_dhir
2 years, 2 months ago
Selected Answer: B
https://www.terraform.io/language/expressions/splat
upvoted 1 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
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.

SaveCancel
Loading ...