title: How to create multiple instances with multiple subnet ids using terraform?
body: <p>I have 2 services test1,test2 and for each service i have to create 6 vm's.This 6 vm's should be placed in 3 subnet id's which created in 3 different zones in a same region</p>
  <p>In this services,test1 will be in private subnets and test2 will be in public subnets.So i have to pass that correct subnet id when creating ec2 instances</p>
  <h1>root module:</h1>
  <pre><code>provider &quot;aws&quot; {
    region = var.region
  }
  
  module &quot;ecom-vpc&quot; {
    source = &quot;./modules/vpc&quot;
  }
  
  module &quot;ecom-public-subnet&quot; {
    source = &quot;./modules/subnets/public&quot;
    vpc-id = module.ecom-vpc.vpc-id
  }
  
  module &quot;ecom-private-subnet&quot; {
    source = &quot;./modules/subnets/private&quot;
    vpc-id = module.ecom-vpc.vpc-id
  }
  
  module &quot;ecom-instances-sg&quot; {
    source = &quot;./modules/sg&quot;
    vpc-id = module.ecom-vpc.vpc-id
  }
  
  module &quot;ecom-vm-instances&quot; {
    source = &quot;./modules/vm&quot;
    priv-subnet-ids = module.ecom-private-subnet.ecom_private_subnets
    pub-subnet-ids = module.ecom-public-subnet.ecom_public_subnets
    instances-sg = module.ecom-instances-sg.ecom-inst-sg
  }
  </code></pre>
  <h1>From child modules - vpc,subnets,ec2</h1>
  <pre><code>variable &quot;service-names&quot; {
    type    = list(any)
    default = [&quot;ecom-app-TEST1&quot;,&quot;ecom-app-TEST2&quot;]
  
  }
  
  variable &quot;availability_zones&quot; {
    type = map
    default = {
      ap-south-1a = {
        private_subnet = &quot;10.0.1.0/24&quot;
        public_subnet  = &quot;10.0.4.0/24&quot;
      }
      ap-south-1b = {
        private_subnet = &quot;10.0.2.0/24&quot;
        public_subnet  = &quot;10.0.5.0/24&quot;
      }
      ap-south-1c = {
        private_subnet = &quot;10.0.3.0/24&quot;
        public_subnet  = &quot;10.0.6.0/24&quot;
      }
        }
    }
  
  resource &quot;aws_vpc&quot; &quot;ecom-vpc&quot; {
    cidr_block = var.ecom-cidr
  }
  
  output &quot;vpc-id&quot; {
    value = aws_vpc.ecom-vpc.id
  }
  
  resource &quot;aws_subnet&quot; &quot;ecom-private&quot; {
    for_each = var.availability_zones
    vpc_id                  = var.vpc-id
    cidr_block              = each.value.private_subnet
    availability_zone       = each.key
    map_public_ip_on_launch = false
    tags = {
      Name = &quot;${split(&quot;-&quot;, each.key)[2]}&quot;
      Subnet_Type = &quot;private&quot;
    }
  }
  
  output &quot;ecom_private_subnets&quot; {
    value = aws_subnet.ecom-private
  }
  
  resource &quot;aws_subnet&quot; &quot;ecom-public&quot; {
    for_each = var.availability_zones
    vpc_id                  = var.vpc-id
    cidr_block              = each.value.public_subnet
    availability_zone       = each.key
    map_public_ip_on_launch = true
    tags = {
      Name = &quot;${split(&quot;-&quot;, each.key)[2]}&quot;
      Subnet_Type = &quot;public&quot;
    }
    depends_on = [aws_internet_gateway.igw
    ]
  }
  
    
  output &quot;ecom_public_subnets&quot; {
    value = aws_subnet.ecom-public
  }
  </code></pre>
  <p>I'm trying to achieve the same by creating a locals which combines service names,priv,public subnet id's,instance count(2).But the problem is i'm not able to make it because i'm not able to create a unique combination of keys</p>
  <pre><code>locals {
   service_subnets = {
      for pair in setproduct(var.service-names, values(var.priv-subnet-ids),values(var.pub-subnet-ids),range(var.instance_count)) :
      &quot;${pair[0]}:${pair[1].availability_zone}-${pair[3]}&quot; =&gt; {
        service_name = pair[0]
        priv-subnet       = pair[1]
        pub-subnet = pair[2]
      }
    }
  }
  
  
   resource &quot;aws_instance&quot; &quot;ecom-instances&quot; {
    for_each = local.service_subnets
    ami           = data.aws_ami.ecom.id
    instance_type = &quot;t3.micro&quot;
    tags = {
      Name = each.value.service_name
      Service = each.value.service_name
    }
  
    vpc_security_group_ids = [var.instances-sg[each.value.service_name].id]
    subnet_id  = &quot;${split(&quot;-&quot;, each.value.service_name)[2] == &quot;TEST1&quot; ? each.value.pub-subnet.id  : each.value.priv-subnet.id }&quot;
  }
  </code></pre>
  <p>I'm getting the below error.</p>
  <pre><code>Two different items produced the key &quot;ecom-app-TEST1:ap-south-1c-1&quot; in this 'for' expression. If duplicates are expected, use the ellipsis (...)
  │ after the value expression to enable grouping by key.
  </code></pre>
  <p>If i add ... and change it as below then it is converted as tuple and i'm not sure how to get and pass the value from each.value in the aws_instance resource</p>
  <pre><code>   locals {
       service_subnets = {
          for pair in setproduct(var.service-names, values(var.priv-subnet-ids),values(var.pub-subnet-ids),range(var.instance_count)) :
          &quot;${pair[0]}:${pair[1].availability_zone}-${pair[3]}&quot; =&gt; {
            service_name = pair[0]
            priv-subnet       = pair[1]
            pub-subnet = pair[2]
          }
       ... }
      }
  
  on modules/vm/main.tf line 60, in resource &quot;aws_instance&quot; &quot;ecom-instances&quot;:
  │   60:   subnet_id  = &quot;${split(&quot;-&quot;, each.value.service_name)[2] == &quot;TEST1&quot; ? each.value.pub-subnet.id  : each.value.priv-subnet.id }&quot;
  │     ├────────────────
  │     │ each.value is tuple with 3 elements
  │ 
  │ This value does not have any attributes.
  </code></pre>
  <p>Please Guide me</p>