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 ?
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 ?
Share Improve this question asked Jun 27, 2020 at 10:25 Tigran SahakyanTigran Sahakyan 4251 gold badge6 silver badges10 bronze badges 1- "I think I can't use top-level-await." Why not? Is there some Pulumi issue? – T.J. Crowder Commented Jun 27, 2020 at 11:08
4 Answers
Reset to default 5For anyone who es 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,
};
}
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 ments, 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).
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.
You are working with promises. Therefore, the answer can e 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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745383758a4625349.html
评论列表(0条)