https://pulumi.com logo
Title
f

fresh-wire-95028

10/19/2021, 8:32 PM
how do I use a
Promise
as an
input
in pulumi? Example I want to use:
get privateSubnetIds(): Promise<pulumi.Output<string>[]>;
from a `Vpc`:
const vpc = new awsx.ec2.Vpc(..., {
  ...
});
in a `NetworkAssociation`:
const networkAssociation = new aws.ec2clientvpn.NetworkAssociation(`...`, {
      subnetId: vpc.privateSubnetIds[0],
  });
l

little-cartoon-10569

10/19/2021, 8:33 PM
A Promise is an Input without doing anything. The issue here is probably that this Promise is wrapping another Output. What problem are you seeing, and what code is it happening in?
f

fresh-wire-95028

10/19/2021, 8:41 PM
yep that seems to be the case. How would I unwrap the Promise? The code sample is above. I'm creating a new VPC, and then trying to use its
privateSubnetIds[0]
as an input in a
clientvpn
NetworkAssociation
l

little-cartoon-10569

10/19/2021, 8:47 PM
There's a couple of ways. I recommend doing everything you can to avoid creating any resource in an
apply()
or
then()
, so let's do that.
The
subnetId
property can take an
Input<string>
, so we want a
Promise<string>
or
Output<string>
there.
vpc.privateSubnetIds.then(subnetIds => subnetIds[0].id)
returns a promise wrapping a string id.
1
Alternatively, you could convert the promise to an output and that might allow Pulumi to promote the id slightly more elegantly, though I'm not sure if the array would break the technique.
Something like
pulumi.output(vpc.privateSubnetIds)[0].id
maybe? Haven't tried that.
This would definitely work:
pulumi.output(vpc.privateSubnetIds[0]).id
. I think it looks better than the
then()
version above.
1
f

fresh-wire-95028

10/19/2021, 10:02 PM
yup! That more or less ended up working!!
const defaultPrivateSubnetId = pulumi
      .output(vpc.privateSubnetIds)
      .apply((ids) => ids[0]);

    const defaultPrivateSubnetCidrBlock = pulumi
      .output(vpc.privateSubnets)
      .apply((subnet) => subnet[0].subnet.cidrBlock);

 const networkAssociation = new aws.ec2clientvpn.NetworkAssociation(
      `${name}-network-association`,
      {
        clientVpnEndpointId: endpoint.id,
        subnetId: defaultPrivateSubnetId,
      },
    );

    // allows the following subnet groups to access these subnets
    const authorizationRule = new aws.ec2clientvpn.AuthorizationRule(
      `${name}-authorization-rule`,
      {
        clientVpnEndpointId: endpoint.id,
        targetNetworkCidr: defaultPrivateSubnetCidrBlock,
        authorizeAllGroups: true,
      },
    );
👍 1
l

little-cartoon-10569

10/19/2021, 10:39 PM
Do you need this?
const defaultPrivateSubnetId = pulumi.output(vpc.privateSubnetIds).apply((ids) => ids[0]);
If you do it inline (i.e., eliminate the
defaultPrivateSubnetId
variable), then I think that Pulumi's output promotion will handle it for you.
subnetId: pulumi.output(vpc.privateSubnetIds)[0]
Hopefully awsx will return an output version of all those promises, at some point in the future.
f

fresh-wire-95028

10/20/2021, 7:09 PM
yeah! that worked 🙂 thanks!
👍 1