https://pulumi.com logo
Title
s

salmon-honey-75627

11/24/2020, 10:46 PM
I’m trying to break my pulumi definition into multiple files. I’m not too familiar with typescript so that might be causing my problems. Right now, if I want to export a helper function for use by another file, that gets picked up as a stack output. What’s the best way to deal with this? I obviously don’t want a bunch of random javascript objects and type interfaces getting exported as stack outputs but I also can’t shove everything in a single file.
b

billowy-army-68599

11/24/2020, 10:49 PM
oh this is a great question and one I don't actually know the answer to. Just to make sure I understand so I can get you the help you need, you're doing
export const foo = bar
in a library file, and it's registering as a stack output, you just want it to be an exported method, right?
s

salmon-honey-75627

11/24/2020, 10:49 PM
yes, ideally. 🙂
b

billowy-army-68599

11/24/2020, 10:52 PM
so normally I wrap this stuff in a component resource, so you'd have
export class foo extends pulumi.ComponentResource {
  // all my resources here
}
you can than set public functions on that component resource
public bar(): Output<string> {
        return this.resource.id;
    }
that way I can just import that into my main library. Is that an option? if it's not, i can get you some help from someone a bit more versed in this 🙂
s

salmon-honey-75627

11/24/2020, 10:55 PM
Okay I googled a bit and I think being able to make custom components would do the trick
b

billowy-army-68599

11/24/2020, 10:56 PM
I personally love this componentresource as an example/boilerplate: https://github.com/jen20/pulumi-aws-vpc/blob/master/nodejs/src/index.ts There are many others, but that one is nice and simple and well written
s

salmon-honey-75627

11/24/2020, 10:57 PM
yeah, i largely just want to put a bit more logic into some lambdas and i can’t have several of them in an
index.ts
, it seemed like a good time to look into breaking everything down
l

little-cartoon-10569

11/24/2020, 11:09 PM
I'm doing this at the moment, and am finding the opt
aliases: [{parent: this}]
to be very useful to prevent Pulumi from deleting and re-creating all my moving resources...
h

hundreds-musician-51496

11/24/2020, 11:32 PM
Hmmm ... I think you guys might be going down the wrong path. TypeScript imports & exports aren't related to stack outputs in any way, except one. If you are using the CLI, then whatever is returned by
index.js
becomes your stack outputs. Does
index.js
have a single exported function, with type
pulumi.Output<...>
or
Promise<pulum.Output<...>>
? If not, maybe you are getting some weird export behavior.
But exporting/importing functions between modules is just a plain typescript thing that doesnt' require custom resources.
l

little-cartoon-10569

11/25/2020, 12:21 AM
@salmon-honey-75627, what was causing your question? Did you have multiple projects? I'm guessing that proj1/index.ts has a function that was also useful in proj2/index.ts, so you exported it, and then Pulumi would dump it to the console in the list of exports, and you didn't like that. That is the circumstance that
ComponentResource
helps with.
s

salmon-honey-75627

11/25/2020, 1:49 AM
yes, basically that
component resource works