I'm trying to get a list of organization account id's from our AWS account.
I have the following code
const acc_list: string[] = [];
(async () => {
const orgs = await aws.organizations.getOrganization();
orgs.accounts.forEach(account =>
acc_list.push(account.id))
})()
console.log(acc_list)
Which logs an empty list because clearly the console command is running before the promise.
My goal is I want to send the list of accounts to a different function (different file) in my typescript application. No clue how to do that.
The problem is that the function you've created async () => { ... }
actually returns a Promise
that you still need to await. So, wrapping an asynchronous code into such async lambda doesn't make sence because the code block remains asynchronous. I can suggest you
this tutorial.
A solution depends on the problem context, might be the whole block should be asynchronous, like:
async function printAccountIds() {
const orgs = await aws.organizations.getOrganization();
const accountIds = orgs.accounts.map(account => account.id);
console.log(accountIds);
}
Or you can just subscribe to the promise, like:
aws.organizations.getOrganization().then(orgs => {
const accountIds = orgs.accounts.map(account => account.id);
console.log(accountIds);
});