Hi, I am trying to filter an array of public subne...
# typescript
b
Hi, I am trying to filter an array of public subnets during ec2 instance creation, but stuck with
pulumi.Output.apply()
function, in particular I am not sure how to work with
pulumu.Output<any>
during preview:
Copy code
const vpc = new awsx.ec2.Vpc("vpc", {
    cidrBlock: '10.0.0.0/16',
    enableDnsHostnames: true,
    numberOfAvailabilityZones: 1,
    subnets: [
        {
            type: "public",
            name: `${prefix}-app`,
            location: "10.0.1.0/26",
            tags: { 
              subnetType: "apps",  // <-- my custom tag I intent to filter by
              ...commonTags 
            }
        },
        {
            type: "public",
            name: `${prefix}-osb`,
            location: {
                cidrBlock: "10.0.20.0/26",
                availabilityZone: `${region}a`
            },
            tags: { subnetType: "osb", ...commonTags }
        }
    ],
    tags: { Name: "Collaboration Zone", ...commonTags }
})
Copy code
const instance = new aws.ec2.Instance("instance", {
    tags: { "Name": `${prefix}-instance`, ...commonTags },
    instanceType: 't2.large',
    vpcSecurityGroupIds: [sg.id],
    ami: ami,
    subnetId: vpc.publicSubnets // <-- awsx.ec2.Subnet
                .filter(subnet => filterSubnetByTag('apps', subnet))
                .map(subnet => subnet.id)[0],
    keyName: deployer.keyName
})
Copy code
function filterSubnetByTag(subnetTag: string, subnet: awsx.ec2.Subnet): boolean {

// subnet.subnet.tags is of the following type:
//     readonly tags: pulumi.Output<{
//        [key: string]: any;
//   } | undefined>;

// how do I filter pulumu.Output<any> during preview????

return !!(subnet.subnet.tags.apply(tag => tag && tag.subnetType && tag.subnetType === subnetTag))
}
s
@busy-magazine-48939 Simply use
tag["subnetType"] === subnetTag
. This kind of object is often called a hashmap, if you want to search for it 🙂 (https://stackoverflow.com/a/30019750/1168315). To be safe, you should also check if there are any tags at all.
Actually, it’s confusing that the value isn’t typed as
string
.