How to create a .NET AWS Lambda function properly

How to create a .NET AWS Lambda function properly

January 14, 2024
Get tips and best practices from Develeap’s experts in your inbox

Introduction:

For those of you who are working with AWS Lambda and have tried creating one before, you must know by now that not all Lambda functions are the same. If you wish to create a ‘Hello World’ function written in Python , you will probably write the Lambda code, make all the integrations needed, test the Lambda and show it to your friends. All these tasks were achieved together in 5 minutes, and I’m being generous.  

But, let me ask you this:  Have you ever tried deploying a zip file as a Lambda function, or more specifically, have you ever successfully deployed a .NET project on AWS Lambda? 

Well, if you are up for a challenge, you can try it on your own. But if you are a bit lazy, you can sit back, relax, and let me guide you through the process as I conclude the process in the following article.

How to deploy a .NET project on AWS Lambda?

.NET Basics:

.NET is a free, open-source, cross-platform framework for building modern apps and powerful cloud services. Its platform allows you to create a wide variety of applications starting with mobile/desktop applications or web-server applications, and you can even create cloud-native applications with containers and docker images. In our case, we can use this programming language to create AWS Lambda functions. 

.NET has many advantages: It is free and open source, fast and cross-platform, and of course, it is a very modern tool used by many developers.

When we download .NET SDK as it is shown on the Microsoft website, we get the dotnet command and can manipulate it for our use:

‘dotnet new {console/web/etc…}’ – create a new .NET project.

‘dotnet restore’ – restore dependencies specified in a .NET project.

‘dotnet build’ – build a .NET project.

‘dotnet run’ – build and run a .NET project output.

And the list goes on according to your needs. You can build a web server or any application you want with .NET programming language.

The problem with using the regular ‘dotnet’ command

When it comes to the AWS Lambda function, when trying to build and run the .NET project,  it uses some dependencies related to AWS and Lambda, and if the dependencies don’t exist in the project, it can result in errors and endless debugging. In order to make this process easier, AWS has come up with two efficient and great tools: AWS SAM and .NET Lambda Tool. Both can deploy your Lambda function to AWS, but each happens in a different way. Let’s explore them and understand the differences.

AWS SAM Tool

SAM, or Serverless Application Module, is a framework for building AWS serverless applications. You can define the application you wish to create and model the dependencies and structure in a few lines of code in YAML format. Then you can build, test, and deploy your application to AWS using the SAM CLI.

So how does it work? 

First of all, a SAM project contains files and folders with a specific structure. Let’s analyze the following example:

/sam_application/
samconfig.toml
template.yaml
test/
events/
src/
  lambda_function_1/
    lambda_function_1.csproj
    Function.cs
    aws-lambda-tools-defaults.json
    bin/
    obj/
  lambda_function_2/
    lambda_function_2.csproj
    Function.cs
    aws-lambda-tools-defaults.json
    bin/
    obj/
  1. The samconfig.toml file contains default parameters for the project in every step or command that triggers the project. For example, this file can contain the AWS S3 bucket where this project is going to be saved. You can also create a new configuration file and specify the flag –config-file in the command line.

example from a samconfig.toml file:

[default.deploy.parameters]

resolve_s3 = true

region = "eu-central-1"

In this example, for ‘sam deploy’ command resolve_s3 parameter will be true and the region will be Frankfurt (“eu-central-1”).

2. template.yaml is the structure of the project itself in AWS. There, you should mention all your Lambda functions, API gateways, and other resources you wish to create. This template will be later used by AWS CloudFormation (we’ll get there, don’t worry).

3. The other three folders are the test, events, and src. events folder that contains test events for local testing, tests folder to support unit testing and src folder for the Lambda functions source code, but I bet that you could figure it out on your own. Note that every folder in the src/ folder is a Lambda function in a .NET project form.

In order to perform actions with the AWS SAM tool, you should use the ‘sam’ CLI command. Then, you will be able to perform many actions such as building the project, testing the project, or even deploying it to AWS:

  • ‘sam build’ command will build the project locally.
  • ‘sam local start-api’ will start a local environment for any purpose you need, including testing purposes. You can also test the Lambda using ‘sam local start-lambda’ for a specific function, but ‘start-api’ will run and test AWS serverless functions locally as a HTTP API.
  • ‘sam deploy’ will deploy the project to AWS, but here’s the catch- this command will use your template file to create a AWS CloudFormation Stack and this stack will deploy your resources for you! While this feature can be helpful for some, especially those new to AWS and unfamiliar with Lambda and API Gateway, others can find it as a disadvantage. It forces you to use CloudFormation to maintain your resources. Maybe you prefer to manage it differently. If we take the Lambda function example, the CloudFormation stack will load the code into S3 bucket, and store it under a file with a new, long, not clear hash as a name. that way you can’t track versions of your Lambda functions source code and you can’t either understand the files names in S3 which can be problematic. In other words, sometimes AWS’s “over management” can be a nuisance.

.NET Lambda tool

.NET Lambda tool is another tool that can solve the problem described when using AWS SAM. You can use this extension following two simple steps:

  1. The first is to download the tool using the command ‘dotnet tool install -g Amazon.Lambda.Tools’. This command will install the correct plugins you need. When adding this plugin you get a dotnet integration with AWS Lambda- the ‘dotnet lambda’ command.
  2. The second step you need is creating a configuration file for the Lambda functions, an AWS-Lambda-tools-defaults.json file will hold basic information about the Lambda function, such as: runtime, handler, function name, region, etc’.

When you finish these two steps, you are ready to use this tool with the ‘dotnet lambda’ command! 

‘dotnet lambda deploy-function’ will deploy the Lambda function into AWS, and will create from scratch if it does not exist.

‘dotnet lambda package’ will pack the Lambda function into a compact zip file that you can now simply upload to anywhere you want. Even to AWS S3 using a simple AWS CLI command. ‘aws s3 cp lambda_name.zip s3://{S3_BUCKET}/‘.

You can find more information about the dotnet lambda tool and the AWS-Lambda-tools-defaults.json file here.

AWS Lambda Console

If you are using the AWS console in order to create your Lambda functions, you will also need to provide your source code location. You can choose to add it from your local computer or from a S3 bucket where your function’s zip file is located.  You will be asked to specify all the function configurations in the Lambda console, and then go to “Code Source”→“Upload from”→“.zip file”, and then upload the compressed file directly from your computer or upload the file from S3 bucket if you wish. This way will probably sound good for those of you that are using IaC tools such as Terraform to create your Lambda functions.

Conclusion

When creating a new AWS Lambda function that uses .NET as a source code, you may face many issues. In order to make the process easier, you have AWS SAM and dotnet lambda tool, that helps you manage the workflow. You can integrate these tools with your CICD pipeline or use them locally from your computer. These tools will work on almost every platform you’ll choose and have a wide documentation that covers almost every edge case you may face.