DarkCenobyte
DarkCenobyte

Reputation: 501

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).

Upvotes: 0

Views: 2838

Answers (4)

Chris Dev
Chris Dev

Reputation: 41

Both the EC2 and ECS classes in the Nodejs SDK have waitFor which allow you to loop until a certain condition is met. It'll loop once every 6 seconds up to 100 times.

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EC2.html#waitFor-property

Upvotes: 2

Shibashis
Shibashis

Reputation: 8411

You should use the node js ecs api describeClusters.

var params = {
  clusters: [
    'STRING_VALUE',
    /* more items */
  ]
};
ecs.describeClusters(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

The response returns the count of registered instances in the attribute "registeredContainerInstancesCount" Take a count of instances before you start the ec2 instance and wait till the count goes up by 1. You can use a library like deasync to synchronize the wait.

Upvotes: -1

Marc Young
Marc Young

Reputation: 4012

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?

Upvotes: 0

mcheshier
mcheshier

Reputation: 745

Have you tried using User Data to send commands to your EC2 at startup? For instance, I pass the following user data into my ECS container instances so they will update themselves and attach to a cluster:

#!/bin/bash
yum update -y
echo ECS_CLUSTER=<my cluster name> >> /etc/ecs/ecs.config

If you're not using autoscaling groups you can also tag your instance with a name in here, etc.

You should be able to run your own code here as well, and then your container instance will be ready by the time it is attached to your cluster.

Upvotes: 0

Related Questions