Hello! The following problem has me stumped and I'...
# typescript
f
Hello! The following problem has me stumped and I'm hoping someone here can help? I suspect I'm missing something obvious... I'm trying to filter an array of Resources by matching an Output property on each element against an array of strings. In addition to the following snippet, I've tried wrapping the array as an Output (
pulumi.output(params.subnets).apply( s => s.filter(...))
), hoping that this filter function (https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/pulumi/#UnwrappedArray-filter) might resolve as intended.
Copy code
type CreateVPCEndpointParams = {
  vpc: aws.ec2.Vpc;
  subnets: aws.ec2.Subnet[];
  region: string;
  provider: aws.Provider;
};

export const createVPCEndpoint = (params: CreateVPCEndpointParams) => {
  const supported_azs = ['use1-az2', 'use1-az4', 'use1-az6'];
  
  // FIXME: This filtering doesn't work. I think it's because the predicate returns an OutputInstance which is always truthy; it's not resolving the wrapped boolean?
  const matching_subnets = params.subnets.filter((subnet) => {
    return subnet.availabilityZoneId.apply((azid) => supported_azs.includes(azid));
  });

  return new aws.ec2.VpcEndpoint(
    'elastic-co-endpoint',
    {
      vpcId: params.vpc.id,
      serviceName: 'redacted.vpce-svc',
      vpcEndpointType: 'Interface',
      subnetIds: matching_subnets.map((subnet) => subnet.id),
    },
    { provider: params.provider }
  );
}
Ahhh, a friend came to the rescue. In case it helps someone else:
Copy code
const subnets_output = pulumi.all(
    params.subnets.map((subnet) => {
      return { id: subnet.id, az_id: subnet.availabilityZoneId };
    })
  );
  const matching_subnet_ids = subnets_output.apply((subnets) => {
    return subnets.filter((subnet) => supported_azs.includes(subnet.az_id)).map((subnet) => subnet.id);
  });