https://pulumi.com logo
Title
a

alert-raincoat-81485

06/14/2021, 7:44 PM
Hello Team, I am trying to understand broader way of pulumi apply call here. i have to statically define pulumi apply call for each instance of the class as shown in an example. when i loop on the apply, it picks up the last instance of class but skips the previous. is there anyway we can use the pulumi apply call dynamically and do not have to define statically for n number of instances of class?
import pulumi
import pulumi_aws as aws  

asg_name = {}
class ASG():

    def __init__(self):
        self.name = name 

    # Creating Autoscaling.
    asg_name[name] = aws.autoscaling.Group(.....defs....)

inst1 = ASG(idx1)
inst2 = ASG(idx2)  
inst3 = ASG(idx3)

defineASG(a,inst_type)
    ## Boto API create EC2 tags ## 

asg_id = {}
asg_id['idx1'] = autoScale['idx1'].name.apply(
    lambda a: defineASG(asg=a, inst_type='idx1')

asg_id['idx2'] = autoScale['idx2'].name.apply(
    lambda a: defineASG(asg=a, inst_type='idx2')

asg_id['idx3'] = autoScale['idx3'].name.apply(
    lambda a: defineASG(asg=a, inst_type='idx3')
l

little-cartoon-10569

06/14/2021, 9:03 PM
Hi. I'm not sure that this is an AWS-specific question. Why are you calling
apply()
on name? What are you doing with the name; is it being passed into a constructor of a Pulumi resource? You don't need to call
apply()
in this case.
a

alert-raincoat-81485

06/14/2021, 9:07 PM
Well this is not AWS specific question, i apologize. This is more regarding pulumi. I am using
apply()
here for some specific reason. When the Autoscaling class will create multiple instance of asg groups eg
idx1. iidx2...n
on each instance,
apply()
will be called on each instance class and start creating EC2 tags for example for ’idx1" instance class, it will create tags as
idx1-1, idx1-2, idx1-3..n
.
So in the real case, apply function does a lot of thing beside just creating tags in my case. i just put ec2 tags creation for an example only.
l

little-cartoon-10569

06/14/2021, 9:14 PM
Often the code can be written so that the
apply()
(or
interpolate()
) is performed on parameters to the constructor. If it's possible, then this will at least eliminate a potential source of issues. Here's an example from the Pulumi examples repo: https://github.com/pulumi/examples/blob/71a705e12b1ea6b132ecf164f0a71adabc78b4ce/aws-py-apigateway-lambda-serverless/__main__.py#L78
In this case, the call to
aws.lambda_.Permission()
could have happened inside the
apply()
for the same effect, but doing it this way allows Pulumi to better report on dependencies. And it may avoid the issue you're seeing.
a

alert-raincoat-81485

06/15/2021, 5:28 PM
I guess the example you referred is quite different and not related the way i want. Here i am trying to accomplish synchronous call with every loop of instance calling
apply()
, but in a loop it picks the last element and execute
apply()
rather it has to go through each element and call
apply()
l

little-cartoon-10569

06/15/2021, 9:30 PM
Technically, it won't ever be synchronous. The loop will finish before the engine is invoked. In Pulumi, your code builds state, and after the state is fully built, the Pulumi engine makes the provider look like your state. That said, there shouldn't be any problem doing it either way. You can loop over all the resources and create new resources from them, whether you're inside an
apply()
or not. I'm not familiar with Python's scoping rules, they may be throwing a spanner in the works here. Maybe the question could be asked in #python, along with the looping code that isn't working for you?