How AWS EC2, EKS, ECS, EFS, and More Come Together to Deploy Your .NET Core Application

From Code to Cloud🌥️: The Long Journey of Application

DotNet Full Stack Dev
7 min readOct 13, 2024

When deploying a .NET Core application in the cloud, you’re stepping into a vast world of AWS services that each serve a unique purpose. Think of these services like the pieces of a puzzle — EC2, EKS, ECS, EFS, and others are all essential components that work together to ensure your app runs smoothly in the cloud.

In this expanded blog, we’ll dive deeper into the various AWS services and show you how they fit into a real-life deployment scenario for your .NET Core Item API. Let’s turn your code into a live, cloud-native application, one piece at a time! 🚀

🔥 Don’t Miss Out! Explore More Exciting Bytes Here! 🔍https://linktr.ee/dotnetfullstackdev and dive into the content!

Piece 1: EC2 (Elastic Compute Cloud) — The Bricks and Mortar of Your Deployment 🏗️

What is EC2?

At its core, EC2 is the infrastructure layer — the actual servers (virtual machines) that host your application. Think of EC2 as the building blocks of your cloud deployment. Whether you’re running a small app or scaling out a massive architecture, EC2 provides the raw compute power.

Where Does EC2 Fit?

In a typical .NET Core application deployment, you might use EC2 instances to host the application itself. Imagine you have a monolithic .NET app — you could deploy it directly to EC2, similar to deploying to an on-premises server, but with more flexibility and scalability.

Here’s a simple example where we deploy a .NET Core app directly on an EC2 instance:

# SSH into your EC2 instance
ssh -i "your-key.pem" ec2-user@your-ec2-public-dns

# Install .NET Core on EC2
sudo yum update -y
sudo amazon-linux-extras enable dotnet6
sudo yum install -y dotnet-sdk-6.0

# Deploy your app
dotnet publish -c Release

Why EC2?

  • Full control over the instance (choose the OS, configurations, etc.).
  • Scalable: You can easily resize or add more instances.
  • Flexible: Good for running background services, .NET Core APIs, or full-fledged web apps.

Piece 2: EKS (Elastic Kubernetes Service) — The Microservice Maestro 🎻

What is EKS?

EKS is AWS’s managed Kubernetes service. It’s ideal for running containerized applications in the cloud with orchestration built in. If your .NET app is designed around microservices, EKS is your go-to choice.

Where Does EKS Fit?

Let’s say you’ve broken your Item API into microservices (e.g., Item Service, Pricing Service, Inventory Service). EKS will be the conductor that manages your Kubernetes cluster, ensuring that each microservice runs smoothly, scales automatically, and stays resilient to failures.

Here’s an example of deploying your .NET Core Item API as a microservice on EKS:

apiVersion: apps/v1
kind: Deployment
metadata:
name: item-api-deployment
spec:
replicas: 3
template:
metadata:
labels:
app: item-api
spec:
containers:
- name: item-api
image: your-ecr-repo-url/item-api:latest
ports:
- containerPort: 80

Why EKS?

  • Orchestration: Automatically manages and scales your containers.
  • Resilience: Self-healing capabilities with Kubernetes at the helm.
  • Microservices-friendly: Perfect for distributed .NET applications.

Piece 3: ECS (Elastic Container Service) — Container Management Made Simple 🛳️

What is ECS?

If EKS is the Kubernetes maestro, then ECS is AWS’s simpler, managed container service. It takes away the complexities of managing Kubernetes while still providing the benefits of containerized application deployment.

Where Does ECS Fit?

Let’s say your .NET app is a single containerized web API, or you have a small number of services. You can deploy your .NET Core app in ECS, and AWS will manage the containers for you.

{
"containerDefinitions": [
{
"name": "item-api",
"image": "your-ecr-repo-url/item-api:latest",
"memory": 512,
"cpu": 256,
"essential": true
}
],
"family": "item-api-task",
"networkMode": "awsvpc"
}

Why ECS?

  • Simple container management: Less complex than EKS.
  • No need to manage servers: Just define the tasks, and ECS handles the rest.
  • Cost-effective: For smaller workloads, ECS might be more cost-efficient.

Piece 4: EFS (Elastic File System) — The Storage Superhero 🗂️

What is EFS?

EFS is AWS’s shared file storage solution. It’s designed for scenarios where multiple EC2 instances or containers need access to the same persistent data.

Where Does EFS Fit?

Imagine your .NET Core Item API needs to save or access files (e.g., images, logs, or configuration data) that multiple services or instances need to share. EFS gives you a scalable, shared file system that can be mounted by all your EC2 instances or EKS/ECS containers.

Here’s an example of mounting an EFS volume in your .NET Core app:

# Mount EFS to an EC2 instance
sudo mkdir /mnt/efs
sudo mount -t efs fs-12345678:/ /mnt/efs

Why EFS?

  • Scalable file storage: Ideal for shared access across multiple services or instances.
  • Fully managed: AWS handles backups, scaling, and availability.
  • Persistent storage: Great for keeping data across container lifecycles.

Piece 5: S3 — Storing Objects in the Cloud 🌐

