[Part 2] How to setup ECS for a scalable web application

Mindaugas Nakrosis
4 min readNov 14, 2021

--

This is part 2 of a series How to deploy a scalable web application

You can read Part 1 here that explains how ECS works in general.
In this article, I will try to guide you through the steps of setting up ECS infrastructure so we can deploy our scalable web application.

For the disclaimer it is possible to setup everything without manually stepping through each step with ECS First Run Wizard. It performs the same actions with CloudFormation template and makes ECS API calls. We are doing it step by step ourselves in this article so we understand what ECS consists of and how each instance is related to each other.

1. Creating ECS Cluster

An ECS cluster controls VMs (EC2s) — how they are created, removed and updated. Before creating an ECS cluster we will need to create a security group that we will attach to the VMs which use the cluster.

aws ec2 create-security-group — group-name web-ecs-sg — description web-ecs-sg

This will create a security group named web-ecs-sg.

Now we need to create a cluster + EC2 instance that will run inside of it. Assign the web-ecs-sg security group when creating it. Do not forget to select Key pair so you can ssh into the instance later on.

For the Networking VPC settings you can use the default VPC and all the associated Subnets. For the IAM Role use ecsInstanceRole. If it does not exist, create it.

Once you have everything filled out. Press to create the cluster and wait for a few minutes. Once it is created you can see it like this:

2. Creating a repository

We need to create an ECS repository that serves the purpose of holding our docker images that we will push during deployment in a pipeline. You can find the Create repository button here:

Once you create a repository, open it and you will find a View push commands button — click it.

It will provide you with steps how to push your docker image to the repository. Authenticate, dockerize your app, tag it and push it to the repository with the instructions you got provided via push commands modal.

3. Creating a task definition

Task definition defines how to use the docker image what resources to allocate and lot of other useful things that I won’t go into detail here. One task definition can have N docker images in a single task definition. You can find an entry point here:

Let’s select the EC2 instance option:

Task definition can have an optional IAM role attached to it. It would provide control over what AWS services are authorized to be used by the images created by the task definition.

Required RAM and CPU are the limits of what your docker image will use. You can limit the values depending on what EC2 instance you select (for example t3.micro). You can play around with it to find what suits you best.

Finally, add the docker image you pushed into the repository (during step 2).

4. Creating a service

Service is created inside a cluster. It takes care of scaling and load-balancing. It also controls placement of tasks inside of virtual machines.

You can read about every single option inside of the creation form. AWS provides pretty elaborate explanations so I won’t go into details here.

And that’s it! You have your ECS infrastructure setup.

You should be able to see your React web application deployed as a task that is defined by the task definition you created in the service:

Photo by Joshua Hoehne on Unsplash

I will be writing an additional article of how to setup CI/CD with Github Actions that will deploy your web application to the ECS we have setup in this one.

Thanks for reading and keep posted!

--

--

No responses yet