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

Optional Parameters in C# — Writing Flexible and Clean Methods

Hello, .NET developers! 👋 How often have you created multiple method overloads just to handle slightly different cases? Maybe one method accepts two parameters, another three, and one more adds a flag for debugging? That’s a lot of code duplication for something that can be solved beautifully with optional parameters . Optional parameters in C# let you define default values for method arguments. When a caller doesn’t pass a value, the compiler automatically substitutes the default. This feature helps keep your APIs simple, readable, and maintainable. 🎥 Explore more on YouTube : DotNet Full Stack Dev Understanding Optional Parameters Optional parameters are defined by assigning default values in the method signature. When calling the method, you can omit those parameters if you’re okay with the defaults. Example public class Logger { public void Log(string message, string level = "INFO", bool writeToFile = false) ...

.NET 10: Your Ultimate Guide to the Coolest New Features (with Real-World Goodies!)

 Hey .NET warriors! 🤓 Are you ready to explore the latest and greatest features that .NET 10 and C# 14 bring to the table? Whether you're a seasoned developer or just starting out, this guide will show you how .NET 10 makes your apps faster, safer, and more productive — with real-world examples to boot! So grab your coffee ☕️ and let’s dive into the awesome . 💪 1️⃣ JIT Compiler Superpowers — Lightning-Fast Apps .NET 10 is all about speed . The Just-In-Time (JIT) compiler has been turbocharged with: Stack Allocation for Small Arrays 🗂️ Think fewer heap allocations, less garbage collection, and blazing-fast performance . Better Code Layout 🔥 Hot code paths are now smarter, meaning faster method calls and fewer CPU cache misses. 💡 Why you care: Your APIs, desktop apps, and services now respond quicker — giving users a snappy experience . 2️⃣ Say Hello to C# 14 — More Power in Your Syntax .NET 10 ships with C# 14 , and it’s packed with developer goodies: Field-Bac...

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