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.

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.
To register an EC2 container instance in a ECS Cluster you have a few options:
Use special amazon-ecs-optimized AMI to manually launch EC2 instance with custom user data with
awsclior AWS Console (read more about Launching an Amazon ECS Container Instance):#!/bin/bash echo ECS_CLUSTER=your_cluster_name >> /etc/ecs/ecs.configUse 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: