You can reference a resource created with for_each using a Splat (*) expression.
You can reference a resource created with for_each using a Splat (*) expression.
In Terraform, resources that use the for_each argument are represented as a map of objects rather than a list. Because the splat (*) expression is designed to work with lists, it cannot be used directly with resources created using for_each. Instead, you would need to use for expressions to iterate over the map to access individual resources.
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.
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
The answer is correct. 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. Source: https://www.terraform.io/language/expressions/splat
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
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/
*option B is the correct answer
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
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.
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 }
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.
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
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
https://www.terraform.io/language/expressions/splat
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.
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.
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
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
aws_instance.example[*].id this is valid
$ 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]