Skip to content

Commit

Permalink
backport of commit 6f2f4a5
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianMMcClain committed Mar 27, 2024
1 parent 58d284c commit 104fb9c
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion website/docs/language/style.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,60 @@ module "aws_vpc" {

The `for_each` and `count` meta-arguments let you create multiple resources from a single `resource` block depending on run-time conditions. You can use these meta-arguments to make your code flexible and reduce duplicate resource blocks. If the resources are almost identical, use `count`. If some of arguments need distinct values that you cannot derive from an integer, use `for_each`.

The `for_each` meta-argument accepts a `map` or `set` value, and Terraform will create an instance of that resource for each element in the value you provide. Refer to the [for_each meta-argument documentation](/terraform/language/meta-arguments/for_each) for examples.
The `for_each` meta-argument accepts a `map` or `set` value, and Terraform will create an instance of that resource for each element in the value you provide. In the following example, Terraform creates an `aws_instance` for each of the strings defined in the `web_instances` variable: "ui", "api", "db" and "metrics". The example uses `each.key` to give each instance a unique name. The `web_private_ips` output uses a [for expression](https://developer.hashicorp.com/terraform/language/expressions/for) to create a map of instance names and their private IP addresses, while the `web_ui_public_ip` output addresses the instance with the key "ui" directly.

<CodeBlockConfig hideClipboard>

```hcl
variable "web_instances" {
type = list(string)
description = "A list of instances for the web application"
default = [
"ui",
"api",
"db",
"metrics"
]
}
resource "aws_instance" "web" {
for_each = toset(var.web_instances)
ami = data.aws_ami.webapp.id
instance_type = "t3.micro"
tags = {
Name = "web_${each.key}"
}
}
output "web_private_ips" {
description = "Private IPs of the web instances"
value = {
for k, v in aws_instance.web : k => v.private_ip
}
}
output "web_ui_public_ip" {
description = "Public IP of the web UI instance"
value = aws_instance.web["ui"].public_ip
}
```

</CodeBlockConfig>

The above example will create the following output:

<CodeBlockConfig hideClipboard>

```
web_private_ips = {
"api" = "172.31.25.29"
"db" = "172.31.18.33"
"metrics" = "172.31.26.112"
"ui" = "172.31.20.142"
}
web_ui_public_ip = "18.216.208.182"
```

</CodeBlockConfig>

Refer to the [for_each meta-argument documentation](/terraform/language/meta-arguments/for_each) for more examples.

The `count` meta-argument lets you create multiple instances of a resource from a single resource block. Refer to the [count meta-argument documentation](/terraform/language/meta-arguments/count) for examples.

Expand Down

0 comments on commit 104fb9c

Please sign in to comment.