Before you can add ECS instances to a cluster you must first go to the EC2 Management Console and create ecs-optimized instances with an IAM role that has the AmazonEC2ContainerServiceforEC2Role policy attached.
Launch Instance button.Community AMIs, search for ecs-optimized, and select the one that best fits your project needs. Any will work. Click next.Configure Instance Details, click on the create new IAM role link and create a new role called ecsInstanceRole.AmazonEC2ContainerServiceforEC2Role policy to that role.default cluster. If you want to launch into your own cluster instead of the default, choose the Advanced Details list and paste the following script into the User data field, replacing your_cluster_name with the name of your cluster.#!/bin/bash
echo ECS_CLUSTER=your_cluster_name >> /etc/ecs/ecs.config
NOTE: If you a creating a web server you will want to create a securityGroup to allow access to port 80.
aws ecr create-repository --repository-name example-repositoryaws ecr get-login --region us-east-1 | shdocker build -t example-image .docker tag example-image:latest example-namespace/example-image:latestdocker push example-namespace/example-image:latestaws ecs register-task-definition --cli-input-json example-task.jsonaws ecs run-task --task-definition example-task{
"family": "example-task",
"containerDefinitions": [
{
"environment": [],
"name": "example-container",
"image": "example-namespace/example-image:latest",
"cpu": 10,
"memory": 500,
"portMappings": [
{
"containerPort": 8080,
"hostPort": 80
}
],
"entryPoint": [],
"essential": true
}
]
}

sudo yum update –y
now to install docker execute yum install
sudo yum install -y docker
sudo service docker start
sudo usermod -a -G docker ec2-user
docker info
sudo yum install -y git
git clone https://github.com/awslabs/ecs-demo-php-simple-app
cd ecs-demo-php-simple-app
verify that Dockerfile exists by listing the directory contents
ls



aws configure
provide AWS Access Key ID, Secret Access key, default region name as per your account
aws ecr get-login --region us-east-1
b) Run the command return as output of previous step
docker build -t amazon-ecs-sample .
(Note the “.” stands for current directory)
b) Run docker images to verify that the image was created correctly and that the image name contains a repository that you can push your changes to the docker image
docker images
c) Run the newly built image. The -p 80:80 option maps the exposed port 80 on the container to port 80 on the host system(Ec2 instance in this case).
docker run -p 80:80 amazon-ecs-sample
Ignore the warning “apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2 for ServerName”
http://<ec2-instance-dns-address>
sudo yum install -y ecs-init
b) Restart the docker daemon
sudo service docker restart
c) Start the ecs-init upstart job
sudo start ecs
d) (Optional) You can verify that the agent is running and see some information on your new container instance with the agent introspection API. Make sure the port 51678 is open in security group.
curl http://localhost:51678/v1/metadata

aws ecs register-task-definition --cli-input-json file://simple-app-task-def.json
e) Go to task definition in ec2 container service page, you ll find the registered task definition
f) Use the following AWS CLI command to run a task with the console-sample-app task definition.
aws ecs run-task --task-definition console-sample-app
g) Open the sample web app in browser, it should be accessible(refer step 19)
Thanks for reading, do share your comments and queries for follow up discussion.