Devops Tools

How to deploy Containerized Applications with Amazon ECS

In this blog, we will deploy a docker container in Amazon ECS. I am using AWS EC2 instance as my local workstation

Check out my Youtube video for this blog.

Prerequisite

  1. Configure aws credential
  2. Install aws cli version 2
  3. Clone this git repo

Agenda:

  1. Create and Push to ECR repository
  2. ECS cluster
  3. Create a Task Definition
  4. Run the task

Create a ECR repository

Go to AWS > ECR > Create a repository 

Click on your repository

View push commands

You will see the below commands to push your docker image to the ECR repository

clone the git repo
docker build -t sample_ecs .
docker tag sample_ecs public.ecr.aws/w0f5g4k6/deploy:v7'
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS -p $(aws ecr get-login-password --region us-east-1)
docker push public.ecr.aws/w0f5g4k6/deploy:v7

Run the image locally to test

docker run -it --rm -d -p 8089:8080 --name samplenginx_c public.ecr.aws/w0f5g4k6/deploy:v7

You should be able to open the url http://localhost:8089/LoginWebApp-1

Create ECS Cluster

These are the four critical items in ECS you need to know:

  • ECS clusters
  • Task definitions
  • Tasks
  • ECS services

AWS ECS Fargate

AWS ECS Fargate is a compute engine for deploying containers without configuring any servers.

Fargate makes the process of deploying containerized applications simple because you don’t need to provision servers, storage, and other infrastructure.

The only thing you need to do is provide AWS Fargate with a container image and deploying it as a service or a single task (container) to ECS.

ECS EC2

You need to provision EC2 instances that will run Docker; act as your container hosts; and manage storage, firewalls, and networking.

The setup time/effort to get an ECS EC2 cluster up and running is about ten times more than running ECS Fargate.

NOTE: use an ECS EC2 cluster over Fargate if you have very specific requirements and you need more control on the servers

Create ECS Cluster

Go to AWS -> ECS — Create Cluster

Select Networking Only option

Click on Create and you should be able to see below page

TASK DEFINITIONS

To run a Docker container in ECS, you need a task definition that defines

  • which image to use in the container, 
  • how much memory and CPU the container will use,
  •  the launch type (Fargate or EC2), 
  • networking options (the subnet and security group), 
  • logging options, 
  • the commands to run, 
  • and the volumes to use.

You can create task definitions without an existing ECS cluster also.

Create new Task Definition

Let’s create a new Task Definition with launch type as Fargate and use our image that we have pushed in our ECR

Click on Add -> Create and you should be able to see the below screen

Tasks

If you need to run a single container without load balancing and more advanced features, ECS tasks can get the job done.

Tasks are designed to help you deploy single containers; 

if you need a more advanced feature such as multiple containers, you use the next type of service, which is an ECS service.

Tasks are also important; however, they are limited and not likely to be used in large or production environments.

Run a Task

Now go to your task definition — Click on Action — Run task

Edit the security group if exposed on different port

Now you can see the status of your tasks

Click on Task ID to check the logs and also to find the public IP

You can browse the url using http://publicip:8080/LoginWebApp-1

Make sure the security group is open for the port 8080 for this task. You can click on ENI from the task details to find which security group is tagged to this task.

ECS SERVICES

To run a group of containers in ECS with multiple instances and load balancing, you create a service from a task definition and run your containers.

Click create with all default settings

Now you can see the service is created and 2 tasks are running. As we have not created any loadbalancer so you can access the application from the task public IP.

Please follow and like us: