https://pulumi.com logo
Title
a

adamant-leather-41068

01/13/2023, 4:09 AM
I'm using
awsx.ec2.Vpc
to generate a VPC across two availability zones:
const vpc = new awsx.ec2.Vpc(`${shortName(nameConfig)}-vpc`, {
    availabilityZoneNames: [zoneName, albSecondZoneName],
    cidrBlock: '10.0.0.0/16',
    enableDnsHostnames: true,
    tags: { name: `${shortName(nameConfig)}-vpc` },
    subnetSpecs:[
      {
        type: awsx.ec2.SubnetType.Public,
        cidrMask: 22,
      },
      {
        type: awsx.ec2.SubnetType.Private,
        cidrMask: 20,
      },
    ],
  })
How do I get the public subnet for the availability zone
zoneName
to launch an EC2 instance in? This seems like it should be easy, but the examples all just choose the first public subnet ID (which isn't correct). There is
vpc.subnets
but checking
tagsAll
and
availabilityZone
inside
subnets.apply()
doesn't seem to work (it appears
tagsAll.apply()
and
availabilityZone.apply()
don't block?)
Hmm maybe I have it working. Surely there is a better way than this though?
let subnet = vpc.subnets.apply(subnets => {
    for (const sn of subnets) {
        
      let tagsCorrect = sn.tagsAll.apply(tags => {
        if (tags["SubnetType"] === "Public") {
          return true;
        } else {
          return false;
        }
      })

      let availabilityZoneCorrect = sn.availabilityZone.apply(az => {        
        if (az === zoneName) {
          return true;
        } else {
          return false;
        }
      })

      if (availabilityZoneCorrect && tagsCorrect) {
        return sn
      } else {
        console.log("Did not find subnet")
      }
    }
    return null;
  })
s

stocky-restaurant-98004

01/13/2023, 5:15 PM
There's a
publicSubnetIds
and
privateSubnetIds
output on the VPC component. I believe they'll get generated in order, so your first AZ supplied should be the first ID in each subnetIds.