AWS Fargate: Deploy and Run Web API (.NET Core)

Fargate is a serverless compute engine for containers that works with both Amazon ECS and Amazon EKS. With AWS Fargate, we can run applications without managing servers (official information page).

In this post, we will take a step-by-step approach to deploying and running 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 another programming language, most of the deployment information will still apply.

The following picture shows a .NET Core Web API application using Visual Studio.

 .NET Core Web API application using Visual Studio

Once the project is created, I add a token controller:

Add a token controller

The token controller has one simple HTTPGet method as follows:

The token controller has one simple HTTPGet method

Now, we can run the application from Visual Studio. The following Swagger UI shows up and we can see the token endpoint and test it.

Swagger UI

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 Dockerfile to the solution, which we can use to run applications locally inside a container and also can use it to publish an image to Docker Hub.

The following picture shows the application running in a container on my local machine.

Aapplication running in a container on local machine

After running it locally and testing 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.

The 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.

Image is available from Docker Hub


At this point, we have a .NET core web API application, packaged as a Docker image and available on Docker Hub. Next, let's use AWS Fargate to select this image to run the application.

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

AWS Fargate Terms

Let's get ourselves familiarized with a few terms surrounding AWS Fargate and container services.

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

Create an Amazon ECS Cluster That Uses Fargate

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

The following picture shows the entered cluster name and AWS Fargate as the selected infrastructure:

Cluster name and AWS Fargate as the selected infrastructure

Click the Create Cluster button on the bottom of the form, and we’ll have a cluster provisioned for our workloads:

Cluster provisioned for our workloads

That’s all that was needed to set up a cluster.

Deploy a Container Using Fargate

Let's start by creating a new task definition on the ECS web console as shown below.

Create new task definition on the ECS web console

Provide the following information:

Provide information for task definition family field

For the infrastructure section, kept the defaults:

Infrastructure requirements: select defaults

For the container section, I input some details; e.g., the container name (tokengen) and the image URI which is the location of the image in a registry (here, it is pointing to the image on Docker Hub).

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

Specify port 5000

Keep other defaults and click Create. That should result in the following definition of creation:

Task definition

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 the Deploy button as shown below.

Create service from deploy dropdown menu

The following is a screenshot for Create service:

Screenshot for Create service

Here, I selected 3 numbers for desired tasks (meaning it will run 3 containers of tokengenwebapi).

Select 3 desired tasks

To distribute traffic among three instances, we can set up a load balancer as follows:

Load balancer setup

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

Specify endpoint for health check

Networking sections allow us to select VPC, Subnets, and Security Groups as shown below:

Select VPC, Subnets, and Security Groups

Click Create when you have reviewed the choices. The following picture shows the service status:

Service status

Soon, the UI will be updated to reflect the task status.

UI: Task status

The following picture shows that all 3 tasks are running:

All 3 tasks running

We can check the logs from Fargate.

Fargate logs

The following picture shows the network configuration:

Network configuration

The load balancer is set up with a DNS name, and we can use it to access our application running in a container.

Make sure that the security group is set to allow inbound traffic if you want to allow public access to the application.

Security group set to allow inbound traffic

The following picture shows the token endpoint accessed via the browser:

Token endpoint accessed via browser

If I refresh the page, we can see the issuer info and all three instances are responding to incoming HTTP requests.

Issuer info, all 3 instances responding to incoming HTTP requests

CLI Commands

If you have the AWS CLI setup, we use CLI commands to check and update the Fargate infrastructure.

Retrieve Cluster Information

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 the task definition and then update the service.

As you can see, all three containers were running, and then a new revision activity was started.

All 3 containers running and new revision activity started

A few minutes later, we could see that all three tasks were updated and running. This all happened seamlessly without any effort on our part, and all that time the web API application was available.

All 3 tasks updated and running

The following picture shows the application is accessed using a load balancer DNS address:

Application is accessed using a load balancer DNS address

With this, we will end this post. The application source code is available on the previously linked Git repository.

Summary

Fargate is a serverless compute engine for containers that works with both Amazon Elastic Container Service and Amazon Elastic Kubernetes Service. In this post, we covered that with AWS Fargate, we can run applications without managing servers. We saw a step-by-step demo of deploying and running a .NET Core Web API application on Fargate in a highly available environment. All it needed was a Dockerfile for our application, and then Fargate was able to pull the image and run the application from it.

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

 

 

 

 

Top