Skip to content Skip to sidebar Skip to footer

Pulumi, How To Export Values Coming From Async Function?

In my Pulumi project, in the index.ts file I have to call const awsIdentity = await aws.getCallerIdentity({ async: true }); So for this reason I have to wrapp all my code into asyn

Solution 1:

From the issue you linked, it seems like my first suggestion in my answer to your previous question should work: Export a promise for each value. Based on the issue comments, it looks like Pulumi understands exported promises.

asyncfunctiongo() {
...
  const awsIdentity = await aws.getCallerIdentity({ async: true });
  const accountId = awsIdentity.accountId;
...
  return {
    dnsZoneName: DNSZone.name,
    BucketID: Bucket.id,
    dbHardURL: DBHost.publicDns,
    devDbURL: publicDbAddress.fqdn,
  };
}

const goPromise = go();
goPromise.catch(error => {
    // Report the error. Note that since we don't chain on this, it doesn't// prevent the exports below from rejecting (so Pulumi will see the error too,// which seems best).
});
exportconst dnsZoneName = goPromise.then(res => res.DNSZone.name);
exportconstBucketID = goPromise.then(res => res.Bucket.id);
exportconst dbHardURL = goPromise.then(res => res.DBHost.publicDns);
exportconst devDbURL = goPromise.then(res => res.publicDbAddress.fqdn);

Otherwise:

You've said you don't think you can use top-level await, but you haven't said why.

In case it's just that you're having trouble figuring out how to use it, you'd do it like this provided aws.getCallerIdentity and whatever's in the "..." of your code example provide promises:

const awsIdentity = await aws.getCallerIdentity({ async: true });
const accountId = awsIdentity.accountId;
// ...exportconst dnsZoneName = DNSZone.name;
exportconstBucketID = Bucket.id;
exportconst dbHardURL = DBHost.publicDns;
exportconst devDbURL = publicDbAddress.fqdn;

Or if you need to export an object with those as properties as a default export:

const awsIdentity = await aws.getCallerIdentity({ async: true });
const accountId = awsIdentity.accountId;
// ...
export default {
    dnsZoneName: DNSZone.name
    BucketID: Bucket.id
    dbHardURL: DBHost.publicDns
    devDbURL: publicDbAddress.fqdn
};

Note that in both cases, the code isn't inside any function, that's at the top-level of your module.

With Node.js v13 and v14 (so far) you need the --harmony-top-level-await runtime flag. My guess is that it won't be behind a flag in v15 (or possibly even just a later version of v14).

Solution 2:

For anyone who comes across this post, asynchronous entry points are now a first class citizen in Pulumi, as detailed Here

You can have code that looks similar to this and Pulumi will automatically resolve everything for you, no need to do anything more for exporting Outputs

export async function go() {
...
  const awsIdentity = await aws.getCallerIdentity({ async: true });
  const accountId = awsIdentity.accountId;
...
  return {
    dnsZoneName: DNSZone.name,
    BucketID: Bucket.id,
    dbHardURL: DBHost.publicDns,
    devDbURL: publicDbAddress.fqdn,
  };
}

Solution 3:

You are working with promises. Therefore, the answer can come at any time. What you should do is that when you call the function you do it with await to wait for the function response

Post a Comment for "Pulumi, How To Export Values Coming From Async Function?"