# python


09/28/2023, 8:59 AM
Hi all. I started using pulumi recently. I have a python code that I need to run only if a particular resource has been created/changed (in particular I want to create an aws ec2 instance if an imagebuilder pipeline has been created/changed):
Copy code
    pipeline = aws.imagebuilder.ImagePipeline(

    pulumi.export("Pipeline ARN", pipeline.arn)

    def run_pipeline(pipeline_arn):
        runner = ec2.Instance(

        pulumi.export("Public IP", runner.public_ip)

    # This should only run if pipeline has been created/changed in this execution.
    # Avoid running this code if pipeline has not been changed.
    pipeline.arn.apply(lambda arn: run_pipeline(arn))
At this moment, when I do a
pulumi up
, the
function is executed even before I select if I want to "perform the update" (the question that pulumi asks). Does anyone know which is the best way to do this?
Or maybe I can somehow encapsulate that function into a Pulumi resource that depends on the pipeline?
I have also tried with a custom class that depends on the pipeline, but the init method is always executed. And I want it to be executed only if there's a change in the pipeline:
Copy code
class MyAMI(pulumi.ComponentResource):
        def __init__(self, name, opts=None):
                "pkg:example:AMI", name, props={}, opts=opts,
            child_opts = pulumi.ResourceOptions(parent=self)

            with open("/tmp/pulumi.log", "a") as file:
                file.write("Running pipeline")
Okay, I can pass the pipeline to that new class and make the instance dependent on it. But what about if I need to run some python code only if the pipeline has been executed? For example that
with open


09/28/2023, 11:02 PM Not sure how to conditionally drive the ec2 creation programmatically since the declarative nature of Pulumi seems to first want to understand the full tree of resources to build, then it fires the dependency tree to another process to create everything But maybe you meant I use that in conjunction with so the deps tree looks nicer on
pulumi up
I find that it helps to think of how pulumi programs work as "yaml", if you can't do it in yaml, you can't do it pulumi, generally speaking


09/29/2023, 12:33 PM
Thanks for the response @straight-cartoon-24485. I'm finally going to use dynamic providers since it seems that they allow me to create my custom code when the resource is created/changed/deleted