pulumi / pulumi-command

Apache License 2.0
64 stars 26 forks source link

Redeploy parent on update #560

Open cowwoc opened 21 hours ago

cowwoc commented 21 hours ago

Hello!

Issue details

Is there any way to instruct pulumi to redeploy (delete and then recreate) the parent resource if a script that was run on it has changed?

It's a bit of a catch-22 in the sense that the script depends on the container, but then the container depends on the script.

mjeffryes commented 13 hours ago

There might be an easier way to do this, but the idea that jumps to my mind is to wrap both the container and the command resource in a component (https://www.pulumi.com/docs/iac/concepts/resources/components/) and then use replaceOnChanges (https://www.pulumi.com/docs/iac/concepts/options/replaceonchanges/) on the component to replace whenever the script changes.

Something like:

type MyComponentArgs {
  script: string;
}

class MyComponent extends pulumi.ComponentResource {
    constructor(name: string, myComponentArgs: MyComponentArgs, opts: pulumi.ComponentResourceOptions) {
        super("pkg:index:MyComponent", name, {}, opts);
        # EC2 instance configuration
        let server = ec2.Instance(
            SERVER_NAME,
            instance_type=AWS_INSTANCE_TYPE,
            ami=ec2.get_ami(
                most_recent=True,
                owners=["amazon"],
                filters=[{"name": "name", "values": ["*ubuntu-latest-*"]}],
            ).id,
            key_name=ec2.get_key_pair(key_name=AWS_KEY_NAME).key_name,
            vpc_security_group_ids=AWS_SECURITY_GROUPS,
            subnet_id=AWS_SUBNET,
             opts=pulumi.ResourceOptions(parent=this),
        )

        # Set up remote connection for commands
        lett ssh_connection = command.remote.ConnectionArgs(
            host=server.public_ip,
            user=SERVER_USER,
            private_key=AWS_PEM_KEY,
            opts=pulumi.ResourceOptions(parent=this),
        )

        # Install Docker on the remote EC2 instance
        lett command = command.remote.Command(
            "run script",
            connection=ssh_connection,
            create="sh -c ${myComponentArgs.script}",
            opts=pulumi.ResourceOptions(parent=this),
        )

    }
}

const ecsServerWithScript = new MyComponent("myContainer", {
  script: "echo 'hello world'"
  }, 
  opts=pulumi.ResourceOptions(replaceOnChanges=["script"]),
}