How to register EC2 Instance to ECS cluster?

Amazon Web-ServicesDocker

Amazon Web-Services Problem Overview


I have started 2 ECS optimized instances on EC2, but how can I register them as ECS container instances ?

Can not figure out a way of doing that.

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

When you start an ECS optimized image, it starts the ECS agent on the instance by default. The ecs agent registers the instance with the default ecs cluster.

For your instance to be available on the cluster, you will have to create the default cluster.

if you have a custom ecs cluster, you can set the cluster name using the userdata section.

The ecs agent expects the cluster name inside the ecs.config file available at /etc/ecs/ecs.config.

You can set it up at instance boot up using userdata script

#!/bin/bash
echo ECS_CLUSTER={cluster_name} >> /etc/ecs/ecs.config

Please refer to the following ecs documentation for more information http://docs.aws.amazon.com/AmazonECS/latest/developerguide/launch_container_instance.html

Solution 2 - Amazon Web-Services

When you create an EC2 instance, you must specified the IAM role linked to your ECS container (if using SDK/..., you must specified the "Instance Profile ARN" of this role in the parameters), if you use the interactive ECS cluster creation at your first ECS use on the aws website, you should already have an ecsInstanceRole link to the default cluster.

Then, after being launched, your EC2 instance will be automatically register as ECS container in this cluster.

Solution 3 - Amazon Web-Services

Other than the user-data script echoing the non-default cluster's name, remember that the container instances need external network access to communicate with the Amazon ECS service. So, if your container instances do not have public IP addresses, then they must use network address translation (NAT) gateway to provide this access.

Source: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/launch_container_instance.html

Solution 4 - Amazon Web-Services

One more thing you can do to register instances in the cluster is to:

  1. Create a service and assign it a task;
  2. When creating a service - choose a load balancer and respective number of tasks that should be launched;
  3. Afterwards, create a target group for the load balancer (if one doesn't exist already);
  4. You have 2 options now - either create desired instances manually or edit a launch template of your cluster (based on the template, the instances will be created automatically);
  5. If you create instances via the launch template - they will be linked to the target group automatically (because you selected the respective load balancer when creating the service);
  6. Otherwise add them manually - any instance that passes health checks and is in your service target group will be automatically added to the cluster, unless the cluster already has the max. amount of instances.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHello ladView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesShibashisView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesDarkCenobyteView Answer on Stackoverflow
Solution 3 - Amazon Web-ServicesSlawomirView Answer on Stackoverflow
Solution 4 - Amazon Web-Services8urning8eardView Answer on Stackoverflow