Steps to write Amazon Lambda using C# (.NET8), debug and deploy using LocalStack, Visual Studio, Amazon Lambda Tools and Test Tools.

Useme Alehosaini
4 min readDec 7, 2024

This Article includes:

  • How to Install Amazon Lambda Templates.
  • How to Install the Amazon Lambda Tools.
  • How to Install the Amazon Lambda TestTool 8.0.
  • How to Debug Lambda using Visual Studio.
  • How to Deploy Amazon Lambda.

Prerequisite:
LocalStack
should be running on your local machine.
Installing LocalStack (To Simulate AWS) using Docker Desktop for Windows

Install Amazon Lambda Templates (if not yet installed)

Run the related dotnet command

dotnet new install Amazon.Lambda.Templates

If needed, list all templates and you should see the related templates there

dotnet new list

Create your first the Lambda Function using IDE

In my case Visual Studio 2022:

The aws-lambda-tools-defaults.json file is highly beneficial in the context of AWS Lambda development with .NET and Visual Studio, as it serves as a centralized configuration file for deployment settings.

{
"Information": [
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
"To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
"dotnet lambda help","All the command line options for the Lambda command can be specified in this file."
],
"profile": "localstack",
// The AWS profile to use for deployment. "localstack" indicates use of a local AWS environment simulator.

"region": "us-east-1",
// The AWS region where the Lambda function will be deployed (e.g., "us-east-1").

"configuration": "Release",
// The build configuration to use when packaging the Lambda function. Typically "Release" or "Debug".

"package-type": "image",
// The type of package used for the Lambda function. "image" indicates a container-based deployment.

"function-memory-size": 512,
// The amount of memory (in MB) allocated to the Lambda function. Here, it's 512 MB.

"function-timeout": 30,
// The timeout for the Lambda function in seconds. If the function runs longer than this, it will be terminated.

"image-command": "MyStringFunction::MyStringFunction.Function::FunctionHandler",
// The handler command used inside the container image to invoke the Lambda function.
// This is usually in the format: Namespace::Class::Method.

"docker-host-build-output-dir": "./bin/Release/lambda-publish"
// The directory path on the Docker host where the build output will be stored.
// This is the folder where the deployment package or container image will be located after the build.
}

The generated default function is a simple function that takes a string and returns both the upper and lower case version of the string.

//**********************************************************
// Function.cs
//**********************************************************

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 MyStringFunction
{
public class Function
{

/// <summary>
/// A simple function that takes a string and returns both the upper and lower case version of the string.
/// </summary>
/// <param name="input">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 Casing FunctionHandler(string input, ILambdaContext context)
{
return new Casing(input.ToLower(), input.ToUpper());
}
}

public record Casing(string Lower, string Upper);
}

Install the Amazon Lambda Tools (if not yet installed)

dotnet tool install -g Amazon.Lambda.Tools

Install Amazon Lambda TestTool (if not installed yet)

dotnet tool install -g Amazon.Lambda.TestTool-8.0

Now, you can debug the function using the Visual Studio:

Again, provide the input string and click on “Execute function”

And here you go, break point hit:

To deploy and test the function:

Access the project file location and deploy using:

cd C:\repos\AWS\MyStringFunction\MyStringFunction\src\MyStringFunction
dotnet build
dotnet lambda deploy-function

Note that no needed to mention the arguments as they are already in the aws-lambda-tools-defaults.json , otherwise the CLI dotnet command should be like:

dotnet lambda deploy-function --profile localstack --region us-east-1 --function-memory-size 512 --function-timeout 30

To run the test tool:

dotnet lambda-test-tool-8.0

In the browser use the input field and click the execute to see the result:

Done! 😊
💡 Please let me know your opinion in the comments, if any.

Thank you for your time 😊

--

--

Useme Alehosaini
Useme Alehosaini

Written by Useme Alehosaini

A Lifelong learner, passionate about self-improvement, soft skills, technology and finance. LinkedIn https://www.linkedin.com/in/useme-mba-msc/

No responses yet