Let’s deploy a simple AWS C# Lambda

Overview

This post is about creating a basic sample handler for an AWS C# Lambda. Here, I will try to do the simplest  way to deploy a lambda to AWS

Prerequisite

  1. Access to AWS Account with all the permission to create and update Lambda and Roles
  2. Dotnet core is installed – minimum required version 2.0
  3. Prior knowledge to AWS Lambda and dotnet core

Tools

To follow this series you need to install:

IDE

Or you can use your preferable IDE such as VSCode. In that case you may need

Steps

Create a C# project

You can create a class library by the following code executing on Command Line

dotnet new classlib -n HelloLambda -f netcoreapp2.1

It’s important you target netcoreapp2.1 as this is what’s supported on AWS C# Lambda’s right now

Codng: Simple Lambda Handler Function

Open it with your preferred IDE. Remove the Class1.cs and create a new class or edit the Class1. That’s how the project should look like on VSCode

using System;
namespace HelloLambdaNameSpace
{
    public class MyHandlerClass
    {
    }
}

Here You may notice that I am using suffix to visualise the components. Ok now we need to add the handler function. But before this I would like to do few changes on HelloLambda.csproj

And Change to
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Amazon.Lambda.Core" Version="1.0.0" />
    <PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.3.0" />
    <DotNetCliToolReference Include="Amazon.Lambda.Tools" Version="2.2.0" />
  </ItemGroup>
</Project>

I have added two packages “Amazon.Lambda.Core“, “Amazon.Lambda.Serialization.Json” and one dotnet tool “Amazon.Lambda.Tools

Packages will help to deserialize the request to a Request object and then serialize the Result object when it returns. The Amazon.Lamda.Tools will help you to create the deployment package comapatible with AWS Lambda.

Let’s add the following code (to MyHandlerClass). This is a pretty simple code. Adding two more classes for request and reponse. And also notice I have added LambdaSerializer  to serialize the HandleFunction

using System;
using Amazon.Lambda.Core;
using Amazon.Lambda.Serialization.Json;
using System.Threading.Tasks;

namespace HelloLambdaNamespace
{
  public class MyHandlerClass {
   [LambdaSerializer(typeof(JsonSerializer))]
   public async Task<Result> HandleFunction(Request request)
   {
    return new Result 
    {
      HelloWorld=request.Name
    };
   }
 }
 public class Request{
  public string Name { get; set; }
 }
 public class Result {
  public string HelloWorld { get; set; }
 }
}

now execute dotnet restore and dotnet build. If succeed then your code is ready to deploy

Create the package

Go to terminal and execute
dotnet lambda package -c Release -o ../HelloWorldLambda.zip -f netcoreapp2.1
It should end with Lambda project successfully packaged: <location>\HelloWorldLambda.CSharp\HelloWorldLambda.zip.
Troubledhooting

Ensure that restore has run and that you have included ‘netcoreapp2.1’ in the TargetFrameworks for your project…
Failed to create application package

Solution: Make sure .net code is installed to the right version and you have added ‘netcoreapp2.1’ to your project file

Version for package `Amazon.Lambda.Tools` could not be resolved.

dotnet tool Amazon.Lambda.Tools has not been added. If it is already added then restore or build was never performed to download this


Now that we have the zip lets upload to AWS Lambda. Before we go I want your attention to the details below

Zip File: HelloWorldLambda.zip 
Assembly: HelloLambda
Namespace: HelloLambdaNamespace
Class: MyHandlerClass
Function: HandleFunction

Create AWS Lambda

I use aws console to create a new aws lambda. After successful login, search for lambda services and go to create function. And I created the lambda. Right now on August 2018 the screen looks like below

create lambda

On the next screen, only two things to do. First upload the image and point the handler to your method

About the Handler:

Handler is split into 3 parts, separated by ::

ASSEMBLY :: NAMESPACE :: METHOD

So our Handler should be configured like: HelloLambda::HelloLambdaNamespace.MyHandlerClass::HandleFunction

And make sure you save your changes. Voila!! the function is ready to go

Test AWS Lambda

Select Test, you will end up with Configure Test Window. Give it a name and change key value pairs to

{
 "name": "Jobair Khan"
}

SAVE the test and exeute the test. You should see the log just after your test button

Next

That’s it for today. In this post I have tried to create the simplest AWS C# Lambda. If you like the article please let me know. I will write some more posts about Lambda triggers and how to connect to db in future.

References
  1. Building Lambda Function
  2. Creating a good old Hello World AWS C# Lambda
  3. AWS .NET Core CLI

1 thought on “Let’s deploy a simple AWS C# Lambda”

I would like to hear your thoughts