AWS ECS container instance

2k views Asked by At

I have tried to launch an ECS container instance using Ansible EC2 module.

My playbook is as follows.

- name: Launch ECS Container Instance
  ec2:
    key_name: "{{ ec2_keyname }}"
    instance_type: t2.micro
    image: ami-ca01d8ca
    wait: yes
    group: "{{ ec2_security_group }}"
    region: ap-northeast-1
    exact_count: 1
    vpc_subnet_id: "{{ ec2_subnet_id }}"
    count_tag:
      docker-registry: 1
    instance_profile_name: ecsInstanceRole
    instance_tags:
      Name: ECS_docker-registry
      docker-registry: 1
    assign_public_ip: yes

As a result, two instances launched; one of them is configured as I intend, but another has following tags that I don't intend to set.

  • aws:autoscaling:groupName
  • aws:cloudformation:logical-id
  • aws:cloudformation:stack-id
  • aws:cloudformation:stack-name

In addition, I can find these two instances on ECS dashboard.

enter image description here

But its only visible for the cluster "default", and invisible for other clusters.


What I really want to do is;

  • Launch a ECS container instance
  • Register the container instance to a cluster

It's better if I can do the process above with aws-cli, but first I should understand the strange behaviour of container instances and do manually.

3

There are 3 answers

0
AudioBubble On

You can also add an EC2 instance to an ECS cluster manually:

  1. Create a file in /etc/ecs/ecs.config with this line:

    ECS_CLUSTER={{ name }}
    

    Where {{ name }} is the cluster name you want.

  2. Install the ecs agent (package name is "ecs-init") and start it.

The instance will create the cluster if it doesn't already exist.

0
shsu On

You can try looking into using user_data field of the ec2 module.

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html

You will need to have the following in ecs.config before the ecs agent starts up.

#!/bin/bash
echo ECS_CLUSTER=your_cluster_name >> /etc/ecs/ecs.config

0
RomanMinkin On

To register an EC2 container instance in a ECS Cluster you have a few options:

  1. Use special amazon-ecs-optimized AMI to manually launch EC2 instance with custom user data with awscli or AWS Console (read more about Launching an Amazon ECS Container Instance):

        #!/bin/bash    
        echo ECS_CLUSTER=your_cluster_name >> /etc/ecs/ecs.config
    
  2. Use AWS OpsWorks and create ECS Cluster layer since AWS OpsWorks Supports Managing Amazon ECS Container Instances. In this case OpsWoks will any EC2 instance within a layer to work with particular ECS Cluster.

There are few limitations:

  1. Each OpsWorks Stack can have only one ECS Cluster layer
  2. Each EC2 instance can be assign only to one ECS Cluster