https://pulumi.com logo
#python
Title
# python
h

hallowed-fireman-90476

01/03/2024, 6:56 PM
I am not sure what I am missing; trying to get the private route table IDs from the vpc instead of doing separate AWS get, but the match condition is not working. Thanks for feedback/suggestion Not Working
Copy code
mws_vpc = awsx.ec2.Vpc()
            private_route_table_details = mws_vpc.route_tables.apply(
                lambda rts: [
                    rt.id
                    for rt in rts
                    if rt.tags and rt.tags.get("SubnetType") == "Private"
                ]
            )
            export("private_route_table_details", private_route_table_details)
Working using get, which i am trying to avoid
Copy code
# Option2: Use the get_route_tables function to retrieve all route tables for the specified VPC
            # route_tables = aws.ec2.get_route_tables(
            #     filters=[
            #         aws.ec2.GetRouteTablesFilterArgs(
            #             name="vpc-id", values=[mws_vpc.vpc_id]
            #         ),
            #         # Filter to include only non-main association route tables, which are considered private
            #         aws.ec2.GetRouteTablesFilterArgs(
            #             name="tag:SubnetType", values=["Private"]
            #         ),
            #     ]
            # )
            # private_route_table_ids = []
            # for i in route_tables.ids:
            #     private_route_table_ids.append(i)
Able to figure it
Copy code
private_route_table_details = mws_vpc.route_tables.apply(
                lambda route_tables: [
                    private_id
                    for route in route_tables
                    if (
                        private_id := route.tags.apply(
                            lambda tag: route.id
                            if tag["SubnetType"] == "Private"
                            else None
                        )
                    )
                ],
            )
But list contain <null> which i cannot remove it with None or "null", "" check
g

green-stone-37839

01/04/2024, 6:47 PM
I've arrived at the same conclusion as you. I'm not able to properly filter out the null values but am checking into that one.
h

hallowed-fireman-90476

01/05/2024, 2:42 PM
@green-stone-37839 any luck
g

green-stone-37839

01/05/2024, 2:51 PM
Yes actually. I'll need a couple of hours (early AM for me) but I'll drop a solution in a bit.
This solution is typescript based (my python is rusty). Hopefully this gives you an idea of what is needed to get want you want. Essentially two steps: 1. unwrap the tags from the route table 2. filter based on needed tags
Copy code
function GetPrivateVpcs(rts: aws.ec2.RouteTable[]): pulumi.Output<aws.ec2.RouteTable | undefined>[] {
    const privateRts: pulumi.Output<aws.ec2.RouteTable | undefined>[] = [];

    for (const rt of rts) {
        const localRt = rt.tags.apply(tag => {

            if (tag!["SubnetType"] == "Private") {
                console.log("Private");
                console.log(`length: ${privateRts.length}`);
                return rt;
            }

            return undefined;
        });

        privateRts.push(localRt);
    }

    return privateRts;
}

const vpc = new awsx.ec2.Vpc("vpc", {
    cidrBlock: "10.100.0.0/16"
});

export const privateRts = vpc.routeTables.apply(GetPrivateVpcs);
This is the working function. Replace GetPrivateVpcs above with the below
Copy code
function GetPrivateVpcs(
  rts: aws.ec2.RouteTable[]
): pulumi.Output<aws.ec2.RouteTable[]> {
  // Resolve all outputs first, then filter within the apply.
  return pulumi
    .all(
      rts.map((rt) =>
        rt.tags.apply((tags) => ({
          rt,
          tags,
        }))
      )
    )
    .apply((unwrapped) => {
      const privateRts: aws.ec2.RouteTable[] = [];

      for (const rt of unwrapped) {
        if (rt.tags!["SubnetType"] == "Private") {
          console.log("Private");
          console.log(`length: ${privateRts.length}`);
          privateRts.push(rt.rt);
        }
      }

      return privateRts;
    });
}