https://pulumi.com logo
Title
t

tall-beard-99429

10/26/2021, 4:08 PM
What's the best way to get the first public subnet ID for a VPC network?
b

billowy-army-68599

10/26/2021, 4:23 PM
depends how you define "first" ? do you mean the first AZ?
t

tall-beard-99429

10/26/2021, 4:27 PM
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

millions-furniture-75402

10/26/2021, 5:21 PM
This might help you:
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,
};
const publicSubnets = await aws.ec2.getSubnetIds({
      vpcId: vpcId,
      filters: [{ name: 'tag:type', values: ['public'] }],
    });

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