How to loop list variable in a within a loop

Hello I am trying to create AWS VPC Endpoint using terraform.
I am using a yaml file as input file. I am trying to create AWS VPC ep, which needs subnets and security_groups as lists. I am trying below but its not working. Could you please suggest a better approach

This is my Yaml Input File

vpc_endpoint:
  prd:
    jira:
      service: XXXX
      type: Interface
      vpc: prd_vpc
      security_groups:
      - endpoint1
      subnet_ids:
      - prd_vpc_sub1
      - prd_vpc_sub2

This is my vars.tf file for EP Section

  vpc_eps = yamldecode(file("${var.config_yaml_file}"))["vpc_endpoint"]["${var.env}"]
  vpc_ep_info = flatten([
    for k,v in local.vpc_eps : {
      ep_name = k
      vpc_id  = v.vpc
      service_name = v.service
      vpc_endpoint_type = v.type
      security_group_ids = v.security_groups
      subnet_ids = v.subnet_ids
    }
  ])

I am trying this in vpc.tf file

resource "aws_vpc_endpoint" "aws_vpc_endpoint" {
  for_each = {
    for element in local.vpc_ep_info : element.ep_name => element
  }
  vpc_id            = aws_vpc.aws_vpc[each.value.vpc_id].id
  service_name      = each.value.service_name
  vpc_endpoint_type = each.value.vpc_endpoint_type
  security_group_ids =  formatlist("aws_security_group.aws_security_group[%s].id", each.value.security_group_ids)
  subnet_ids =  formatlist("aws_subnet.aws_subnet[%s].id", each.value.subnet_ids)
  private_dns_enabled = true
  tags = {
    Name        = each.value.ep_name
  }
}

But it is treating formatlist output as string, instead of object.
So “terraform plan” works, but not “terraform apply”
Pls suggest a better way of doing this.

You can’t compose a chunk of Terraform code using string manipulation functions, and then have it evaluated - Terraform just doesn’t support that at all.

You are looking for something more like:

  subnet_ids = [for i in each.value.subnet_ids: aws_subnet.aws_subnet[i].id]

Thank you. I have tried that, if I do that I am getting this error

Error: Invalid index

│ on terraform/aws_vpc.tf line 23, in resource “aws_vpc_endpoint” “aws_vpc_endpoint”:
│ 23: subnet_ids = [ for sub in each.value.subnet_ids: aws_subnet.aws_subnet[sub].id ]
│ ├────────────────
│ │ aws_subnet.aws_subnet is object with 2 attributes

│ The given key does not identify an element in this collection value.

Please ignore the above message. It’s working fine for me now. I had added vpc name to subnet while creating it and forgot to reference it here :slight_smile:

Hi @ae07177!

I’m glad you found a working solution. If you would be willing it would be helpful to share your final working solution here so that I’d somebody else finds this topic in future they may be able to use your example to answer their similar question.

Thanks!

Hello I was creating subnet as “vpc-name_subnet-name”. But while referencing the subnet id , was just using the “subnet-name” instead of prefixing it with vac-name. And then I changed the subnet_ids attribute as below to fix it.

subnet_ids = each.value.vpc_endpoint_type == “Interface” ? [ for sub in each.value.subnet_ids: aws_subnet.aws_subnet[join(“_”,[each.value.vpc_id, sub])].id ] :