How do I override values in map passed from tfvars in my locals?

Hello!

We’re using terraform public EKS repo GitHub - terraform-aws-modules/terraform-aws-eks: Terraform module to create an Elastic Kubernetes (EKS) cluster and associated worker instances on AWS, which currently doesn’t support specifying different AMI-IDs for different worker-node groups, so we do that instead from a TFVARS map-type variable.

worker_groups_launch_template = [
  {
    instance_type                = "m5a.large"
    asg_max_size                 = 5
    asg_min_size                 = 1
    tags = [
      {
        "key"                 = "tier"
        "propagate_at_launch" = "true"
        "value"               = "apps"
      }
    ]
  },
  {
    instance_type                = "m5a.large"
    asg_max_size                 = 5
    asg_min_size                 = 1
    tags = [
      {
        "key"                 = "tier"
        "propagate_at_launch" = "true"
        "value"               = "sharedservices"
      }
    ]
  },
  {
    instance_type                = "m5a.large"
    asg_max_size                 = 2
    asg_min_size                 = 1
    tags = [
      {
        "key"                 = "tier"
        "propagate_at_launch" = "true"
        "value"               = "ci"
      }
    ]
  },
  {
    instance_type                = "p2.xlarge"
    ami_id                     = "ami-0a8b33ec7a8b87a33"
    asg_max_size                 = 1
    asg_min_size                 = 0
    tags = [
      {
        "key"                 = "tier"
        "propagate_at_launch" = "true"
        "value"               = "nvidia"
      }
    ]
  },
]

However, this means that this AMI ID can get stale over time until someone commits a new AMI ID into the repo.

So, we’re trying to fetch the latest AMI ID in our child module and pass that into the map-variable via locals.

This is what I’ve tried:

  worker_groups_launch_template = flatten(
    [for i, item in var.worker_groups_launch_template: 
      {for key, value in item:
        key => "${key == "ami_id" ? "ami-0be42f7e2a0804716" : value}"
      }
    ]
  )

with the idea that I replace “ami-0be42f7e2a0804716” with a data source (like data.aws_ssm_parameter.current_ami.value) once I can prove it works.

This results in an error:

Error: Inconsistent conditional result types

  on local.tf line 82, in locals:
  82:         key => "${key == "ami_id" ? "ami-0be42f7e2a0804716" : value}"

The true and false result expressions must have consistent types. The given
expressions are string and object, respectively.

I realize this error is because not all key-values in my map-variable are strings, but I so wish TF would ignore that fact for keys != “ami_id”.

This is a bummer since I wanted to expand on this idea to override more values in the future.

Can someone help figure out how to override vars in this fashion? I’m afraid I’ve reached the limits of my TF skills here. Thanks!!