DarkCenobyte DarkCenobyte - 2 months ago 12x
Node.js Question

Using AWS SDK for Node.js, how to wait for EC2 instance being added to ECS cluster?

I recently start using AWS, and read its docs.

For my needs, I must deploy EC2 instances from my node.js code, wait they are added to my ECS cluster (via using the right Arn Instance Profile), then start a task on the last EC2 instance started.

But, actually, I didn't find a way to define a task at the EC2 start, because I must override the docker command with a variable from my nodejs and do some other task. So, I must wait the EC2 being added to the ECS cluster before try to startTask() with my params and the Arn from the Container Instance Id (but I can't get it from a listContainerInstances() until).

Is there a way to achieve this easily?
(Actually I try to loop with a setTimeout until the listContainerInstances() return something new, but I don't think it's a good practice at all and my code look a little bit ugly).


How are you launching into the ASG?

Your best bet is to make a call to describeAutoScalingInstances and get the instanceIds. Set your instance to launch, find it's instanceId by getting the one not in the previous call. Then set a loop with a max attempts and a sleep timeout to wait until it's ready. You'll have to make a call with listContainerInstances for your cluster to know when it is ready.

When it's ready you'll want to run the task on it and override the command there. You can pin the task to a specific instance via the SDK and CLI, but not in the UI

What have you tried?