how do I use a `Promise` as an `input` in pulumi? ...
# general
f
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`:
Copy code
const vpc = new awsx.ec2.Vpc(..., {
  ...
});
in a `NetworkAssociation`:
Copy code
const networkAssociation = new aws.ec2clientvpn.NetworkAssociation(`...`, {
      subnetId: vpc.privateSubnetIds[0],
  });
l
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
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
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
yup! That more or less ended up working!!
Copy code
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
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
yeah! that worked 🙂 thanks!
👍 1
708 Views