https://pulumi.com logo
Title
s

stocky-petabyte-29883

03/31/2022, 12:42 PM
Hi I am trying to export outputs from resources created inside a loop using typescript. However the accountOutput is always empty. I am wondering whats the right approach to do this.
let accountDetails = [
    {name: 'test', email: '<mailto:testt@example.io|testt@example.io>', environment: 'test', billingInfoAccess: 'ALLOW'}
];

var accountOutput: { accountName: string, accountId: string }[] = [];

for (const accountDetail of accountDetails) {
    const account = new aws.organizations.Account(accountDetail.name, {
        email: accountDetail.email,
        iamUserAccessToBilling: accountDetail.billingInfoAccess,
        name: accountDetail.name,
        tags: {
            Environment: accountDetail.environment
        },
    });

    account.arn.apply(arn => {
        console.log(arn);
        accountOutput.push({accountName: accountDetail.name, accountId: arn})
    })
};


export const outputs = {
    accountInfo: accountOutput,
}
e

echoing-dinner-19531

03/31/2022, 1:30 PM
"applys" run asynchronously trying to do data mutations in them is fraught with issues. Flip the apply and push around:
accountOutput.push(account.arn.apply(arn => {accountName: accountDetail.name, accountId: arn}))
🙌 1