Split a list and merge it based in common variable in terraform

706 views Asked by At

I want to retrieve the subnet ids using it's name and then club them based on their VPC id.

I am using data source to retrieve the subnet ID from it's name where it's being split. Once the subnet IDs are retrieved, how can I make a list of subnets for each VPC. Below is my code. Please check and help.

resource.tf

resource "aws_ec2_transit_gateway_vpc_attachment" "this" {
 
  transit_gateway_id = var.transit_gateway_id
  vpc_id = var.vpc_id
  subnet_ids = var.subnet_ids

  dns_support                                     = "disable"
  ipv6_support                                    = "disable"
  transit_gateway_default_route_table_association = false
  transit_gateway_default_route_table_propagation = false

}

module.tf

I want to get the list of subnet ids so I can give in subnet_ids.

module "tgw" {
  source = "./aws_tgw"
  count = var.accounts[0].vpc_names != [""] ? length(var.accounts[0].vpc_names) : 0
  transit_gateway_id = aws_ec2_transit_gateway.this.id
  transit_gateway_arn = aws_ec2_transit_gateway.this.arn
  vpc_id = data.aws_vpc.vpc0[count.index].id
  vpc_name = var.accounts[0].vpc_subnets[count.index].vpc_name
  subnet_ids = data.aws_subnet.subnet0[var.accounts[0].vpc_subnets[count.index].vpc_name].id
  destination_cidr_block = var.destination_cidr_block_route
}

vars.tf

variable "accounts" {
  type = list(object({
    account_id = string
    vpc_names    = list(string)
    vpc_subnets = list(object({
        vpc_name = string
        subnet_names = list(string)
  })
  )
    asn = string
  }))
}

data.tf

data "aws_vpc" "vpc0" {
  count = length(var.accounts[0].vpc_names)
  filter {
    name = "tag:Name"
    values = [format("%s",var.accounts[0].vpc_names[count.index])]
  }
}
data "aws_subnet" "subnet0" {
  count = length(local.account0_subnet_list)
  filter {
    name = "tag:Name"
    values = [format("%s-%s",local.account0_subnet_list[count.index].vpc_name,local.account0_subnet_list[count.index].subnet)]
  }
}

I also tried with this but it says snet is a tuple with 2 strings

data "aws_subnet" "subnet0" {
  for_each = { for vpc in var.accounts[0].vpc_subnets : vpc.vpc_name => [ for snet in vpc.subnet_names : { vpc = vpc.vpc_name, subnet = snet } ] }
    filter {
    name = "tag:Name"
    values = [format("%s-%s",each.value.vpc,each.value.subnet)]
  }
}

locals.tf

locals {
  account0_subnet_list = flatten([ for vpc in var.accounts[0].vpc_subnets : [ for snet in vpc.subnet_names : { vpc_name = vpc.vpc_name, subnet = snet } ]] )
}

tfvars file

accounts = [
  {
    "account_id": "256869728636",
    "asn": "64513",
    "vpc_names": [
      "POC-Management1","POC-Management2"
    ],
    "vpc_subnets": [
      {
        "subnet_names": [
          "PrivateSubnet-1A-us-west-1b","PrivateSubnet-2B-us-west-1c"
        ],
        "vpc_name": "POC-Management1"
      },
      {
        "subnet_names": [
          "PublicSubnet-1A-us-west-1b","PublicSubnet-2A-us-west-1c"
        ],
        "vpc_name": "POC-Management2"
      }
    ]
  }
1

There are 1 answers

0
Abhishek Solanki On BEST ANSWER

define the data.tf in resource level instead of module level, that way you can achieve what you want instead of splitting the subnet_ids.