Setting Up a Minimal Amazon ECS Cluster to Manage Multiple Applications
Tags: aws, ecs, alb, tutorial
Published: 2019-12-26 02:09:29

---
title: Setting Up a Minimal Amazon ECS Cluster to Manage Multiple Applications
published: true
description: This tutorial walks thru step by step to set up a minimal Amazon ECS cluster to manage multiple applications. 
tags: aws, ecs, alb, tutorial
---

## Outline

1. [What is Amazon ECS?](#heading--what-is-amazon-ecs)
1. [Creating an ECS Cluster](#heading--creating-an-ecs-cluster)
1. [Creating an Application Load Balancer (ALB)](#heading--creating-an-application-load-balancer-alb)
1. [Setting up an Application in the ECS cluster](#heading--setting-up-an-application-in-the-ecs-cluster)
1. [Adding a Domain to ALB](#heading--adding-a-domain-to-alb)
1. [Testing the Application](#heading--testing-the-application)

<h2 id="heading--what-is-amazon-ecs">What is Amazon ECS?</h2>

I will not explain too much about it here. It's not the main point of this tutorial. Please read the quote below to get some idea what it is. 😆

> Amazon Elastic Container Service (Amazon ECS) is a fully managed container orchestration service. Customers such as Duolingo, Samsung, GE, and Cook Pad use ECS to run their most sensitive and mission critical applications because of its security, reliability, and scalability.

**Reference:** [Amazon ECS](https://aws.amazon.com/ecs/)


<h2 id="heading--creating-an-ecs-cluster">Creating an ECS Cluster</h2>

Go to the ECS service and click "Create Cluster".

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/s5nnnnnrtqbqomu5k05q.png)

Select the "EC2 Linux + Networking" template. Don't worry if we don't see AWS Fargate here, we can set it later.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/62gg0xoxsh5psz1punoo.png)

Fill in the information below. If we want to SSH into the EC2 instance, we need to specify the key pair. In this tutorial, we don't need it.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/apfdumn9i4sdn9ckn2yv.png)

For networking, let's create a new VPC.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ari7mni7qy3vm6drscfg.png)

Use the default IAM role `ecsInstanceRole`. Specify the tags, so we can track the bill and keep our resources organized.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/41nfgxcispg3y5gs5p3l.png)

Once we finish, we should see the launch status as shown in the screenshot below.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/kzzlt2kcslbkhetpvzph.png)

We can then view the newly created cluster.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/0onrme4juuimxtn0kkah.png)

<h2 id="heading--creating-an-application-load-balancer-alb">Creating an Application Load Balancer (ALB)</h2>

We need a load balancer to route requests to the destination. Let's create one. Go to the EC2 service then find the "Load Balancer" on the menu on the left. Click "Create Load Balancer".

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/8f3bxijxne6qjpgki0cp.png)

Choose the Application Load Balancer.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/6k3t3r4ln1ulwx5zksit.png)

Configure the ALB and choose the VPC we created from above in the Availability Zone section. Again, don't forget to add the tags.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/r8xiiyvq3mfxlila00zh.png)

In the step 2, we need a SSL certificate for it. We can set it later and can skip this for now.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/a4rb8sxmrjt14npfucvl.png)

For the security group, just create a new one.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/lrrewo657jxo7pbsx8x9.png)

For the routing configuration, create a new target group.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/8sb6orncfv5ffijje5tv.png)

We can skip the register targets step. ECS will do it for us when we create a new service in ECS.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/15e9a1n53q29uuu08824.png)

Wait until the ALB is provisioned.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/94se0qng158w1zkd6953.png)

Once the provision is finished, what we need to do is to update the security group rule on the EC2 instances where containers will run in the ECS cluster, so the ALB can access.

Go back to the ECS console, select the cluster, click on the tab "ECS Instances". We will see an instance is running. Click the ECS instance ID.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ei2ma00fpzrxhnzegk6m.png)

Click on the security group name.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/frrzmmobrn0v0ohz3nma.png)

Edit the inbound rule.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/m1rnu2c635e8osmt2qt9.png)

Select the security group previously created for the ALB. We're allowing all the traffic from the ALB to the instances since when working with ECS we can use a dynamic port mapping feature in order to run more containers with the same image in the same EC2 instances, so when starting our task, we won't specify any port to run applications and the ECS will do it for us.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/to4ca3k8nixwvjsr0hbf.png)

<h2 id="heading--setting-up-an-application-in-the-ecs-cluster">Setting up an Application in the ECS cluster</h2>

In order to set up an application in the ECS cluster, we need to:

1. Create a task definition;
1. Create a service.

### Creating a Task Definition

Go to the ECS console and click the "Task Definitions" on the left menu.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/utn7t7dxuex8gwamxkj4.png)

Choose the EC2 launch type.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/dmvify1x5t4fkh5l85nn.png)

Configure the task and container definitions.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/bm9mahcxtqsqqc746j5d.png)

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/90433ljl0bfvtleqdg6l.png)

Click "Add container" then.

To make it simple, we're using [this public Docker image](https://hub.docker.com/repository/docker/zkan/bangkok) in this tutorial. Note that we can actually use any Docker image from either a public or private Docker image repository here.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/es7z3h7ddajx2kmjvfc4.png)

We intentionally set the host port to 0 since it will be used to facilitate dynamic port allocation. The ALB dynamically allocate a port during the task placement.

Add the tags.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/0tkx7qadcpov34m060j7.png)

Check the "Auto-configure CloudWatch Logs", so we can view the log in CloudWatch. Just keep the default settings.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/e4ietz4in9kyxk1m2kwm.png)

After that, we add the container and create the task definition.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ve0u55js0kji2r9me94n.png)

### Creating a Service

Let's create a service. This allow us to run and maintain a specified number of instances of a task definition simultaneously in the ECS cluster. 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/s9f870ejfqbmc75ir82h.png)

Configure the service.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/9xmt7xt9ei02pkz3o7aa.png)

For Load balancing, choose the Application Load Balancer. Click "Add to load balancer" to add the container to the load balancer.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/98lac9xgv5n2simk1qil.png)

Configure the container settings as follows.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/110moeyodbi64xv97adl.png)

Uncheck the "Enable service discovery integration" option. This is not necessary for now since we won't do anything in the Route 53 now.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/syut1h7vdah1i7f8suq8.png)

After that, create the service.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/arg9m9101595bks3vnx7.png)

Our application should be up and running soon.

<h2 id="heading--adding-a-domain-to-alb">Adding a Domain to ALB</h2>

This is the last step before we can access our application from the Internet. Go to our ALB and edit rules.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/8mz1smijxcoiwfef2nt0.png)

Create a new rule or edit the existing one like this. In this tutorial, choose the host header for the condition, then we set the domain `my-test-zkan-bkk.prontotools.io`. 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/j9lcw6ipjpqotsi8zxo3.png)

Click "Update". 

<h2 id="heading--testing-the-application">Testing the Application</h2>

If we have our own domain, we can point it to the ALB's DNS name.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/a53secrru1por4l8kdwh.png)

If not, we can modify our hosts file. Get the ALB's IP with `nslookup`.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/wvvu4jr6zmt7a81lbbk6.png)

Modify the hosts file.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/uyjvnqe1u53o002y6d8e.png)

Done! 🍻🎄🎉

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/wvs86kcxr3p5frb99cin.png)

If we want to add a new application, just repeat the step 3 to 5 again. 😉

I've drawn a simple diagram to show how it will look like after complete those steps above. 👇

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/pjuklfa51pv4qud3db0z.PNG)

Hope this helps! 😇