Loop for_each Lightsail public_ports

Hi fellow Terraformers.

I’m new and having difficulty with for_each and this Lightsail aws_lightsail_public_ports resource. I’m able to use it statically without a loop with no problem. However, when I go to add a for_each = local.instances under aws_lightsail_public_ports and what I think should be the right map key instance_name = "${each.key}", I get this error:

“the instance does not exist: vm01”

I’ve tried many variations of this with no luck.

Any help would be appreciated.

locals {
instances = {
“vm01” = { bundle_id = var.bundle, availability_zone = var.zone , blueprint_id = var.blueprint },
“vm02” = { bundle_id = var.bundle, availability_zone = var.zone , blueprint_id = var.blueprint },

resource “aws_lightsail_instance” “instances” {
for_each = local.instances
bundle_id = each.value.bundle
availability_zone = each.value. zone
blueprint_id = each.value.blueprint
name = “${each.key}”

resource “aws_lightsail_instance_public_ports” “test” {
instance_name = aws_lightsail_instance.test.name
port_info {
from_port = 0
to_port = 100
protocol = “tcp”
cidrs = [“1.1.1.1/24”]
}
}