# Serverless Computing with AWS Lambda In C#

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](https://devwithjosh.com/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:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694104590872/697b69a8-9891-4d70-920b-ea65a006f892.png align="center")

Firstly, some valuable terms in AWS Lambda:

1. **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.
    
2. **Function**: A piece of code that you deploy to AWS Lambda. Various AWS services or events can trigger it.
    
3. **Trigger**: An event source that invokes a Lambda function. Common triggers include AWS services like S3, API Gateway, CloudWatch Events, and more.
    
4. **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.
    
5. **Runtime**: The environment in which your Lambda function code runs. AWS Lambda supports various runtimes like Node.js, Python, Java, Go, and more.
    
6. **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!

1. Firstly, create an AWS account and sign in to it on your computer via the console/terminal.
    
2. Install the AWS lambda CLI extension for `dotnet`. Type in the below command to install it globally.
    
    ```bash
    dotnet tool install --global Amazon.Lambda.Tools
    ```
    
3. Install Lambda templates for `.NET`. This will allow us to create various lambda projects from our IDE.
    
    ```bash
     dotnet new --install "Amazon.Lambda.Templates"
    ```
    
    Now, when you attempt to create a new project, you'll see some AWS lambda-related templates
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694175556533/d10b3c15-5722-4326-973c-c93906f2a0e8.png align="center")
    
4. Create a new Project with the Lambda
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694176890912/ca034ec3-1b9d-4938-834e-203b919e1f79.png align="center")
    
5. Implement the notifications service in the `ProcessMessageAsync` method.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695228134057/c2f9ebd3-c913-41b9-9bc9-65645b591ce4.png align="center")
    
6. Deploy our just-created lambda project to AWS. Navigate to the new project's directory, open the terminal, and enter the following command.
    
    ```bash
     dotnet lambda deploy-function NotificationService
    ```
    
    Create or select an appropriate IAM role for your Lambda
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695224225369/2a6e66b4-3282-4ff1-b82a-018e7fa27613.png align="center")
    
7. Go to the Lambda dashboard on the AWS console.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695218269961/fa790e44-32a1-4a0a-af93-255a27e8fbd1.png align="center")
    
    Confirm the presence of your just deployed Lambda
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695218336161/113c5027-4524-46ce-bf19-c90c1ff0fdc6.png align="center")
    
8. Now, let's configure the trigger for our deployed Lambda
    
    Open your newly created Lambda on the dashboard and click "Add trigger."
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695224309321/c96f39e6-60a1-412b-a6bf-eaac6ae1ec73.png align="center")
    
    Select SQS as the trigger
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695224426680/c4c5b340-ee21-4649-9e41-06f290a4c4e4.png align="center")
    
    Add your SQS queue. In our case, this is the `orders` queue.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695224540626/403e470d-b29c-4ec9-b007-f8303ac33956.png align="center")
    
    Save your changes. Your Lambda should look like this
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695224915020/3e3038a4-75fa-4c84-b88e-33e501300406.png align="center")
    

And that's it! Now, let's test the whole flow.

---

To test the integration:

1. Publish some messages to our queue via the `Orders.Api`
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695225230822/be5ba147-31b3-440a-966e-d8f0cdd54303.png align="center")
    
2. Verify the messages were published in the `SQS` dashboard
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695225272586/8f75b3d9-bc50-4b8d-bebe-e70af10a4c42.png align="center")
    
3. Verify via the logs that our messages have been appropriately handled
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695226758481/6329e4e6-1b66-4075-bc85-085d72b280a0.png align="center")
    

The code for this article is available [here](https://github.com/JoshPhronesis/AwsLambda).
