Building and Deploying AWS Lambda using .Net 6

Building and Deploying AWS Lambda using .Net 6

In this article, we will clearly understand how to build and deploy lambda functions using .NET 6. Before we dive deep into the implementation part let's understand the fundamentals

What is Lambda?

AWS Lambda is a computing service that lets you run code without provisioning or managing servers. You write the code for the task you want to perform into Lambda functions.

The Lambda service runs your function only when needed and scales automatically. You only pay for the computing time that you consume, there is no charge when your code is not running.

Lambda functions can be built using Node.js, Java, Ruby, C#, Go, Typescript, and Python. Today we will focus on Building with C#

Requirements

We need Visual Studio 2022 as currently AWS Lambda supports .NET6(C#) and VS 2019 don't support .Net 6 so make sure you are using the right versions else it won't work.

.net6

Reference - https://learn.microsoft.com/en-us/dotnet/core/tools/sdk-errors/netsdk1182

Now once you are done with the installation part let's begin with the implementation.

Open Visual Studio 2022 and create a new project, search for AWS Lambda, and select AWS Lambda Function Project (.Net Core - C#).

Then select Empty Function if your use case is different then you can select any of the others from the below options.

Once the New Project is created there are two files to look into

  1. Function.cs

  2. aws-lambda-tools-default.json

  • Function.cs

Snapshot of Function.cs file which has a sample code with the function name FunctionHandler that returns a string with a status message.

It is just calling an external API and checking its response. You can have your own implementation inside FunctionHandler()according to whatever task you want the lambda to perform.

We used LambdaLogger class from the Amazon.Lambda.Core the library which logs the START, END, and REPORT lines for each invocation.

  • aws-lambda-tools-default.json -This is a config file that has all the default values.

eg - We have "function-name": "FunctionHandler" but if you want to rename it then be sure to make changes in this JSON file as well. Do read the other key-value pairs mentioned it has information regarding the framework, aws profile, region, etc.

The field function-handler specifies the method that runs when the Lambda function runs. It is format


Once all the code changes are done just try to run the project on localhost. You should be able to see such output. Make sure you are signed in using your AWS account and have AWS Explorer in VS2022.

As we got the desired output let's move to the second part i.e. Deployment of Lambda Functions.

We can deploy the Lambda function in two ways

  1. Deploying from Visual Studio 2022 using Publish To AWS Lambda.

  2. Normal Publish, creating a .zip folder of publish folder and uploading from AWS Console.

Method 1

  • You will see the below screen on click of Publish to AWS Lambda. Fill in all the required details and click next.

  • Hurray now your Lambda is deployed and you are ready to test your Lambda Function, below is the output.

Method 2

  • Publish the code in the folder. Once it is published rename the folder with the namespace name i.e. TestLambda, note that the publish folder and the .deps and .dll files should be the root folder so on click of the TextLambda.zip the below structure should be visible.

  • Login to AWS Console to upload the TestLambda.zip

  • Go to the dashboard and search for Lambda.

  • Click the Create function button and fill in the details.

  • You should be able to see such dropdown of upload from, select zip, and upload TestLambda.zip here.

amazon web services - AWS Lambda upload by zip file not working - Stack  Overflow

  • Try testing the lambda function and see if you get the Execution result: succeeded as your output.

We have successfully built and deployed our initial lambda function with the utilization of .NET6. The primary objective behind writing this article was to save your efforts of doing research, as I personally encountered a lack of resources of .NET6. Most of the available videos and solutions were based on outdated versions like .Net Core 2.1/3.1, which are no longer recommended. I don't want you to struggle with implementation and troubleshooting runtime and environment issues. I hope you find it useful. Thank you :)

Resources referred

  1. https://docs.aws.amazon.com/lambda/latest/dg/csharp-package.html

  2. https://docs.aws.amazon.com/lambda/latest/dg/csharp-package-toolkit.html

  3. https://www.youtube.com/watch?v=IHIJFVUQyFY

  4. https://github.com/aws/aws-sam-cli/issues/4953