For_each loop not working in a Terraform module

Happy Monday,

I’ve created a Terraform module that includes host, host set and target resources to help with the provisioning. Since some of the servers have more than 1 port I need access to, I used a map variable and for_each loop in the target resource block. Although the for_each loop doesn’t seem to be working since the Terraform only creates one target despite there are multiple ports specified.

# vars.tf

variable "name" {
  type        = string
  description = "Name of the resource"
}
variable "description" {
  type        = string
  description = "Description of the resource"
}
variable "host_catalog_id" {
  type        = string
  description = "Host catalog the resource belongs to"
}
variable "scope_id" {
  type        = string
  description = "Scope which the resource belongs to"
}
variable "address" {
  type        = string
  description = "Address of the resource"
}
variable "protocol" {
  type        = map(any)
  description = "Protocol used to connect to the target"
}

# main.tf

resource "boundary_host" "host" {
  type            = "static"
  name            = var.name
  description     = var.description
  address         = var.address
  host_catalog_id = var.host_catalog_id
}
resource "boundary_host_set" "host_set" {
  type            = "static"
  name            = var.name
  description     = var.description
  host_catalog_id = var.host_catalog_id
  host_ids = [
    boundary_host.host.id
  ]
}
resource "boundary_target" "target" {
  description              = var.description
  scope_id                 = var.scope_id
  session_connection_limit = -1
  for_each     = var.protocol
  name         = "${var.name}-${each.key}/${each.value}"
  type         = each.key
  default_port = each.value
  host_set_ids = [
    boundary_host_set.host_set.id
  ]
}

# Sample resource

module "stg-vaultsrv01" {
  source = "../modules/targets"
  name            = "STG-VAULTSRV01"
  description     = "Staging Hashicorp Vault server 01"
  address         = "192.168.192.14"
  host_catalog_id = boundary_host_catalog.stg-ct.id
  scope_id        = boundary_scope.proj-rm-ct.id
  protocol = {
    "tcp" = 22,
    "tcp" = 8200
  }
}

Issue found in the module definitions. I have multiple lines in the map that has the same key.

The key and value in protocol section should be like the one listed below.

protocol = {
    22 = "tcp",
    8200 = "tcp"
  }