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

C# : How can we access private method outside class

Introduction In object-oriented programming, encapsulation is a fundamental principle that restricts direct access to the internal implementation details of a class. Private methods, being part of this internal implementation, are designed to be accessible only within the confines of the class they belong to. However, there might be scenarios where you need to access a private method from outside the class. In this blog post, we'll explore several techniques to achieve this in C#. 1. Reflection: A Powerful Yet Delicate Approach Reflection is a mechanism in C# that allows inspecting and interacting with metadata about types, fields, properties, and methods. While it provides a way to access private methods, it should be used cautiously due to its potential impact on maintainability and performance. using System ; using System . Reflection ; public class MyClass { private void PrivateMethod ( ) { Console . WriteLine ( "This is a private method."

C# : Understanding Types of Classes

In C#, classes serve as the building blocks of object-oriented programming, providing a blueprint for creating objects. Understanding the types of classes and their applications is crucial for designing robust and maintainable software. In this blog, we’ll delve into various types of classes in C#, accompanied by real-world scenarios and code snippets for a practical understanding. 1. Regular (Instance) Classes Definition: Regular classes are the most common type and are used to create instances or objects. They can contain fields, properties, methods, and other members. Example Scenario: A Person class representing individual persons with properties like Name and Age. public class Person { public string Name { get ; set ; } public int Age { get ; set ; } } 2. Static Classes Definition: A static class cannot be instantiated and can only contain static members (methods, properties, fields). It’s often used for utility functions. Example Scenario: A MathUtility cla

C# : 12.0 : Primary constructor

Introduction In C# 12.0, the introduction of the "Primary Constructor" simplifies the constructor declaration process. Before delving into this concept, let's revisit constructors. A constructor is a special method in a class with the same name as the class itself. It's possible to have multiple constructors through a technique called constructor overloading.  By default, if no constructors are explicitly defined, the C# compiler generates a default constructor for each class. Now, in C# 12.0, the term "Primary Constructor" refers to a more streamlined way of declaring constructors. This feature enhances the clarity and conciseness of constructor declarations in C# code. Lets see an simple example code, which will be known to everyone. public class Version { private int _value ; private string _name ; public Version ( int value , string name ) { _name = name ; _value = value ; } public string Ve