Skip to main content

From Code to Cloud: Deploying a .NET Item API on AWS EKS Like a Pro 🚀

 

In the fast-paced world of cloud-native development, deploying your .NET applications in a scalable, resilient way is the name of the game. And what's better than leveraging AWS EKS (Elastic Kubernetes Service) to deploy your .NET Core Item API to the cloud in a way that’s as flexible as it is powerful? Let’s walk through the process of how you can get your .NET Core Item API running in AWS EKS like a pro — and have some fun along the way!

Why AWS EKS? And Why Kubernetes? 🤔

Before we dive into the nitty-gritty, let's set the stage. Kubernetes has become the de facto standard for managing containerized applications, and AWS EKS is a managed service that lets you run Kubernetes clusters without needing to maintain the control plane (AWS takes care of that!).

Here’s why you’d want to use AWS EKS for your .NET Core API:

  • Scalability: Easily scale your application up or down based on demand.
  • Resilience: Automated recovery and failover in case of issues.
  • Cost-Efficiency: Pay only for the worker nodes and resources you actually use.
  • Ecosystem Integration: Seamless integration with AWS services like IAM, VPC, and CloudWatch for a smoother experience.

So, with AWS EKS, you get the benefits of Kubernetes without the overhead of managing the infrastructure. Sounds cool, right? 😎

Let’s Get Our Hands Dirty: Deploying the .NET Core Item API to EKS 🛠️

Step 1: Prepare Your .NET Core Item API

Here’s a super simple Item API that exposes a few endpoints to create, update, and fetch items.

public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

[ApiController]
[Route("api/[controller]")]
public class ItemController : ControllerBase
{
    private static List<Item> items = new List<Item>
    {
        new Item { Id = 1, Name = "Item1", Price = 10 },
        new Item { Id = 2, Name = "Item2", Price = 20 },
    };

    [HttpGet]
    public IActionResult GetItems()
    {
        return Ok(items);
    }

    [HttpPost]
    public IActionResult CreateItem(Item newItem)
    {
        items.Add(newItem);
        return CreatedAtAction(nameof(GetItems), new { id = newItem.Id }, newItem);
    }
}

Step 2: Dockerize the Application 🐳

Now that you have your API ready, the next step is to containerize it using Docker. Here’s a simple Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "ItemApi.dll"]
Once your Dockerfile is ready, build the image:
docker build -t item-api .
And then push it to a container registry (e.g., Amazon ECR or Docker Hub):
docker tag item-api:latest YOUR_ECR_REPO_URL/item-api:latest
docker push YOUR_ECR_REPO_URL/item-api:latest

Step 3: Set Up Your EKS Cluster ⛅

If you haven't already created an EKS cluster, here’s a simple command to get you started (assuming you have the AWS CLI and eksctl set up):

eksctl create cluster --name dotnet-eks-cluster --region us-west-2 --nodes 3

This command will spin up an EKS cluster with 3 worker nodes.

Step 4: Deploy Your Item API to the EKS Cluster 🛳️

Once your EKS cluster is up, the next step is deploying your Item API to the cluster. First, create a Kubernetes deployment and service for your API.

