Terraform - Define dictionary using variable map

by
Jeremy Canfield |
Updated: November 06 2023
| Terraform articles
A dictionary (key value pairs) can be defined in
- locals
- variable map (this article)
For example, let's say you have the following files on your Terraform server.
├── required_provider.tf
├── child (directory, child module)
│ ├── data.tf
│ ├── outputs.tf
│ ├── resources.tf
│ ├── variables.tf
Let's say variables.tf file has the following.
variable "protocols_and_ports_map" {
type = map(string)
default = {
HTTP = "80"
HTTPS = "443"
}
}
Here is an example of how you use loop through the dictionary in a resource.
resource "aws_lb_target_group" "target_groups" {
for_each = var.protocols_and_ports_map
name = each.key
port = each.value
protocol = each.key
vpc_id = data.aws_vpc.default_vpc.id
tags = {
Name = each.key
}
}
Did you find this article helpful?
If so, consider buying me a coffee over at