1

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 async function. My problem is with exported variables at the end of the file.

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,
  };
}

I want to export these 4 values. I can't understand how, but the code that follows exports( at least, pulumi up shows the values at the end of execution).

const result = go();
export const dnsZoneName = result.then((res) => res.dnsZoneName);

look at this

I think I can't use top-level-await.

What is the clear solution ?

Tigran Sahakyan
  • 363
  • 1
  • 5
  • 10

4 Answers4

4

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,
  };
}
JTango18
  • 633
  • 6
  • 11
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.

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,
  };
}

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).
});
export const dnsZoneName = goPromise.then(res => res.DNSZone.name);
export const BucketID = goPromise.then(res => res.Bucket.id);
export const dbHardURL = goPromise.then(res => res.DBHost.publicDns);
export const 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;
// ...
export const dnsZoneName = DNSZone.name;
export const BucketID = Bucket.id;
export const dbHardURL = DBHost.publicDns;
export const 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).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • `You've said you don't think you can use top-level await, but you haven't said why.` I am not sure how to pass NodeJS arguments to Pulumi, with my current setup it gives an error. I found [this](https://github.com/pulumi/pulumi/issues/3061) and it is still open. – Tigran Sahakyan Jun 27 '20 at 11:57
  • @TigranSahakyan - Sorry, I don't see what that has to do with top-level `await`...? – T.J. Crowder Jun 27 '20 at 12:10
  • I mean, I am running the script with `pulumi up` and I don't know how to pass NodeJS argument. – Tigran Sahakyan Jun 27 '20 at 12:37
1

The most straight-forward way I found is to map getCallerIdentity to an Output this way

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const account = aws.getCallerIdentity({/* async = true (by default) */});
const bucket = new aws.s3.Bucket("bucket", {
    bucket: pulumi.output(account).apply(acc => `my-bucket-${acc.accountId}`),
});

This way you don't have to wrap all your code in a top-level async function, which is somewhat undesirable if you need it only to get the result of that function.

futtetennista
  • 1,876
  • 1
  • 20
  • 33
-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

Gabriel Taype
  • 192
  • 3
  • 6