Deploy Docker Container as serverless architecture to AWS Fargate

Dr. Tri Basuki Kurniawan
TheLorry Data, Tech & Product
12 min readFeb 15, 2021

Serverless containerized deployment of a Python application using AWS Fargate.

This introductory guide walks through the process of deploying the docker container to AWS Fargate. The main focus is on creating a docker container and deployment process to AWS Fargate rather than automatically build a pipeline or continuous deployment (CD). The code repository accompanies the guide to get you started with this tutorial. Basic knowledge of AWS concepts by the reader is recommended.

Let us start with our tutorial.

Prepare the simple project

You can easily clone the sample project from our Github here. Or you can follow this tutorial to create the basic application which will be used in this tutorial. Please use your terminal to execute this command in your working directory.

git clone https://github.com/tribasuki74/mypython.gitCloning into 'mypython'...
remote: Enumerating objects: 1914, done.
remote: Counting objects: 100% (1914/1914), done.
remote: Compressing objects: 100% (1702/1702), done.
remote: Total 1914 (delta 125), reused 1902 (delta 121), pack-reused 0
Receiving objects: 100% (1914/1914), 7.99 MiB | 2.32 MiB/s, done.
Resolving deltas: 100% (125/125), done.

Next, lets us create Dockerfile for this application.

FROM python:3WORKDIR /appCOPY . .RUN python3 -m pip install --upgrade pipRUN python3 -m pip install --no-cache-dir -r requirements.txtENV FLASK_APP=mypython.pyENV FLASK_ENV=developmentENV FLASK_RUN_HOST=0.0.0.0EXPOSE 5000CMD ["flask", "run"]

To create the docker image, you can run this command

docker build -t mypython .

After a certain time of the process, if all processes are successful, you will get the docker images. To check that image, you should check it by this command

docker images -aREPOSITORY                    TAG                   IMAGE ID            CREATED             SIZE
mypython latest 24b7006722b4 15 minutes ago 929MB

Lets us running our docker image

docker run -d --name mypython -p 5000:5000 mypython:latest

Ok, we will check it using our browser. Please type localhost:5000/into your browser. If everything is fine, you will get this display.

the result of our application which running at docker image

and you also can try another route localhost:5000/add?data=1,2,3,4,5.

the result for route /add

Now that we have a working simple docker container, let’s see how it fits in the context of AWS Fargate.

Introduction

AWS, which stands for Amazon Web Services, provides many great services, including Elastic Container Registration (ECR), where we can register our docker images, same as Docker Hub functionality and Elastic Container Service (ECS), which is consist of two different services, Fargate and EC2. In this tutorial, we will deploy our docker image only to the Fargate service in ECS.

Create Free AWS Account

First, we need an AWS account. Please click this link to start. You will bring it to this page.

Register page to AWS Account

The AWS provides 12 months of free tier access, which you can use for this tutorial.

After you finished the registration process and login, you will be directed to the main page.

AWS dashboard

Register Docker Image to ECR

Please select ECR service by type ECR in the search area and then click on the ECR title. You will be directed to the ECR dashboard.

Search for ECR service
ECR dashboard

Lets us create a new repository by click [Create repository] button (which has orange color) at the right top of the dashboard.

Please fill the Repository name with mypython and set Enabled for Tag immutability and Scan on push properties, and then press [Create repository] button at the bottom of the form.

Select the mypython repository

Select the mypython repository and then click [View push commands] button to see the instruction on how to push our local docker images into the ECR.

instruction on how to push our local docker images

There are two types of commands: the macOS or Linux platform and the other one for the Windows platform. This tutorial assumes you are using the macOS or Linux platform; if not, please refer to the instruction for Windows.

There are 4 (four) steps. You can copy (by clicking the copy icon on the right of the command) and paste it to your terminal. If you have an error, it probably you not configure your security information yet.
Please set up your configuration by type command

aws configure

and followed by input the AWS Access Key ID, AWS Secret Access Key from the information that you obtain when you create your account. Also, we need to input the Default region name (you can choose based on your region setting) and Default output format which you can type json as an input.

If everything is gone well, you will get a result similar to this.

mypython docker image

Deploy the Docker Image to ECS

Similar to the previous step, now we need to access ECS service. Please search in the search area by typing ECS and then click on the ECS title. You will be directed to ECS dashboard.

ECS dashboard

First, please click the Cluster on the left side of the sidebar’s menu to create our cluster and then click [Create cluster] button.

select cluster template

Please select [Network only] tab under Select cluster template and then click [Next step] button on the bottom of the screen.

Please give a name for your cluster. In this tutorial, we use mypython-cluster as cluster’s name. Click [Create] button to create our cluster.

cluster name setting
cluster successfully create

The cluster will be created, and then you can view the cluster by clicking the [View Cluster] button.

cluster configuration

Creation of Task Definition

Next, we can start creating Task Definitions and Services to run our application. Please click the Task Definition sidebar menu at the left of the display and then click [Create new Task Definition] button to start to create a new one.

Task Definitions setting
Type compatibility selection form

Since we need to deploy to AWS Fargate, we select Fargate tab and then click [Next step] button.

Please use mypython-tasks as task’s name and Task Role, we no need setting anything because of our application no need that for right now. For Network Mode, already set to awsvpc as default. Then, for the Task execution role, we can set Create new role to create a new role for us.

need to specify the memory and CPU capacity for our container

Next, we need to specify how much memory and CPU you need for this container. In this tutorial, please use 1GB for Task memory and 0.5vCPU for Task CPU. After this, we need to add our container, which previously already we push into our ECR. Please click [Add container] button.

