Serverless Computing with AWS Lambda In C#

Search for a command to run...

No comments yet. Be the first to comment.
In this series, we delve into the captivating universe of cloud computing, utilizing AWS and C# programming language.
Introduction In a distributed system, services must communicate and exchange data to achieve the software objectives. For example, imagine an eCommerce store implemented with the microservices architecture; when a user places an order, the front-end ...
Deploying a .NET application to a cloud environment like DigitalOcean is a powerful way to ensure scalability, reliability, and ease of management. In this post, we’ll walk through deploying a real-world .NET application—a simple ASP.NET Core MVC app...

Building a gRPC Service in C# and ASP.NET Core – Part 2

Building a gRPC Service in C# and ASP.NET Core – Part 1

Software architecture is the backbone of any application, and a well-thought-out architecture can make the difference between a maintainable, scalable application and a fragile, difficult-to-modify one. Clean Architecture, popularized by Robert C. Ma...

In software development, as systems grow more complex, objects start interacting with each other in increasingly intricate ways. Over time, this can lead to a tightly coupled, hard-to-maintain system where modifying or extending a particular feature ...

Serverless computing allows us to write and run our code in the cloud without necessarily bothering about infrastructure-related concerns: hosting, scaling, etc.
AWS Lambda is an AWS service offering serverless computing possibilities. With AWS Lambda, developers can focus on writing code instead of managing infrastructure. In addition, Lambda supports C# as one of its programming languages, providing developers with a powerful and familiar toolset to build serverless applications on AWS.
In this article, we will be exploring working with AWS lambda in C# .NET.
This article is a sequel to Asynchronous Communication In Microservices Via C# and AWS SQS, in which we built our Order processing service. We would be building on the code that we established there.
Our architecture would look like this:

Firstly, some valuable terms in AWS Lambda:
AWS Lambda: The core service lets you run code without provisioning or managing servers. You upload your code, and AWS Lambda automatically scales and manages your infrastructure.
Function: A piece of code that you deploy to AWS Lambda. Various AWS services or events can trigger it.
Trigger: An event source that invokes a Lambda function. Common triggers include AWS services like S3, API Gateway, CloudWatch Events, and more.
Event: The input that triggers a Lambda function. Events can be of various types, such as an S3 object creation, an HTTP request, or a timer-based CloudWatch Event.
Runtime: The environment in which your Lambda function code runs. AWS Lambda supports various runtimes like Node.js, Python, Java, Go, and more.
Handler: The specific function within your Lambda code that is executed when the function is triggered. It's defined in the format filename.handler where filename is your code's filename and handler is the function name.
Let's get started writing some code!
Firstly, create an AWS account and sign in to it on your computer via the console/terminal.
Install the AWS lambda CLI extension for dotnet. Type in the below command to install it globally.
dotnet tool install --global Amazon.Lambda.Tools
Install Lambda templates for .NET. This will allow us to create various lambda projects from our IDE.
dotnet new --install "Amazon.Lambda.Templates"
Now, when you attempt to create a new project, you'll see some AWS lambda-related templates

Create a new Project with the Lambda

Implement the notifications service in the ProcessMessageAsync method.

Deploy our just-created lambda project to AWS. Navigate to the new project's directory, open the terminal, and enter the following command.
dotnet lambda deploy-function NotificationService
Create or select an appropriate IAM role for your Lambda

Go to the Lambda dashboard on the AWS console.

Confirm the presence of your just deployed Lambda

Now, let's configure the trigger for our deployed Lambda
Open your newly created Lambda on the dashboard and click "Add trigger."

Select SQS as the trigger

Add your SQS queue. In our case, this is the orders queue.

Save your changes. Your Lambda should look like this

And that's it! Now, let's test the whole flow.
To test the integration:
Publish some messages to our queue via the Orders.Api

Verify the messages were published in the SQS dashboard

Verify via the logs that our messages have been appropriately handled

The code for this article is available here.