Hi all. I started using pulumi recently. I have a...
# python
b
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(
        "ImagePipeline",
        image_recipe_arn=recipe.arn,
        infrastructure_configuration_arn=infrastructure_configuration.arn,
    )

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

    def run_pipeline(pipeline_arn):
        <http://pulumi.info|pulumi.info>(pipeline_arn)
  
        runner = ec2.Instance(
            "AmiInstance",
            ami=ami_id,
            instance_type=instance_type,
            key_name=key_name,
            iam_instance_profile=instance_profile.name,
            user_data=runner_user_data_script,
        )

        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
run_pipeline
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):
            super().__init__(
                "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
statement
s
https://pulumi-developer-docs.readthedocs.io/en/latest/architecture/overview.html#resource-providers 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 https://www.pulumi.com/docs/concepts/options/dependson/ I use that in conjunction with https://www.pulumi.com/docs/concepts/options/parent/ 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
b
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