Skip to main content

Building an AWS Serverless .NET Core Application: S3, DynamoDB, API Gateway, and Lambda—Step-by-Step Guide!

 

In today's fast-paced tech world, building applications that can handle scale while minimizing infrastructure management is a must. And guess what? With AWS serverless architecture, you can achieve just that. Whether you're building a new app or migrating from a traditional monolithic setup, AWS Serverless combined with .NET Core gives you a powerful, efficient, and scalable way to deploy your application.

In this guide, we’ll walk through building an AWS serverless application using S3, DynamoDB, API Gateway, and AWS Lambda—all in the language we know and love, C#. So, get your developer hat on, and let’s dive into the cloud!

Why Go Serverless with AWS?

Before we get started with code, let’s address why you’d even want to go serverless. Here’s what AWS Serverless architecture brings to the table:

  1. No Server Management: Say goodbye to worrying about server patches or capacity planning. AWS handles it all.
  2. Cost Efficiency: You only pay for what you use—when your Lambda functions are idle, you're not paying for idle time.
  3. Scalability: AWS automatically scales your app based on the load, ensuring that you’re prepared for anything, whether it’s 10 requests or 10 million.
  4. Flexibility with Services: AWS offers a wide range of services like S3, DynamoDB, and API Gateway, all seamlessly integrated for your serverless needs.

Now, let's jump into how we can put these benefits into action with a simple yet powerful C# .NET Core serverless application.

The Architecture We’re Going to Build

Here’s what we’re aiming for:

  1. API Gateway will act as the entry point to expose our RESTful API.
  2. AWS Lambda will execute the business logic (coded in C#).
  3. Amazon S3 will store our uploaded files.
  4. Amazon DynamoDB will act as the NoSQL database to store and retrieve information about the files.

In this scenario, we’ll create a simple API that allows users to upload files to S3 and save metadata in DynamoDB. Let’s get to the fun part—coding!

Step 1: Setting Up Your AWS Environment

1.1 Install the AWS Toolkit for Visual Studio

The AWS Toolkit makes it easy to develop, debug, and deploy .NET applications to AWS. You can install it directly from Visual Studio or through AWS CLI.

1.2 Create an AWS Account and Setup IAM Roles

Make sure you have an AWS account and set up IAM roles with permissions for:

  • S3: For file storage.
  • DynamoDB: For metadata management.
  • Lambda: To run your C# code.
  • API Gateway: To expose your REST endpoints.

Step 2: Create a .NET Core Lambda Function

Lambda is the heart of our serverless app, where all the business logic will reside. Let’s create a Lambda function using .NET Core.

2.1 Create a .NET Lambda Project

Open Visual Studio, and create a new AWS Lambda project:

  • Select AWS Serverless Application (.NET Core).
  • Choose ASP.NET Core Web API.

Your project will now contain a sample Lambda function, but we’re going to modify it to handle file uploads and metadata storage.

2.2 Write the Lambda Handler

Let’s create a handler that takes a file upload request and stores the file in S3 while saving metadata in DynamoDB.

public class Function
{
    private readonly IAmazonS3 _s3Client;
    private readonly IAmazonDynamoDB _dynamoDbClient;

    public Function() 
    {
        _s3Client = new AmazonS3Client();
        _dynamoDbClient = new AmazonDynamoDBClient();
    }

    public async Task<APIGatewayProxyResponse> UploadFile(APIGatewayProxyRequest request, ILambdaContext context)
    {
        try
        {
            var file = ExtractFileFromRequest(request); // Assume this method extracts file content from request
            var fileId = Guid.NewGuid().ToString();

            // Upload to S3
            var s3Key = $"uploads/{fileId}";
            await _s3Client.PutObjectAsync(new PutObjectRequest
            {
                BucketName = "my-upload-bucket",
                Key = s3Key,
                InputStream = file.InputStream,
                ContentType = file.ContentType
            });

            // Save metadata in DynamoDB
            var item = new Dictionary<string, AttributeValue>
            {
                { "FileId", new AttributeValue { S = fileId } },
                { "FileName", new AttributeValue { S = file.FileName } },
                { "ContentType", new AttributeValue { S = file.ContentType } },
                { "UploadedDate", new AttributeValue { S = DateTime.UtcNow.ToString("o") } }
            };

            await _dynamoDbClient.PutItemAsync("FileMetadata", item);

            return new APIGatewayProxyResponse
            {
                StatusCode = (int)HttpStatusCode.OK,
                Body = $"File {file.FileName} uploaded successfully."
            };
        }
        catch (Exception ex)
        {
            return new APIGatewayProxyResponse
            {
                StatusCode = (int)HttpStatusCode.InternalServerError,
                Body = $"Failed to upload file: {ex.Message}"
            };
        }
    }
}

This Lambda function:

  • Uploads the file to S3.
  • Saves the file metadata in DynamoDB.
  • Returns a success or failure message based on the outcome.

Step 3: API Gateway—Exposing the Lambda Function

3.1 Create API Gateway

API Gateway will act as the entry point to our application. Here’s how to set it up:

  • Create a new REST API in API Gateway.
  • Create a new POST method that triggers the Lambda function we just created.

Once done, you’ll get a public URL that users can hit to upload files through our API.

Step 4: Storing Files in S3

We’ve already integrated the S3 upload process in the Lambda function. Let’s just quickly highlight the importance of S3:

  • S3 (Simple Storage Service) allows you to store virtually unlimited files with high availability.
  • It integrates perfectly with Lambda, so you don’t have to worry about manual storage management.

The files uploaded to S3 can be accessed via unique URLs, making it easy to distribute or retrieve them later.

Step 5: DynamoDB for Metadata Management

While S3 handles file storage, we’ll use DynamoDB for storing metadata about each file (such as the file name, upload time, and file type). Here’s why DynamoDB is the best fit for this use case:

  • NoSQL, Scalable: DynamoDB can handle millions of requests per second, ensuring your metadata is stored and retrieved instantly.
  • Serverless: Just like Lambda and S3, DynamoDB is serverless, meaning AWS manages scaling and availability for you.

Step 6: Deployment—Publish the Serverless App

After setting up your code and AWS resources, it’s time to deploy:

  • Use the AWS Toolkit or AWS CLI to deploy your Lambda function.
  • Ensure your API Gateway is configured to invoke the Lambda function and is secured with proper IAM roles.

Conclusion: The Power of Serverless in .NET Core

There you have it—a fully functioning serverless C# .NET Core application leveraging the power of AWS Lambda, API Gateway, S3, and DynamoDB. With this architecture, you’re getting scalability, reliability, and cost-effectiveness, all without managing any infrastructure.

Serverless development is not just a trend; it’s the future. It allows you to focus more on delivering business value than on mundane infrastructure management. Now, it’s your turn to try it out and see how much faster and more efficient your development process becomes.

Next Steps: Build Your Own Serverless App Today!

Now that you've seen how to create a serverless app with .NET Core and AWS, it’s time to take action. Get started by setting up your AWS environment and coding your own serverless applications. And as always, let me know in the comments how you’re using serverless in your projects—I'd love to hear your success stories!

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

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