https://pulumi.com logo
l

little-cartoon-10569

08/10/2020, 5:04 AM
Is it pointless to use any of the resource-getting functions in a resource constructor? I want to call
awsx.ec2.Vpc.fromExistingIds
to load a Vpc created elsewhere so that I can create a new route on one of its subnets. But
awsx.ec2.Vpc#privateSubnets
is
async
, and typescript doesn't allow use of
await
in constructors. I assume that I have to use an async function called after my constructor?
g

green-school-95910

08/10/2020, 9:01 PM
Two ways to do it: ā€¢ Make a constructor that takes the responses, not the ids, and a static async method. The boring way ā€¢ Wrap the responses in
pulumi.output
and treat them as any pulumi resource, manipulating using
.apply(...)
l

little-cartoon-10569

08/10/2020, 9:38 PM
Thanks. I didn't know you could wrap a Promise with
pulumi.output
and use apply in the normal way, that's useful. I do the opposite in my tests all the time, I should have guessed!
I think I'll go with your first option though: a little rework allows me to pass the Vpc in from the project, and later I'll be able to switch to using StackReferences, like I should. (Can't do that yet because of our chosen backend solution.)
g

green-school-95910

08/10/2020, 9:43 PM
I personally prefer to have all my inputs types as
pulumi.Input<something>
and then use it with
pulumi.output
, this my functions and constructors accepts resource outputs, promises and raw values at any time
šŸ‘ 1
l

little-cartoon-10569

08/10/2020, 10:40 PM
Good idea. Makes it slightly more verbose in the unit tests: we put 99% of code in ComponentResources and unit test them, so the overhead is noticeable. But it's worth it, I think. Certainly worth doing for new classes.