https://pulumi.com logo
Title
w

wet-noon-14291

05/05/2021, 2:06 PM
A general question. If you do apply on a property of a resource to use that as input to another resource, when will the apply resolve? Will it resolve when the property has a value or will it resolve when the resource is ready? If it is the former, what is the recommended way of running some code after a resource is ready to calculate the input to another resource?
To clarify what I want to achieve. Two services: A, and B. Flow is then: 1. Provision A 2. Run some code, that depends on A, to calculate value Y 3. Provision B with value from Y
b

bored-oyster-3147

05/05/2021, 2:12 PM
I think you should use
.apply(...)
in your scenario. To clarify, and somebody correct me if I'm wrong, but you asked:
Will it resolve when the property has a value or will it resolve when the resource is ready?
I don't believe any of those output properties will have a value until the resource is ready. At the very least, the `Output<T>`s provided by a given resource contain information such that when they are passed into the
Input<T>
of another resource that resource is made aware that it depends on the previous, and must wait for it to be provisioned.
I find it best to think of using
.apply(...)
to transform the value like using a
Promise.then(...)
to transform the return value of a promise
w

wet-noon-14291

05/05/2021, 2:28 PM
What I would like to have though is
A.apply
or something, that is, on the resource and not on the properties... I'll try and see how it goes. With that said, reading this I get a little bit confused:
During some program executions, apply doesn’t run. For example, it won’t run during a preview, when resource output values may be unknown. Therefore, you should avoid side-effects within the callbacks. For this reason, you should not allocate new resources inside of your callbacks either, as it could lead to pulumi preview being wrong. - https://www.pulumi.com/docs/intro/concepts/inputs-outputs/#apply
Does this mean that if I have a change in the result from 2 (see flow above) it won't be recognized during preview?
I mean, if
apply
works as in the link, how would a change be recognized if the only change to a resource comes from an apply statement.
b

bored-oyster-3147

05/05/2021, 2:36 PM
It's a good question. I know that provisioning doesn't happen in preview so some values it is not able to know, such as those that are derived from other resources. But if the resource is already deployed, than it is able to use the state of a previous deployment when calculating your preview.
What I would like to have though is 
A.apply
 or something, that is, on the resource and not on the properties
I don't think this is necessary, because like I said, the
Output<T>
contains information detailing what Resource it came from so that pulumi is able to build your dependency tree implicitly.
Does this mean that if I have a change in the result from 2 (see flow above) it won't be recognized during preview?
If neither resources are deployed yet, pulumi preview would recognize that: • Resource A will be provisioned • Resource B will be provisioned • Resource B depends on Resource A If all or some of those resources are already provisioned, than pulumi preview logic is a little more complex and utilizes your current state in order to calculate changes
w

wet-noon-14291

05/05/2021, 4:27 PM
So if A changes causing the output from 2 to change, will B also then be provisioned updated? I guess (and hope) it will.
l

little-cartoon-10569

05/05/2021, 9:00 PM
This is all handled correctly within Pulumi. Usually you don't want to create B within A.apply; instead, use the fact that
.apply
returns an output:
const a = new A("a", {});
const b = new B("b", {
  y: a.y.apply(y => doExtraWork(y)
});
So long as A.y is an Output and and B.y accepts an Input, it should all just work.