Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions writing-terraform-configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,22 @@ website = {
}
```
{% endcode %}

## Use `for_each` and `count` for different purposes

If you want to create multiple resources in a loop for each element in a list or map, always use `for_each`. It will save you if you want to remove some resources dependent on an element in the middle of a list.

```hcl
resource "aws_instance" "this" {
for_each = toset(var.availability_zones)
availability_zone = each.value
// ... other attributes
}
```
If you want to have resource only if a variable seted to true, use `count`
```hcl
resource "aws_instance" "vpn" {
count = var.use_vpn ? 1 : 0
// ... other attributes
}
```