Hi everyone, :smile: We're using Pulumi's Compone...
# typescript
p
Hi everyone, šŸ˜„ We're using Pulumi's ComponentResource to encapsulate complex infrastructure definitions, or to manage multiple resources under a single lifecycle. However, we're facing challenges when asynchronous logic is required before creating child resources inside a ComponentResource constructor. Here are some common cases we're dealing with: - We need to query external APIs (using the AWS SDK) or perform custom business logic to determine how to structure resources — for example, describing all ALB listener rules for a specific listener before creating target group attachments. - In more complex scenarios, chaining multiple
apply()
calls can make the logic difficult to maintain and reason about. - We’re also aware of dynamic providers, but writing a separate provider for every such case seems too heavyweight — especially when the logic isn’t strictly about CRUD operations. - We're not sure if preparing all necessary values outside the constructor ahead of time is always practical or even a good idea, especially as complexity grows. So the question is: • What is the recommended best practice for handling asynchronous business logic in
ComponentResource
constructors? • Are there any common patterns, official guidance, or real-world examples on how to cleanly manage async operations (such as API calls, data lookups, etc.) inside or before creating child resources in a
ComponentResource
? We'd really appreciate any insights or examples from those who have tackled similar situations. Thanks!
s
@prehistoric-egg-84122 Have you considered using the resource hooks? (doc, blog) These hooks are there specifically to splice some custom logics at various steps of a resource lifecycle. I would also recommend looking at
dependsOn
(doc) as a way to sequence resource management to your needs.
šŸ‘ 1
p
Thank you! I’ll definitely take a closer look. That said, what I’m most curious about is this: • When working with a ComponentResource, there are situations where it’s necessary to call async functions such as AWS SDK calls. • (ex. We need to call AWS SDKs like DescribeRules inside a ComponentResource to compute priorities dynamically) In these cases, is there any best practice or official guidance on how to handle them properly?
b
Just wrap them in a pulumi output if it is async
šŸ‘ 1
then you can use it natively
most pulumi resource take pulumi.Input so you don’t even need to apply the results before using them
if they do, just do a resource.apply((r) => {create resource w/ applied value})
I do component resource extensions for all my classes and I always try to resolve promises at the class level rather than in something like index.ts
g
sometimes you can also use ā€œundocumentedā€ (due to misconfiguration of doc engine) there is a method called
initialize
where you can run
async/await
and then pass
Outputs
to the constructor https://github.com/pulumi/pulumi-awsx/blob/bb8d4ef9dcff0a4fc446d8921665fcdf3a89c55e/awsx-classic/ec2/vpc.ts#L284
šŸ‘ 1
t
> if they do, just do a resource.apply((r) => {create resource w/ applied value}) It was my understanding that creating resources within an
apply
is discouraged. Has that changed recently?
b
I generally don’t do it as it can lead to missing resources in previews, but if I do I create one resource inside of it and return. Always better to just pass in outputs to inputs
šŸ™Œ 1