https://pulumi.com logo
#typescript
Title
# typescript
b

boundless-airport-99052

04/09/2020, 4:00 PM
In my case, I’m creating a
ComponentResource
and want to use
await
in the
initialize
function as below:
Copy code
export class MyComponent extends pulumi.ComponentResource {
    protected async initialize(args: Inputs): Promise<any> {
         await pulumi.output(args.inputA).apply(value => ....)
    }
}
w

white-balloon-205

04/10/2020, 4:10 AM
In general it’s not recommended to do this - as you are losing the dependency information associated the Output, which may lead to incorrect dependencies between resources. But if you do need to do it, there is a .promise() method that returns the underlying value promise. It is private in TypeScript so you may need to cast to any. What is the rest of what you are trying to do here? I expect there is a cleaner way to accomplish it.
b

boundless-airport-99052

04/10/2020, 8:26 AM
Thank @white-balloon-205 for your answer. It’s related to my tests with pulumi 2.0 and I give an early feedback here: https://pulumi-community.slack.com/archives/CB36DSVSA/p1586506952088200 But I’m trying to simplify my use case in order to share them with you, and to see if it’s blocking or not.
@white-balloon-205, to give you more details about my use case: (note: I simplified the use case, so the one detailed below could have inconsistencies) I have a
ComponentResource
, called
CustomSubnet
that could be instantiate several times. Each
CustomSubnet
create nested resources, BUT one of them resources should be created only once per
vnet
. Let’s call it
vnetRouteTable
Let say we can have: -
vnet1
(created by another stack) -
CustomSubnetA
=> must create`vnet1RouteTable` -
CustomSubnetB
-
vnet2
(created by another stack) -
CustomSubnetX
=> must create`vnet2RouteTable` -
CustomSubnetY
With pulumi
1.x
, the
CustomSubnet
args was taken a string
vnetName: string
This value was used as a cache key to know if the corresponding
vnetRouteTable
has already been created or not. The
vnetName
was retrieved from another stack output with
new StackReference(..).requireOutputSync('vnetName')
With pulumi
2.0
, the method `requireOutputSync`has been removed, so I use
requireOutput
which returns a
pulumi.Output<string>
My Issue: I can’t use this value anymore as cache key (nor in pulumi resource name which takes a
string
). So I was wondering what I can do 🙂 I understood the usage of
pulumi.Output
in resource graph dependency, but it’s not a problem here as the resource has been created by another stack, so loosing it is not an issue. In tried several things: 1) Use
initialize
method with
Copy code
const vnetName = await (args.vnetName as any).promise()
This seems to work but the code is less cleaner and I don’t know what is the impact of creating other resources asynchronously here (are there impacts on resources graph and dependencies??) 2) Do not use
initialize
and defer that part of code (using the cache) in the
.apply
function like this :
Copy code
args.vnetName.apply( name => {
  if (!inCache(name)) {
     this.vnetRouteTable = new azure.RouteTable(...)
  } else {
     this.vnetRouteTable = getFromCache(name)
  }
})
The
vnetRouteTable
resource is created asynchronously and I do not know the impacts but the others resources are created as previously. AFAIK, I do not have another option. Sorry for the long explanation but I tried to be concise and to simplify my use case. 1. What are the impact in resource graph dependencies of solution 1 or 2? 2. Do you see another solution? 3. Are you sure you want to remove the
requireOutputSync
😂 Best regards
Hi @white-balloon-205, I know it’s a long message, but I wonder if I do the right way. In short, I wonder if I can create a CustomComponent’s sub-resources in a
pulumi.Output.apply
callback if I know that the
pulumi.Output
does not wrap a resource (said differently, if the
pulumi.Output
is not used for dependency graph)