Hello! I was wondering if there was an easy way to...
# general
c
Hello! I was wondering if there was an easy way to wait for a newly created
aws.ec2.Instance
to be in a running state before attempting to do a
command.remote.Command
on that host.
e
The normal way to do this is to write a custom "wait script" as part of an apply. Found an k8s example of this at https://gist.github.com/lukehoban/fd0355ed5b82386bd89c0ffe2a3c916a But the idea's similar for any resource pair. Take an output from the ec2.Instance and "apply" it to run some logic in a while loop that doesn't return till it's ready. Then exit the while loop and return a value to resolve the output. If the command resource then depends on that output it won't run till the ec2 instance is ready.
c
hmm, so do you think something like this would work:
Copy code
async function waitForInstance(id: string) {
  if (pulumi.runtime.isDryRun()) {
    return;
  }

  // Wait for up to 10 minutes
  for (let i = 0; i < 60; i++) {
    let instance = await aws.ec2.getInstance({ instanceId: id });
    if (instance.instanceState === "running") {
      return instance;
    }
    pulumi.log.info(`Waiting for Instance to start (${id})`);
    // Wait for 10s between polls
    await new Promise((r) => setTimeout(r, 10000));
  }

  throw new Error("timed out waiting for Instance to start");
}
e
Yes I think so, I don't think repeated getInstances for the same ID are cached so I suspect this would work
1211 Views