https://pulumi.com logo
Title
n

nice-father-44210

12/02/2021, 2:37 AM
What’s the correct way to create resources when iterating over a List or Map that is provided as an Output? The Python docs for
Output::apply
say the following:
'func' is not allowed to make resources.
c

colossal-boots-62227

12/02/2021, 3:15 AM
Not an definitive answer but I had the same question and did some DD on this and the consensus seems to be “avoid creating resources in apply if you can, but it is ok to do it if there is no alternative”
It would be great to get a definitive answer from the Pulumi folks however as it feels a bit dirty doing it when the docs say not to!
The only case I found so far that needed this was automatic AWS certificate verification, where you need to
apply
the
domain_validation_options
and create an Route53
Record
for each (if anyone knows a way around this I’m all ears!)
r

red-match-15116

12/02/2021, 4:04 AM
Yeah this is kind of a weird one. The best practice is to avoid creating resources in apply _as much as possible (_but it is sometimes unavoidable). FWIW, the advice to "not create resources in apply" is because of the fact that it may not be adequately captured in the
preview
(because some items in the output may not be known at preview time). If you can deal with the "may not have accurate preview" issue, then it's IMO fine to create resources within the apply. That being said, depending on if you know the length of your list or map, you can do things like
list_of_things: Output[List]

for i in range(3):
    resource = MyResource("my-resource", property=list_of_things.apply(lambda args: args[i])
Note: I am no longer an "official"
Pulumi folk
but I was until pretty recently.
❤️ 2
n

nice-father-44210

12/08/2021, 10:43 PM
In Python, I think there’s a slight problem with the above. The
lambda
will be invoked asynchronously and the value of
i
at that time will always be
2
This can be fixed as follows:
lambda args, i = i: args[i]
🙌🏽 1
r

red-match-15116

12/08/2021, 10:50 PM
good callout!
n

nice-father-44210

12/09/2021, 5:03 PM
Thanks for your feedback. This is essentially the approach I had started using and was wondering if I’m on the right track. Much appreciated!