Hi there! I have a question that basically boils d...
# typescript
a
Hi there! I have a question that basically boils down to how to go from something like
Output<string[]>
to
Output<string>[]
or
Output<Record<string, string>>
to
Record<string, Output<string>>
Why would I be looking to do this? Well, I’m trying to rehydrate an
awsx.ec2.Vpc
using
fromExistingIds
using a
StackReference
The example in the examples repo works because it only passes in the
vpcId
which is referenced as `networkingStack.getOutput("appVpcId")`(github link 🔗 ). This is an
Output<string>
which is happily accepted by
(property) ExistingVpcIdArgs.vpcId: pulumi.Input_<_string_>_
However, if I want to pass properties that expect an array (eg
(property) ExistingVpcIdArgs.natGatewayIds_?:_ pulumi.Input_<_string_>_[] _|_ undefined
) then the only way I can see to do this is:
Copy code
const vpcId: pulumi.Input<string> = sharedInfraStack.requireOutput("vpcId");
const internetGatewayId: pulumi.Input<string> = sharedInfraStack.requireOutput(
  "vpcInternetGatewayId"
);
const natGatewayId: pulumi.Input<string> =
  sharedInfraStack.requireOutput("vpcNatGatewayId");
const privateSubnetIdA: pulumi.Input<string> = sharedInfraStack.requireOutput(
  "vpcPrivateSubnetIdA"
);
const privateSubnetIdB: pulumi.Input<string> = sharedInfraStack.requireOutput(
  "vpcPrivateSubnetIdB"
);
const publicSubnetIdA: pulumi.Input<string> =
  sharedInfraStack.requireOutput("vpcPublicSubnetIdA");
const publicSubnetIdB: pulumi.Input<string> =
  sharedInfraStack.requireOutput("vpcPublicSubnetIdB");

awsx.ec2.Vpc.fromExistingIds("shared-vpc", {
  vpcId,
  internetGatewayId,
  natGatewayIds: [natGatewayId],
  privateSubnetIds: [privateSubnetIdA, privateSubnetIdB],
  publicSubnetIds: [publicSubnetIdA, publicSubnetIdB],
});
I’d love to be able to do something like this:
Copy code
awsx.ec2.Vpc.fromExistingIds("shared-vpc", sharedInfraStack.requireOutput("vpcConfig").apply(configAsStr => JSON.parse(configAsStr)))
I could also do this
Copy code
sharedInfraStack.requireOutput("vpcConfig").apply(configAsStr => 
    awsx.ec2.Vpc.fromExistingIds("shared-vpc", JSON.parse(configAsStr))
)
But then the rest of my application has to deal with
Output<awsx.ec2.Vpc>
instead of
awsx.ec2.Vpc
Anyone know of a neat way to deal with this?
m
I think I fell into a similar trap and ended up using
any
😖
I ended up abstracting this away from developers with a custom component.
Copy code
export interface ExistingVpcArgs extends BaseArgs {
  privateSubnetIds: Input<Input<string>[]>;
  publicSubnetIds: Input<Input<string>[]>;
  vpcId: Input<string>;
}

export class ExistingVpc extends BaseComponentResource {
  vpc: any;

  constructor(name: string, args: ExistingVpcArgs, opts?: ComponentResourceOptions) {
    super("shared-vpc", name, args, opts);

    this.vpc = all([args.publicSubnetIds, args.privateSubnetIds, args.vpcId]).apply(
      ([publicSubnetIds, privateSubnetIds, vpcId]) => {
        return awsx.ec2.Vpc.fromExistingIds(this.getName(), {
          vpcId,
          publicSubnetIds,
          privateSubnetIds,
        });
      },
    );
  }
}
I think I misunderstood your question initially, but this still might help you. I'm passing an array of subnet ids and using stackreference to get the outputs and apply them.