Hello, given this note from the docs: > During ...
# general
b
Hello, given this note from the docs:
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.
can anyone let me know the preferred way to create a resource that depends on an output value? Me and my teammates have been trying for days to create resources without this, and always end up with various errors relating to things being an Output instance (we’re using python if it matters)
r
Can you give an example of the error and what you are trying to do? It'll be a lot easier to point you in the right direction that way.
You should be able to pass in the Output to another resource's Input directly. If you need to put strings together or create json you'll need to use
apply
. It's possible the errors you are running into is when you try to
print
the output?
b
i’d have to do a bunch of re-rework to get it producing the errors again, we’d just refit everything to use an apply callback any time an output is used
then i noticed our previews were short, and then i noticed the note in the docs
r
we’d just refit everything to use an apply callback any time an output is used
that's probably not necessary TBH
b
i was hoping so, but we’ve not gotten it fully working without that
i’ll keep trying for now and post again if i get the errors back, thanks for looking
👍🏽 1
r
Are you creating the resources inside the apply?
Also, perhaps this blog post is helpful: https://www.leebriggs.co.uk/blog/2021/05/09/pulumi-apply.html
b
Yeah currently all the resources that depend on the value of any output are being created in an apply
(which was to avoid errors relating to outputs, still don’t have those back)
r
FWIW, you should just be able to do this:
Copy code
res1 = MyResource("my_resource", property="blah")

res2 = OtherResource("foo", dependent_property=res1.property)
If you need to transform the output (like add something to the string)
Copy code
res1 = MyResource("my_resource", property="blah")

res2 = OtherResource("foo", dependent_property=res1.property.apply(lambda x: f"new-string-{x}")
The reason that your previews are inaccurate are 100% because you are creating those resources within an apply. There are some instances where it is unavoidable, but for the most part you should be able to do it without creating resources in the apply.
b
yeah i knew it was the cause of the wrong previews, thanks for the datapoint though
i think i’ve got this sorted out, tldr your
print
suggestion was not far off, Outputs were just being used as an argument to not-a-Resource
🎉 1
🙌🏽 1
thanks again 🎉
r
awesome!