Fetch the count of ips in a list

Hi Team,
I have a requirement to fetch the count of ips from a list in a yaml file and use that details to dynamically create prefix list with the count value. Can anyone help me on that :pray:

My yaml file is a below

key1:
  - Cidr: value1
    Description: description1
  - Cidr: value2
    Description: description2
  - Cidr: value3
    Description: description3
key2:
  - Cidr: value1
    Description: description1
  - Cidr: value2
    Description: description2
  - Cidr: value3
    Description: description3
key3:
  - FromPort: port1
    ToPort: port2
    Protocol: tcp
    Description: description1
  - FromPort: port1
    ToPort: port2
    Protocol: tcp
    Description: description2
key4:
  - Cidr: value1
  - Cidr: value2
  - Cidr: value3
locals {
  yamlfile          = yamldecode(file("${path.module}/list.yaml"))
  keys              = compact([for keys in keys(local.yamlfile) : keys == "key3" || keys == "key4" ? "" : keys])
}

resource "aws_ec2_managed_prefix_list" "pl-rules" {
  for_each       = { for idx, rule_names in local.keys : lower(rule_names) => idx }
  name           = "pl-${each.key}"
  address_family = "IPv4"
  max_entries    = length("${each.key}")
}

Above code is not working as expected since it calculates the length of keys[key1,key2,key3] instead of local.key