AWS Fargate – Deploy and Run Web Application (.NET Core)

Introduction

With AWS Fargate, we can run applications without managing servers.

Fargate is a serverless compute engine for containers that works with both Amazon ECS and Amazon EKS.

The official information page can be visited on this link.

In this post, we will take a step by step approach to deploy and run a .NET Core Web API application on AWS Fargate Service.

Typical use cases for Fargate

Fargate supports all of the common container use cases, including microservices architecture applications, batch processing, machine learning applications etc.

Application

For the application, I’ll be using a .NET Core Web API application. But if you have a JAVA application or server application written in other programming language, most of the deployment information will still apply.

Subscriber Content

Following picture shows a .NET Core Web API application using visual studio.

Once project is created, I added a token controller

token controller has one simple HTTP Get method as follows

Now, we can run the application from visual studio and the following swagger UI shows up and we can see that the token endpoint and test it

This is a basic web API with very limited functionality but that’s ok for our demo purposes.

Docker Support

Next, I’ve added a docker file to solution which we can use to run application locally inside a container and also can use it to publish image to docker hub.

Following picture shows the application running in a container on my local machine

after running it locally and test it, we can save the image to Docker Hub or AWS Elastic Container Registry (ECR), so that we can use it in AWS Fargate to run the containers from it.

Application source code is available on this git repository.

I will be using docker hub but feel free to select ECR, as per your requirements.

The following picture shows that the image is available from docker hub

At this point, we have a .NET core web API application, packaged as docker image and available on docker hub. Next, lets use AWS Fargate to select this image to run application.

As mentioned earlier that Fargate is a serverless compute engine for containers.

AWS Fargate Terms

Lets get ourselves familiarize few terms around AWS Fargate and container services.

An Amazon ECS cluster is a logical grouping of tasks or services. We can use clusters to isolate our applications. This way, they don’t use the same underlying infrastructure. When our tasks are run on Fargate, the service also manages our cluster resources.

To deploy applications on Amazon ECS, our application components must be configured to run in containers.

A task definition is a text file in JSON format that describes one or more containers that form the application. We can use it to describe up to a maximum of 10 containers.

A task is the instantiation of a task definition in a cluster. After we create a task definition for our application in Amazon ECS, we can specify the number of tasks to run on our cluster. We can run a standalone task, or run a task as part of a service.

We can use an Amazon ECS service to run and maintain desired number of tasks simultaneously in an Amazon ECS cluster.

Create an Amazon ECS cluster that uses Fargate

Log in to AWS web console and open the Amazon ECS dashboard and click Create Cluster button. It will open a form similar to the following.

Following picture shows the entered cluster name and AWS Fargate is selected Infrastructure:

Click create cluster button on the bottom of form and we’ll have a cluster provisioned for our workloads

That’s all was needed to setup a cluster.

Deploy a container using Fargate

Lets start by creating a new Task definition on ECS web console as shown below

and provide the following information as shown below

for the infrastructure section, kept the defaults

For the container section, I input some details e.g. what’s the container name (tokengen) and the Image URI which is the location of image in a registry (here it is pointing to image on docker hub).

We can also specify port which in this case is 5000.

Keep other defaults and click create and that shall result in following definition creation

So a task definition is created but no containers are running yet.

We can now create a service as follows.

Select the definition and Create service from Deploy button as shown below

The following is screen from for Create service

Here I selected 3 number for desired tasks (meaning it will run 3 containers of tokengen webapi).

to distribute traffic among three instance, we can setup a load balancer as follows

We can setup a target-group for the load balancer to route traffic. For health-check, we can specify an endpoint as shown below:

Networking sections allows use to select VPS, Subnets and Security Groups as shown below

click Create when you have reviewed the choices and following picture shows the service status

Soon UI will be updated to reflect the tasks status

and following picture shows that all 3 tasks are running

We can check the logs from the Fargate

Following picture shows the network configuration

Load balancer is setup with a DNS name and we can use it to access our application running in container.

Make sure that security group is setup to allows inbound traffic, if you want to allow public access to the application

Following picture shows the token endpoint accessed via browser

If I refresh the page, we can see the issuer information changing, meaning that load balancer is routing traffic to different container instances

and all three instances are responding to incoming http requests

CLI Commands

If you have aws cli setup, we cli commands to check and update the Fargate infrastructure

Retrieve cluster information?

 aws ecs describe-clusters --cluster DevCluster

Scale out the Amazon ECS service

When we create the Amazon ECS service, it includes three Amazon ECS task replicas. We can see this by using the describe-services command, which returns three.

Use the update-service command to scale the service to five tasks. Re-run the describe-services command to see the updated five.

aws ecs describe-services --cluster DevCluster --services tokengensrv --query 'services[0].desiredCount'
aws ecs update-service --cluster DevCluster --service tokengensrv --desired-count 5

Rolling Update

During my testing, I updated the image on docker hub and wanted to redeploy the application with new updates. One way to do that is to create a revision of task definition and then update the service.

As you can see that all three containers were running and then a new revision activity is started

a few minutes later we can see that all three tasks are updated and running and this all happen seamlessly without any effort on our part and all that time the web API application was available.

following picture shows application is accessed using load balancer DNS address

With this, we will end this post. Application source code is available on this git repository.

Summary

In this post, we covered that with AWS Fargate, we can run applications without managing servers.

Fargate is a serverless compute engine for containers that works with both Amazon Elastic Container Service and Amazon Elastic Kubernetes Service.

We saw a step by step demo of deploying and running a .NET Core Web API application on Fargate in highly available environment and all it needed was to have a docker file for our application and then Fargate was able to pull the image and run application from it.

Let me know if you have any questions and comments. Till next time, happy coding.


Discover more from Hex Quote

Subscribe to get the latest posts sent to your email.

1 thought on “AWS Fargate – Deploy and Run Web Application (.NET Core)”

Comments are closed.

Discover more from Hex Quote

Subscribe now to keep reading and get access to the full archive.

Continue reading