Extract objects without for_each

I need community help)

I have a var with output like below

private_subnets                                      = {
      + eu-west-1a = [
          + "subnet-0b53fe5230ebe5c78",
      + eu-west-1b = [
          + "subnet-0eab295109e89e12a",
      + eu-west-1c = [
          + "subnet-02124519fb318a211",

How can I extract values (subnet id) by providing a key (availability zone)?
Also my part of code

module "ec2_instance" {
  source  = "terraform-aws-modules/ec2-instance/aws"
  version = "~> 3.0"

  count                  = length(local.azs)
  name                   = "${local.server_name}-${count.index +1}"
  subnet_id              =  element(local.az_subnet_ids[local.azs], count.index)
 Error: Invalid index
β”‚   on runner.tf line 104, in module "ec2_instance":
β”‚  104:   subnet_id              = element(local.az_subnet_ids[local.azs], count.index)
β”‚     β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     β”‚ local.az_subnet_ids is object with 3 attributes
β”‚     β”‚ local.azs is tuple with 3 elements
β”‚ The given key does not identify an element in this collection value: string required.

You can’t use a tuple (your local.azs) to do a lookup in an object (your local.az_subnet_ids).

I’m not sure what are in both those local variables, as the code doesn’t match the above bit and nothing shows local.azs, but assuming az_subnet_ids is the map you showed above (called private_subnets) and azs is a list of the different availability zones you’d just want

subnet_id = local.az_subnet_ids[count.index]
1 Like