How to Create a Simple .NET Core AWS Lambda Function

Skip the detail and show me the solution.

AWS Lambda is one of the hottest technologies in cloud software development today. And, as hot as it is, there is still a lot of confusion on just what Lambda is and how to develop a serverless function. 

First, let’s clarify a few things, serverless doesn’t mean that there isn’t a server. It just means that you don’t have to provision, maintain, upgrade (etc.) a server in order to develop and deploy your software.

Second, events – simply put, Lambda functions show their true power when responding to events. These events could be a web request from API Gateway, an event signaling an item has been added to an SQS queue or even a scheduled event from CloudWatch. Think of AWS Lambda as a wrapper for your (event) handler method and the event data that flows through Lambda to your handler method becomes the input for that handler method.

The Solution

At the moment, AWS supports the .NET Core 3.1 runtime. That doesn’t mean that you have to use .NET Core 3.1 to develop your Lambda function. However, we will use the .NET Core 3.1 SDK/runtime in this tutorial to avoid confusion.

Remember, for any example solution from AWS with .NET, we focus on the code that exemplifies the problem we are trying to solve. We don’t include logging, input validation, exception handling, etc., and we embed the configuration data within classes instead of using environment variables, configuration files, key/value stores and the like. These items should not be skipped for proper solutions.

Prerequisites

This post focuses on developing a Lambda Function using a tool like Visual Studio Code, Sublime, Brackets, etc. and does not cover developing an AWS Lambda Function with Visual Studio.

Warning: some AWS services may have fees associated with them.

Creating a Simple .NET based AWS Lambda Function

1. Specify the version of the .NET SDK to use:

Navigate to the folder/directory that you would like to develop your Lambda function in. Using the .NET CLI, run the following command, replacing with the corresponding value of a version of the .NET SDK that you have installed:

$ dotnet new globaljson --sdk-version <version>

For example, if you have the .NET SDK version of 6.1.204 installed, the command would look like this:

$ dotnet new globaljson --sdk-version 6.1.204

2. Create a .NET console application by running the following .NET CLI command:

$ dotnet new console --name simplelambda

3. Change the directory to the app directory of “simplelambda”. Include AWS Nuget packages by running the following .NET Core CLI commands:

$ dotnet add package Amazon.Lambda.Serialization.Json  
$ dotnet add package Amazon.Lambda.CloudwatchEvents  
$ dotnet add package Amazon.Lambda.Core  
$ dotnet add package AWSSDK.Lambda

4. Create the “Handler”

Here we will create a class named “Handler” and in that class, let’s create a method named, “Handle”. This Handle method will accept the event message that is passed to the Lambda function. In this case it will be a CloudWatch scheduled event. Here’s what the class looks like.

using System;  
using Amazon.Lambda.CloudWatchEvents.ScheduledEvents;  
using Amazon.Lambda.Core;  
  
namespace simplelambda{  
   
    public class Handler  
    {  
       [LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]  
        public void Handle(ScheduledEvent cloudWatchEvent)  
        {                          
            Console.WriteLine("Message: " + cloudWatchEvent.Source);              
        }  
    }  
}

5. Publish the .NET application with the following .NET CLI Command. The “-c” parameter is an alias for, “-configuration” and the “-o” parameter is an alias for output and is used to specify the directory/folder to output the compiled solution to. Here we are publishing for Release to the directory/folder of buildoutput.

$ dotnet publish -c Release -o buildoutput

Once this command finishes, navigate to the “buildoutput” folder\directory within your application folder and zip the files. This new zip file will be the file that you upload in the AWS Console when you create your Lambda function.

6. Upload the zip file to create your Lambda Function. Once this completes change the Handler to “simplelambda::simplelambda.Handler::Handle”. This points Lambda to the Handle method in the Handler class. Lambda will use this method to handle any requests from this Lambda function.

7. Now, let’s test this Lambda function through the console. Click the “Test” tab and in the template drop down, select “Amazon CloudWatch”. Once CloudWatch is selected, click the “Test” button. You should then see a message at the top of the page that reads, “Execution result: succeeded” with a “logs” link right next to it. Clicking this link will bring you to the CloudWatch Events console where you are able to view the event.

That’s it – The Simplest .NET Core Lambda Function!

Well, maybe not the simplest .NET Core Lambda Function, but pretty close…

Get all the source code at GitHub!