Lambda Layers .NET

Introduction

In this exercise we are going to see some techniques on how to create and use Lambda Layers with .NET Core.

What is Lambda Layers?

Lambda Layer is a feature in AWS Lambda that allows customers to create reusable Lambda components and use them in several Lambda functions without having to deploy the component each time in separate applications.

When to use Lambda Layers?

Some of the use cases for Lambda Layers could be (but not limited to) 1. Enterprise level reusable business logic components 2. Generic logging and exception handling components 3. Common database access components You get the idea. You can use Lambda layers for any NuGet package that you can reuse across applications. The only caveat is that the package has to be in NuGet package format.

Support for Lamba Layers in AWS Extensions for .NET CLI

1. Install the latest version of the CLI using this command

dotnet tool install -g Amazon.Lambda.Tools
  • If you had it installed already, update it with the following command so you can get the latest features like Lambda Layer support.

    dotnet tool update -g Amazon.Lambda.Tools
    

    2. How does Lambda Layers work?

    When you package a library as a Lambda Layer, the library gets uploaded to /opt folder in the target Linux environment. Depending on the runtime, libraries are placed in subfolders where runtimes can access. In the case of .NET Core, the libraries are placed under /opt/dotnetcore/store path.

As you might already wonder, how is this possible in .NET where the runtime expects to have all the referenced libraries be available during the application load itself rather than dynamically loading files as needed in other platforms such as Ruby, Python, NodeJS etc.

Starting .NET Core 2.0, Microsoft introduced a new concept called Runtime Package Store which basically allows us to package and deploy .NET Core applications against a known set of packages that exist in the target environment. When you do this, you no longer have to package components in your application if they are already available in the target environment. This reduces deployment time, better performance and also reduces disk footprint significantly. See this link for some additional details on this topic - https://docs.microsoft.com/en-us/dotnet/core/deploying/runtime-store

AWS Lambda uses leverages the Runtime Package Store functionality to achieve Lambda Layers functionality.

3. Sample Application using Lambda Layers

We are going to see how to create a sample Lambda Layer, publish to AWS Lambda and also use the Layer in another Lambda function

4. Use Case

Let’s assume you had several Lambda functions that were processing Orders for various business use cases and each of these functions were sending messages to an Amazon SQS queue to communicate to other services. While the business logic code will be different across these functions, the actual piece of code that is sending the message to the SQS queue would essentially be the same. In a world without Lambda Layers, you would create a generic library and reference that library in all the Lambda functions to remove redundancy. However, the library/package that you created will be deployed along with every single Lambda function that is processing the orders.

With Lambda Layers, you can avoid the redundant copy situation just by deploying the messaging component as a Lambda Layer and simply using it in all the Lambda functions that need the functionality.

Prerequisites

Visual Studio Environment Setup

5. Install Lambda extensions for dotnet CLI

This installation will allow you to execute dotnet lambda commands from the CLI - Open command prompt/PowerShell and run the following command

dotnet tool install -g Amazon.Lambda.Tools
  • If you already had the tool installed, run the following command to upgrade to the latest version

    dotnet tool update -g Amazon.Lambda.Tools
    
  • Once installed/updated, run the following command and ensure it appears in the result

    dotnet tool list -g