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
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.
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
$ 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]
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
}
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/
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
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
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
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.
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.
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.
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
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
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.
Nunyabiznes
Highly Voted 1Â year, 7Â months agochxnedu
Highly Voted 8Â months, 1Â week agoKaname93
Most Recent 9Â months agoNashP
9Â months, 3Â weeks agoTigerInTheCloud
11Â months, 1Â week agoBaburTurk
1Â year, 2Â months agokiran15789
1Â year, 6Â months agogspb
1Â year, 6Â months agogspb
1Â year, 6Â months agoFarziWaliMarzi
1Â year, 7Â months agoFliyd
1Â year, 7Â months agoDavid_C_90
1Â year, 8Â months agoDaro_
1Â year, 9Â months agolezgino
1Â year, 9Â months agoAbuu
1Â year, 10Â months agodinesh198728
2Â years, 2Â months ago[Removed]
2Â years, 2Â months agodepal_dhir
2Â years, 2Â months ago