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.
Once the project is created, I add a token controller:
The token controller has one simple HTTPGet
method as follows:
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.
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.
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.
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.
- 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 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:
Click the Create Cluster button on the bottom of the form, and we’ll have a 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.
Provide the following information:
For the infrastructure section, kept the 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.
Keep other defaults and click Create. That should result in the following definition of 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 the Deploy button as shown below.
The following is a screenshot for Create service:
Here, I selected 3 numbers for desired tasks (meaning it will run 3 containers of tokengenwebapi).
To distribute traffic among three instances, we can set up a load balancer as follows:
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:
Networking sections allow us to select VPC, Subnets, and Security Groups as shown below:
Click Create when you have reviewed the choices. The following picture shows the service status:
Soon, the UI will be updated to reflect the task status.
The following picture shows that all 3 tasks are running:
We can check the logs from Fargate.
The following picture shows the 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.
The following picture shows the token endpoint accessed via the browser:
If I refresh the page, we can see the issuer info and all three instances are 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
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 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.
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.
The following picture shows the 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.