Creating and Testing a Simple AWS Lambda Function in .NET

Osama HaiDer
3 min readAug 21, 2024

--

AWS Lambda is a powerful tool that allows you to run code without provisioning or managing servers. In this blog, we’ll create a simple Lambda function in .NET that converts a string to uppercase. We’ll also address common issues with input JSON deserialization when testing your function.

Prerequisites

Before we begin, make sure you have the following:

  1. .NET SDK: Ensure you have the latest .NET SDK installed. You can download it from Microsoft’s official site.
  2. AWS Toolkit for Visual Studio: This makes it easier to develop and deploy Lambda functions directly from Visual Studio. You can find it in the Visual Studio Marketplace.
  3. AWS Account: You need an AWS account to deploy and test your Lambda function.

Step 1: Create a New .NET Lambda Project

  1. Open Visual Studio and select Create a new project.
  2. Search for AWS Lambda Project (.NET Core — C#) and select it.
  3. Name your project (e.g., LambdaTestTool) and click Create.
  4. Choose Empty Function when prompted to select a blueprint, and then click Finish.

This will create a basic Lambda project with some boilerplate code.

Step 2: Write the Lambda Function

Let’s modify the existing function to convert a string to uppercase.

  1. Open the Function.cs file.
  2. Replace the existing code with the following:
using Amazon.Lambda.Core;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace LambdaTestTool;

public class Function
{
/// <summary>
/// A simple function that takes a string and does a ToUpper
/// </summary>
/// <param name="inputModel">The event for the Lambda function handler to process.</param>
/// <param name="context">The ILambdaContext that provides methods for logging and describing the Lambda environment.</param>
/// <returns></returns>
public string FunctionHandler(InputModel inputModel, ILambdaContext context)
{
return inputModel.Input.ToUpper();
}
}

public class InputModel
{
public string Input { get; set; }
}

3. Here’s what this code does:

  • FunctionHandler: This is the entry point of your Lambda function. It takes an InputModel object and converts the Input property to uppercase.
  • InputModel: This class is used to model the input JSON. It helps with deserializing the incoming JSON string into a C# object.

Step 3: Test the Lambda Function Locally

To test the function locally using the Amazon Lambda Test Tool:

  1. Run the Lambda Test Tool from the command line or Visual Studio.
  2. Provide the input JSON for testing. The input should look like this:
{
"Input": "test"
}

3. The output should return "TEST".

Step 4: Adding a Screenshot of the Test Tool Output

It’s always helpful to see the actual output of your Lambda function when testing locally. Below is a screenshot of the Amazon Lambda Test Tool showing the output for the input "test".

Step 5: Understanding JSON Deserialization

If you try to pass a plain string without wrapping it in an object, you might encounter a deserialization error. This happens because the Lambda function expects a structured JSON object, not just a raw string.

Here’s how the error might look:

System.Exception: Error deserializing the input JSON to type String

By using an InputModel class, we ensure the input JSON is correctly deserialized, avoiding this common pitfall.

Step 6: Deploying the Lambda Function

Once you’ve tested the function locally, you can deploy it to AWS:

  1. Right-click your project in Visual Studio and select Publish to AWS Lambda.
  2. Follow the prompts to deploy your function.

After deployment, you can test the function using the AWS Management Console.

Conclusion

By following these steps, you can create a simple AWS Lambda function in .NET that processes string inputs. We’ve also covered how to avoid common issues with input JSON deserialization by using a model class to structure your input.

Whether you’re new to Lambda or an experienced developer, handling input correctly is crucial for smooth function execution. Happy coding!

--

--

Osama HaiDer
Osama HaiDer

Written by Osama HaiDer

SSE at TEO International | .Net | Azure | AWS | Web APIs | C#

No responses yet