Creating and Testing a Simple AWS Lambda Function in .NET
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:
- .NET SDK: Ensure you have the latest .NET SDK installed. You can download it from Microsoft’s official site.
- 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.
- AWS Account: You need an AWS account to deploy and test your Lambda function.
Step 1: Create a New .NET Lambda Project
- Open Visual Studio and select Create a new project.
- Search for AWS Lambda Project (.NET Core — C#) and select it.
- Name your project (e.g.,
LambdaTestTool
) and click Create. - 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.
- Open the
Function.cs
file. - 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 theInput
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:
- Run the Lambda Test Tool from the command line or Visual Studio.
- 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:
- Right-click your project in Visual Studio and select Publish to AWS Lambda.
- 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!