What's the best way to get the first public subnet...
# aws
t
What's the best way to get the first public subnet ID for a VPC network?
b
depends how you define "first" ? do you mean the first AZ?
t
Well, more so any public subnet ID
I have a VPC that has 3 public subnets, I need to use a subnet ID for a network association on the client VPN
network.publicSubnetIds.then(subnets => subnets[0].apply(subnet => subnet)),
thats my latest attempt, but it doesn't work
m
This might help you:
Copy code
import * as awsx from "@pulumi/awsx";

const vpc = new awsx.ec2.Vpc("test-vpc", {});

const ue1aPubSub = vpc.publicSubnets.then((a) => {
  return a
    .filter((s) => s.subnet.availabilityZone.apply((az) => az === "us-east-1a"))
    .pop()?.id;
});

export const out = {
  vpcId: vpc.id,
  vpcPubSubIds: vpc.publicSubnetIds,
  vpcPrivSubIds: vpc.privateSubnetIds,
  vpcDataSubIds: vpc.isolatedSubnetIds,
  vpcPubSubCidrs: vpc.publicSubnets.then((a) =>
    a.map((s) => s.subnet.cidrBlock)
  ),
  vpcPrivSubCidrs: vpc.privateSubnets.then((a) =>
    a.map((s) => s.subnet.cidrBlock)
  ),
  vpcDataSubCidrs: vpc.isolatedSubnets.then((a) =>
    a.map((s) => s.subnet.cidrBlock)
  ),
  usEast1aPubSubnet: ue1aPubSub,
};
Copy code
const publicSubnets = await aws.ec2.getSubnetIds({
      vpcId: vpcId,
      filters: [{ name: 'tag:type', values: ['public'] }],
    });

console.log(publicSubnets[0]) // first