Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expected outputs are not produced when using an async main function in Node #2910

Closed
jen20 opened this issue Jul 8, 2019 · 9 comments · Fixed by #2916
Closed

Expected outputs are not produced when using an async main function in Node #2910

jen20 opened this issue Jul 8, 2019 · 9 comments · Fixed by #2916
Assignees
Labels

Comments

@jen20
Copy link
Contributor

jen20 commented Jul 8, 2019

I would expect the following program to produce an output name bucketArn with the value from the resource, however it produces no outputs.

import * as aws from "@pulumi/aws";
import {ComponentResource, ComponentResourceOptions, Output} from "@pulumi/pulumi";

class TestComponent extends ComponentResource {
    private bucket: aws.s3.Bucket;

    public bucketArn(): Output<string> {
        return this.bucket.arn;
    }

    constructor(name: string, args: {}, opts?: ComponentResourceOptions) {
        super("TestComponent", name, {}, opts);

        this.bucket = new aws.s3.Bucket(`${name}-bucket`, {}, {
            parent: this,
        });
    }
}

async function main() {
    const component = new TestComponent("test", {});

    return {
        bucketArn: component.bucketArn(),
    }
}

module.exports = main();
@jen20 jen20 added area/sdks Pulumi language SDKs language/javascript labels Jul 8, 2019
@jen20
Copy link
Contributor Author

jen20 commented Jul 8, 2019

Current versions:

pulumi CLI: v0.17.21
node: v8.14.0

@lukehoban lukehoban self-assigned this Jul 8, 2019
@clstokes
Copy link
Contributor

clstokes commented Jul 9, 2019

Occurs with these versions too if helpful:

  • pulumi v0.17.21
  • node v12.5.0

@lukehoban lukehoban assigned CyrusNajmabadi and unassigned lukehoban Jul 9, 2019
@lukehoban
Copy link
Member

A simple workaround is:

export const bucketArn = main().then(m => m.bucketArn);

It is mostly unintentional that this does not work as written - if your module export is itself a Promise or Output, we should be able to handle that (just like we handle if it is an object with Promise or Output-valued properties).

@CyrusNajmabadi
Copy link
Contributor

Fix available.

@xtellurian
Copy link

Thanks for the snippet @lukehoban :) it worked for me

Is there a way to export multiple stack outputs?

@CyrusNajmabadi
Copy link
Contributor

Is there a way to export multiple stack outputs?

Absolutely! Just do this:

const mainResult = main();
export const bucketArn = mainResult then(m => m.bucketArn);
export const whatever = mainResult.then(m => yadda yadda);
export const whatever2 = 1 + 2 + floop;

basically, all your top-level exports become 'stack outputs'. You can have those be outputs, promises, expressions, whatever. :)

@tigran-gruv
Copy link

export const bucketArn = mainResult then(m => m.bucketArn);
export const whatever = mainResult.then(m => yadda yadda);

^^^ Is it a guaranteed behavior to export promise and get the resolved value at the end?

@lukehoban
Copy link
Member

Is it a guaranteed behavior to export promise and get the resolved value at the end?

Yes - the Pulumi JavaScript SDK will load your entrypoint module, then resolve all Inputs (Outputs or Promises) that were set on the modules exports. This is required and guaranteed behavior.

@fuadsaud
Copy link

I was bitten by this issue today and, as far as I understand, the docs seem to indicate that exporting an async function would work. Am I missing something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants