I’m trying to break my pulumi definition into mult...
# typescript
s
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
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
yes, ideally. 🙂
b
so normally I wrap this stuff in a component resource, so you'd have
Copy code
export class foo extends pulumi.ComponentResource {
  // all my resources here
}
you can than set public functions on that component resource
Copy code
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
Okay I googled a bit and I think being able to make custom components would do the trick
b
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
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
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
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
@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
yes, basically that
component resource works