Kubernetes Deployment (item-api-deployment.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: item-api-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: item-api
  template:
    metadata:
      labels:
        app: item-api
    spec:
      containers:
      - name: item-api
        image: YOUR_ECR_REPO_URL/item-api:latest
        ports:
        - containerPort: 80

Kubernetes Service (item-api-service.yaml)

apiVersion: v1
kind: Service
metadata:
  name: item-api-service
spec:
  type: LoadBalancer
  selector:
    app: item-api
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

Apply these files to your cluster:

kubectl apply -f item-api-deployment.yaml
kubectl apply -f item-api-service.yaml

Step 5: Access Your Item API 🎉

Once the service is created, AWS will provision an Elastic Load Balancer (ELB) for your Item API, and you can use the external IP to access the API from anywhere.

You can find the external IP with:

kubectl get svc item-api-service

Once you have the IP, hit the following URL to get the list of items:

http://EXTERNAL_IP/api/item

Scaling with EKS: The Magic of Kubernetes 🪄

One of the coolest things about deploying your .NET Core API on EKS is scalability. Need more instances of your API? Simply adjust the number of replicas in your Kubernetes deployment, and Kubernetes will scale up your app to meet demand.

For example, to scale your Item API to 10 replicas, just run:

kubectl scale deployment item-api-deployment --replicas=10

Kubernetes and EKS will automatically handle the rest, ensuring your API can handle whatever traffic comes its way.

Final Thoughts: Flexibility Meets Power in AWS EKS

Deploying a .NET Core application on AWS EKS combines the best of both worlds: the flexibility and scalability of Kubernetes with the rock-solid infrastructure of AWS. Whether you’re running a small app or scaling out a massive microservice architecture, EKS provides the tools to do it efficiently.

And now that you’ve seen how easy it is to deploy your Item API on AWS EKS, what’s stopping you from taking your next app to the cloud? 🛠️🔥

What’s Next?

In future articles, we’ll explore more advanced topics like monitoring, autoscaling, and deploying complex microservice architectures with AWS EKS. Stay tuned for hands-on code snippets and step-by-step guides to help you master cloud-native development with .NET and AWS! 🌐

Comments

Popular posts from this blog

Implementing and Integrating RabbitMQ in .NET Core Application: Shopping Cart and Order API

RabbitMQ is a robust message broker that enables communication between services in a decoupled, reliable manner. In this guide, we’ll implement RabbitMQ in a .NET Core application to connect two microservices: Shopping Cart API (Producer) and Order API (Consumer). 1. Prerequisites Install RabbitMQ locally or on a server. Default Management UI: http://localhost:15672 Default Credentials: guest/guest Install the RabbitMQ.Client package for .NET: dotnet add package RabbitMQ.Client 2. Architecture Overview Shopping Cart API (Producer): Sends a message when a user places an order. RabbitMQ : Acts as the broker to hold the message. Order API (Consumer): Receives the message and processes the order. 3. RabbitMQ Producer: Shopping Cart API Step 1: Install RabbitMQ.Client Ensure the RabbitMQ client library is installed: dotnet add package RabbitMQ.Client Step 2: Create the Producer Service Add a RabbitMQProducer class to send messages. RabbitMQProducer.cs : using RabbitMQ.Client; usin...

How Does My .NET Core Application Build Once and Run Everywhere?

One of the most powerful features of .NET Core is its cross-platform nature. Unlike the traditional .NET Framework, which was limited to Windows, .NET Core allows you to build your application once and run it on Windows , Linux , or macOS . This makes it an excellent choice for modern, scalable, and portable applications. In this blog, we’ll explore how .NET Core achieves this, the underlying architecture, and how you can leverage it to make your applications truly cross-platform. Key Features of .NET Core for Cross-Platform Development Platform Independence : .NET Core Runtime is available for multiple platforms (Windows, Linux, macOS). Applications can run seamlessly without platform-specific adjustments. Build Once, Run Anywhere : Compile your code once and deploy it on any OS with minimal effort. Self-Contained Deployment : .NET Core apps can include the runtime in the deployment package, making them independent of the host system's installed runtime. Standardized Libraries ...

Clean Architecture: What It Is and How It Differs from Microservices

In the tech world, buzzwords like   Clean Architecture   and   Microservices   often dominate discussions about building scalable, maintainable applications. But what exactly is Clean Architecture? How does it compare to Microservices? And most importantly, is it more efficient? Let’s break it all down, from understanding the core principles of Clean Architecture to comparing it with Microservices. By the end of this blog, you’ll know when to use each and why Clean Architecture might just be the silent hero your projects need. What is Clean Architecture? Clean Architecture  is a design paradigm introduced by Robert C. Martin (Uncle Bob) in his book  Clean Architecture: A Craftsman’s Guide to Software Structure and Design . It’s an evolution of layered architecture, focusing on organizing code in a way that makes it  flexible ,  testable , and  easy to maintain . Core Principles of Clean Architecture Dependency Inversion : High-level modules s...