DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Amazon Lightsail: Virtual Cloud Server
  • Adding a Custom Domain and SSL to AWS EC2
  • Alexa Skill With .NET Core
  • Zero Trust for AWS NLBs: Why It Matters and How to Do It

Trending

  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  • Measuring the Impact of AI on Software Engineering Productivity
  • Start Coding With Google Cloud Workstations
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. AWS Fargate: Deploy and Run Web API (.NET Core)

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

Explore this simple step-by-step guide to deploying a .NET Core Web API to AWS ECS Fargate serverless container service.

By 
Jawad Hasan Shani user avatar
Jawad Hasan Shani
DZone Core CORE ·
Mar. 19, 24 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
19.3K Views

Join the DZone community and get the full member experience.

Join For Free

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.

  • 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:

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

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

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.

AWS Web API Docker (software) Load balancing (computing) .NET

Published at DZone with permission of Jawad Hasan Shani. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Amazon Lightsail: Virtual Cloud Server
  • Adding a Custom Domain and SSL to AWS EC2
  • Alexa Skill With .NET Core
  • Zero Trust for AWS NLBs: Why It Matters and How to Do It

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!