Add container page
copy from here and paste into Image parameter in Add container page

For container definition, first, we need to define a name for the container; in this tutorial, we use mypython-container. For the Image setting, we need to supply a link which our docker images in ECR. Please copy from our images repository and paste it here. The last one is we need to define a port number exposed by the docker image that we already defined in our Dockerfile. In this tutorial, we use 5000. Next, we click [Add] button.

list of our container

Our containers will be displayed in the list of containers. To create the Task Definition, please click [Create] button.

Then we will get results as display above.
Next, click [View task definition] button to see our task definition configuration just we created.

our task definition configuration

Creation of Load Balancer

The next step is creating the service, but for doing that, we need a load balancer, which will be used in the process of creating the service. To create the load balancer, we can search in the AWS services search area by typing EC2 load balancer.

search AWS services for EC2 load balancer
Load balancer dashboard

Please click [Create Load Balancer] button to start to create a new load balancer.

load balancer type selection

Please select Application Load Balancer by clicking the [Create] button.

load balancer configuration page

We need to provide the name of our load balancer. In this tutorial, we used mypython-alb, which alb is standing for application load balancer. By default, we will use HTTP with port 80 for our load balancer, so our application's access will be managed from this port.

configure load balancer configuration page

We also need to set Availability Zones by checked all provided availability zones, and AWS will automatically be assigned the IPv4.
Next, we click [Next: Configure Security Setting] button.

configure security setting page

We still continue. Please click [Next: Configure Security Group] button.

configure security groups page

On this page, first, we need to check the Create a new security group and then give a name for that. In this tutorial, we usemypython-albsg. Then we select HTTP for the type of rule, which will have port 80 as default. To continue the process, we click [Next: Configure Routing] button.

configuration routing page

Since, by default, we don’t yet have any Target group, we need to create a new one. So, we will define a name for this new target group, called mypython-albtg. Then we should select IP for the Target type. This selection is because the service which we will be created initiates a task from within our vpc.
The last one, we need to define the path where the health code was placed.
Click [Next: Regular Targets] button to continue.

Register Target page

We don’t need to change anything here. Just click [Next: Review] button and then click the [Create] button on the next page. We will see the process of load balancer will be created.

Load balancer creation status page

Just click [Close] button. Congratulations, we already successfully created our load balancer. Next, we will use our load balancer on the creation of services.

list of our load balancer

Creation of Services

To create the service, please click the Cluster sidebar menu and then click our cluster’s name.

our created cluster’s
cluster’s configuration

Please click [Create] button in the Services tab to create new services for our cluster.

Configuration service page (part 1)
Configuration service page (part 2)

First, we set Launch Type as FARGATE. Next, we select mypython-tasks as Task Definition and select the latest’s tag for Revision. For cluster, default is our current cluster is mypython-cluster, and we give our service name as mypython-service. The last one, we need to define how many tasks will be managed by these services. We set it 2, which means the services will manage 2 of the tasks, which if the first task fails, the second task will be running.
Please click [Nest step] button to continue the process.

configuration network page (part 1)

On this page, we need to select our cluster VPC, which based on our load balancer already created in the previous process is vpc-d77892b1 (see image below).

VPC ID which will be used on our network service

Then, we set the subnet; in this case, we select all available subnet for our region. Next, we click [Edit] button to set our security group, which we already created when we create our load balancer.

Configuration security groups page

We select Select existing security groupfor Assigned security groups, and then from a list, we select our load balancer security group, which is mypython-albsg, and then click [Save] button to back to our Configuration network page.

configuration network page (part 2)

Then, we set 0 to Health check grace period, that means we will ignore the checking it process. For Load balancer type, we select Application Load Balanacer.

configuration network page (part 3)

Then, for the next set, we need to select our load balancer, mypython-alb. For Container to load balancer, we will add our container. mypython-container:5000:5000. Please click [Add to load balancer] button.

Container to the load balancer page

For our container, we select 80:HTTP from a dropdown component, and also we select our Target group name, which is mypython-albtg.

For Enable service discovery integration, we should not check because for right now, we no need any other microservices in our application. But, this feature can be handy when we used other microservices in our application.
Please click [Next step] to continue.

We don’t need to change anything on this page, and please continue by clicking [Next step] button.

Configuration network review page

And then, we click [Create Service] button.

Launch Status of created service

We will get the result page. Congratulation again, we did it.
We can see our service detail by clicking the [View Service] button.

our mypython-service

We can see here; there are two tasks to have the status PENDING. It should need more time to be PROVISIONING, and then the status is RUNNING.

two of our task already RUNNING

Now, we will back to our load balancer. Please going back to EC2 dashboard.

EC2 dashboard shows Load Balancers sidebar menu

Please click Load Balancer sidebar menu.

list of Load balancer

Check our load balancer, and then in Description tab, click the copy icon to copy our DNS name. Then please paste it into your browser. You will get a result similar to the image below.

the running application from the DNS load balancer name

Congratulations, we have finished our tutorial. Good job!

Summary

Setting load balancer with services that running two tasks simultaneously will give us high availability to our application. That means our load balancer will have two available services which always ready to serve our request. If one of the tasks fails, the other will be initiated right aways to serve.

Hopefully, this article will help you learn more about AWS Fargate as a serverless architecture and learn about Docker and Docker image technologies.

Thank you for your time!

--

--

Dr. Tri Basuki Kurniawan
TheLorry Data, Tech & Product

Loves artificial intelligence learning including optimization, data science and programming