Connect with us

Get more updates and further details about your project right in your mailbox.

Thank you!
Oops! Something went wrong while submitting the form.
August 17, 2023

Getting Started With AWS Fargate

The best time to establish protocols with your clients is when you onboard them.

Heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

Deploying the application to the web is a burden and maintaining the server is also another big task for the DevOps engineers. In order to avoid the burden to maintain the server when a high load of users are getting in and to avoid the server crash and load time, there are few serverless deployment platforms provided by AWS through which you can deploy your application and don’t care about maintaining the server. Also the another benefit of serverless deployment is that you only pay for what you use. Before that let’s see about AWS, Amazon Web Services is a comprehensive, evolving cloud computing platform provided by Amazon that includes a mixture of infrastructure as a service (IaaS), platform as a service (PaaS) and packaged software as a service (SaaS) offerings. Now let’s get deep into how things are done with AWS Fargate.

Containers & AWS Fargate:

In Fargate we deploy our applications as containers which have a container image of our application. Containers are an abstraction at the app layer that packages code and dependencies together. Containers help developers to use the application without managing the dependencies burden for an application. Now lets see how we can convert a Node.js application into a container image so that we can deploy it into the web.

Converting Node.js Application in to an Container image:

Now lets create a sample Node.js application which has a simple get method which displays “hello from fargate”. The below written node js application is what we are going to convert into container Image.

In order to convert our Node js application into a container image we have to write code yes you got it we have to write a code in a Docker file which will convert our application into a container image.

Docker is an open source platform for building, deploying, and managing containerized applications. Docker hub is where our application image can be visualized and can be triggered to run. Create a new file named “Dockerfile’’ and you shouldn’t change it.

Now lets see what we have to write in the dockerfile in order to get our container image.

The below code is a simple example of what goes into a dockerfile.

Now let’s go through that code:

  • FROM: Sets the base image for further instructions. For the sake of simplicity, we will use an officially supported Node.js image. I am using the exact version mentioned in my package.json, so change it accordingly if you’re using a different node version.
  • WORKDIR: Adds source code from our current working directory to the image.
  • COPY: Copy files and folders from source to destination in the image filesystem. We are copying package.json and package-lock.json. This command ensures that we have a list of dependencies to install in our docker container.
  • RUN: Executes the given command. As we have package.json from the previous step, we can install dependencies in our container.
  • CMD: There can be only one CMD command in one image, which tells the container how to start the application. Notice we have passed as array and the necessary command as elements. This is called an exec form and it allows us to run the command without starting a shell session.
  • For the Node.js base image, we are going to use the official one available in docker hub. (Docker Hub — where users can create their own private/public repositories to contain their images and also can access any open-source image).

Now run the command in the cli which will convert the Node js application to a container image with the name specified:

docker build -t docker-express-app

Our Docker image is created you can look at it through the docker desktop application or by using the command : docker images

Now upload your container image to AWS Elastic Container Registry (ECR). Which is similar to docker hub but an AWS service to store our container image and run it in AWS Fargate. You can also use Amazon Elastic Kubernetes Service for storing and running our container image. But in this article we are only going to look at how to deploy Fargate using our Elastic Container Registry (ECR) and Amazon Elastic Container Service (Amazon ECS).

Upload your container image to AWS Elastic Container Registry (ECR):

At first create a private/public repository using the aws console where we can store the container image.

We can upload our container image to the AWS Elastic Container Registry (ECR) through our aws cli commands by specifying the AWS Elastic Container Registry (ECR) URI.

Steps to upload your docker image to AWS Elastic Container Registry (ECR) :

  • Go to the terminal and login to your ECR profile through your AWS cli. Command to Run:

aws ecr get-login-password — region region | docker login — username AWS — password- stdin aws_account_id.dkr.ecr.region.amazonaws.com

  • Rename your image tag to your repository URI like shown below:

docker tag docker-express-app your-repo-URI

  • Now push your docker image to the AWS ECR by using the following command:

It will take a few minutes after completing the push. You can check your image in the AWS ECR to verify that it is uploaded.

All done now let’s start deploying our application image to Fargate. Let’s see how we can do that.

Deploying the application image to web using the Fargate:

We have a service called ECS in AWS which allows us to use fargate and deploy our application to the web. Create a cluster in AWS ECS. A cluster in ECS can have many services which in our case is Fargate. Let’s create a cluster in the console.

Choose the networking only option since that is what is supported for Fargate.

After this, create a service which will run our application image as a task. In Fargate a task is a container which has the application image. Lets create a service and a task.

Creating a Task definition:

Choose fargate and proceed

  • I have given the basic configuration for our application since it will not have any high intense operations and will not have more visitors.

Now create a container with the image URI that we got from the uploaded image from ECR and give the port mapping the same as what we have used in the node.js application. And click add to create a container,

Now click on create to create the task definition.

Task is successfully created. Now let’s create a service which will run the task definition/container which has the application image to run. Go to your created cluster and create a new service.

Choose fargate and select your task definition and give the basic number of tasks to run as 1 and keep others as default. Next select a VPC and proceed with others as default. You can use a load balancer with Fargate too but for now we are not going to see that. Select a VPC and proceed.

This is where you can select the option where you want your application to scale up or down based on the visitors. For this tutorial I am going to use the scale up feature here.

Give the minimum and maximum number of tasks to run and create a sample policy which states

whether the application should scale based on the CPU or Memory utilization and give target value for when to deploy a new task to scale the application. Now click next step and review all the options and create a service.

Now our service is created and our task is up and running. Now click on the task id and copy the public IP address provided by AWS. You can see our get statement from our Node js application.

We have successfully deployed our Node.js application on the web using Fargate.

CodeStax.Ai
Profile
August 17, 2023
-
6
min read
Subscribe to our newsletter
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Share this article:

More articles