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.

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

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

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

The token controller has one simple HTTPGet method as follows:

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

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.

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

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.

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

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.

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


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:

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

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

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

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.

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

Provide the following information:

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

For the infrastructure section, kept the defaults:

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

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.

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

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

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

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.

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

The following is a screenshot for Create service:

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

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

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

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

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

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:

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

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

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

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

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

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

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

The following picture shows that all 3 tasks are running:

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

We can check the logs from Fargate.

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

The following picture shows the network configuration:

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

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.

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

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

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

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

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

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.

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

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.

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

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

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

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