0
votes

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

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

root module:

provider "aws" {
  region = var.region
}

module "ecom-vpc" {
  source = "./modules/vpc"
}

module "ecom-public-subnet" {
  source = "./modules/subnets/public"
  vpc-id = module.ecom-vpc.vpc-id
}

module "ecom-private-subnet" {
  source = "./modules/subnets/private"
  vpc-id = module.ecom-vpc.vpc-id
}

module "ecom-instances-sg" {
  source = "./modules/sg"
  vpc-id = module.ecom-vpc.vpc-id
}

module "ecom-vm-instances" {
  source = "./modules/vm"
  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
}

From child modules - vpc,subnets,ec2

variable "service-names" {
  type    = list(any)
  default = ["ecom-app-TEST1","ecom-app-TEST2"]

}

variable "availability_zones" {
  type = map
  default = {
    ap-south-1a = {
      private_subnet = "10.0.1.0/24"
      public_subnet  = "10.0.4.0/24"
    }
    ap-south-1b = {
      private_subnet = "10.0.2.0/24"
      public_subnet  = "10.0.5.0/24"
    }
    ap-south-1c = {
      private_subnet = "10.0.3.0/24"
      public_subnet  = "10.0.6.0/24"
    }
      }
  }

resource "aws_vpc" "ecom-vpc" {
  cidr_block = var.ecom-cidr
}

output "vpc-id" {
  value = aws_vpc.ecom-vpc.id
}

resource "aws_subnet" "ecom-private" {
  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 = "${split("-", each.key)[2]}"
    Subnet_Type = "private"
  }
}

output "ecom_private_subnets" {
  value = aws_subnet.ecom-private
}

resource "aws_subnet" "ecom-public" {
  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 = "${split("-", each.key)[2]}"
    Subnet_Type = "public"
  }
  depends_on = [aws_internet_gateway.igw
  ]
}

  
output "ecom_public_subnets" {
  value = aws_subnet.ecom-public
}

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

locals {
 service_subnets = {
    for pair in setproduct(var.service-names, values(var.priv-subnet-ids),values(var.pub-subnet-ids),range(var.instance_count)) :
    "${pair[0]}:${pair[1].availability_zone}-${pair[3]}" => {
      service_name = pair[0]
      priv-subnet       = pair[1]
      pub-subnet = pair[2]
    }
  }
}


 resource "aws_instance" "ecom-instances" {
  for_each = local.service_subnets
  ami           = data.aws_ami.ecom.id
  instance_type = "t3.micro"
  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  = "${split("-", each.value.service_name)[2] == "TEST1" ? each.value.pub-subnet.id  : each.value.priv-subnet.id }"
}

I'm getting the below error.

Two different items produced the key "ecom-app-TEST1:ap-south-1c-1" in this 'for' expression. If duplicates are expected, use the ellipsis (...)
│ after the value expression to enable grouping by key.

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

   locals {
     service_subnets = {
        for pair in setproduct(var.service-names, values(var.priv-subnet-ids),values(var.pub-subnet-ids),range(var.instance_count)) :
        "${pair[0]}:${pair[1].availability_zone}-${pair[3]}" => {
          service_name = pair[0]
          priv-subnet       = pair[1]
          pub-subnet = pair[2]
        }
     ... }
    }

on modules/vm/main.tf line 60, in resource "aws_instance" "ecom-instances":
│   60:   subnet_id  = "${split("-", each.value.service_name)[2] == "TEST1" ? each.value.pub-subnet.id  : each.value.priv-subnet.id }"
│     ├────────────────
│     │ each.value is tuple with 3 elements
│ 
│ This value does not have any attributes.

Please Guide me

1
To help people help you, perhaps you should provide the definitions of the variables you use in the definition of the local (var.priv-subnet-ids, etc...).RafaP
@RafaP everything is separate module.Thats why i'm unable to give details.Now i added root,child module code also.Will pass the output from each moduleJPNagarajan
Could you please suggestJPNagarajan
I'm afraid pasting lots of code does not help (at least it doesn't help me, I don't have time to do your job...) What I am suggesting you do is simply use the variable definitions of var.priv-subnet-ids and var.pub-subnet-ids, plug them into the expression that gives you the error and do some debugging; if still not working, ask againRafaP

1 Answers

0
votes

You could try adding the index to your for loop and making it part of your name, might help you avoid elipsis/tuple conversion.

locals {
 service_subnets = {
    for index, pair in setproduct(var.service-names, values(var.priv-subnet-ids),values(var.pub-subnet-ids),range(var.instance_count)) :
    "${pair[0]}:${pair[1].availability_zone}-${pair[3]}=${index}" => {
      service_name = pair[0]
      priv-subnet       = pair[1]
      pub-subnet = pair[2]
    }
  }
}