S3 is ideal for storing and retrieving large objects such as images, backups, and log files. Whether your .NET Core Item API needs to store media files or large datasets, S3 is the perfect fit.

using Amazon.S3;
using Amazon.S3.Transfer;

public class S3FileUploadService
{
private readonly IAmazonS3 _s3Client;
private const string bucketName = "your-s3-bucket";

public S3FileUploadService(IAmazonS3 s3Client)
{
_s3Client = s3Client;
}

public async Task UploadFileAsync(string filePath)
{
var transferUtility = new TransferUtility(_s3Client);
await transferUtility.UploadAsync(filePath, bucketName);
}
}

Piece 6: RDS — Managed Relational Databases 📊

Every application needs a database, and AWS offers RDS (Relational Database Service) to manage databases like SQL Server, PostgreSQL, and MySQL. For your .NET Core app, RDS can handle data storage while scaling and maintaining availability.

Where Does RDS Fit?

Your Item API will most likely need to store and retrieve structured data, like inventory information, pricing, or order details. By using RDS, you can offload the heavy lifting of database maintenance, backups, and scaling to AWS.

public class ItemRepository
{
private readonly string _connectionString;

public ItemRepository(string connectionString)
{
_connectionString = connectionString;
}

public async Task<List<Item>> GetItemsAsync()
{
using (var connection = new SqlConnection(_connectionString))
{
await connection.OpenAsync();
// Your SQL Query and Data Retrieval Logic
}
}
}

Piece 7: CloudFront — Global Content Delivery 🌍

Do you want your .NET Core application to serve static content (like images, CSS, and JavaScript) to users worldwide with blazing speed? That’s where CloudFront comes in.

Where Does CloudFront Fit?

If your Item API serves a front-end that requires rapid delivery of static assets to users, CloudFront acts as a content delivery network (CDN), caching content closer to your users, reducing latency, and improving performance globally.

Piece 8: IAM — Keeping Security Locked Tight 🔒

Security is a top priority for any cloud-native app, and IAM (Identity and Access Management) ensures that you control who has access to what. You can set fine-grained permissions for your .NET Core app’s components, ensuring that only the right services can interact with specific resources.

Where Does IAM Fit?

Your .NET Core app might need access to resources like S3, DynamoDB, or RDS, but you don’t want it to have unrestricted access. With IAM, you can create roles and policies that limit permissions to exactly what your app needs — nothing more, nothing less.

Piece 9: Lambda — Go Serverless with Lightweight Functions ⚡

Do you have small, event-driven tasks in your app that don’t need a full-blown server? AWS Lambda is perfect for executing lightweight functions without managing servers.

Where Does Lambda Fit?

Let’s say you want to trigger some .NET Core logic when a new item is added to your inventory or when a file is uploaded to S3. Lambda functions can be triggered by these events, allowing you to run small units of logic in response without provisioning servers.

public class LambdaFunction
{
public string FunctionHandler(string input)
{
return $"Processed input: {input}";
}
}

Piece 10: DynamoDB — The NoSQL Powerhouse 📈

For high-speed, scalable NoSQL databases, DynamoDB is your go-to. If your app needs to handle large amounts of unstructured or semi-structured data, DynamoDB fits perfectly.

Where Does DynamoDB Fit?

For example, your Item API might store product inventory in a structured RDS table, but you might want to log user activity or store session data in a NoSQL format. DynamoDB can handle this kind of data with high availability and low latency.

Final Piece: CloudWatch — Monitoring the Pulse of Your App 🖥️

Deploying an app is one thing, but making sure it runs smoothly is another. CloudWatch provides the monitoring and logging you need to ensure everything is functioning correctly.

Where Does CloudWatch Fit?

CloudWatch can monitor your .NET Core app’s performance, track API calls, and even alert you if something goes wrong. For example, you could set an alarm if CPU usage on your EC2 instance spikes or if your EKS containers restart unexpectedly.

// Pushing custom metrics to CloudWatch
var client = new AmazonCloudWatchClient();
var metricDatum = new MetricDatum
{
MetricName = "ItemAPILatency",
Unit = StandardUnit.Milliseconds,
Value = 123.45
};

var request = new PutMetricDataRequest
{
Namespace = "ItemAPI",
MetricData = new List<MetricDatum> { metricDatum }
};

await client.PutMetricDataAsync(request);

Wrapping It All Together: The Cloud-Native Puzzle 🧩

Deploying a .NET Core application in AWS is like solving a complex, exciting puzzle. Each piece — whether it’s EC2, EKS, ECS, EFS, or others — has its unique role to play in ensuring your app is secure, scalable, and high-performing.

By leveraging the right AWS services, you can build a flexible, cloud-native architecture that grows with your needs, while maintaining the stability and reliability that every modern app demands.

Coming Soon… 🚀

In future posts, we’ll dive deeper into the actual implementations of these services in a live .NET Core app. Stay tuned for hands-on guides and detailed code snippets that will help you deploy your application like a cloud pro!

--

--

DotNet Full Stack Dev
DotNet Full Stack Dev

Written by DotNet Full Stack Dev

Join me to master .NET Full Stack Development & boost your skills by 1% daily with insights, examples, and techniques! https://dotnet-fullstack-dev.blogspot.com

No responses yet