Part 2 - Create a Lambda Layer

1. Open the QueueHelper Solution from ..Layers/QueueHelper location

  • The project references QueueHelperLibrary NuGet package which may not resolve properly in your case. Go ahead and fix that reference and make sure it’s referencing the package that you created on your local machine.

2. Build and make sure there are no errors

3. Open PowerShell and navigate to the project file location and execute the following command after replacing the placeholder text with appropriate values that are relevant to you.

dotnet lambda publish-layer --region <AWS_REGION> --layer-name QueueHelperLayer --layer-type runtime-package-store -sb <S3_BUCKETNAME> -f netcoreapp2.1

4. To validate that the new layer has been created, you can go to AWS Lambda service home page on the AWS console and check if it exists. Also, navigate to the S3 bucket location where you will find a new folder with two new files inside namely,

- artifact.xml
- packages.zip

5. Download packages.zip file to your local machine and unzip it. The file contents should appear similar to the screenshot below

As you can see, the QueueHelperLibrary package along with the other dependencies have been packaged into a layer.

Now you might wonder, why did I have to create an entirely new Lambda solution just to throw it all away. It doesn’t actually have to be that way. You just need to create a .xml file with the list of NuGet packages that need to be created into a Layer. The format of the XML file should be as below.

<StoreArtifacts>
  <Package Id="<NUGET_PACKAGE>" Version="<VERSION>" />
</StoreArtifacts>

In our case, you could have a file that looks like the one below and push create the Lambda Layer by adding the additional -package-manifest parameter

<StoreArtifacts>
  <Package Id="QueueHelperLibrary" Version="1.2.0" />
</StoreArtifacts>
dotnet lambda publish-layer --region <AWS_REGION> --layer-name QueueHelperLayer --layer-type runtime-package-store -sb <S3_BUCKETNAME> -f netcoreapp2.1 --package-manifest <XML_FILENAME>

6. Create the Lambda Function

7. Open the OrderProcessor Solution using Visual Studio 2019

8. Ensure the NuGet Package QueuHelperLibrary is referenced from the local NuGet store you created earlier.

9. Open the aws-lambda-tools-defaults.json file and make the following changes

- Change the profile parameter to your AWS profile name on the local machine
- Change the function-layers parameter to the ARN of the Lambda Layer you just created
- Change the function-role parameter to the ARN of the IAM role you created earlier
- Change the s3-bucket parameter to the name of the S3 bucket you created earlier

10. Right click on the Lambda project and publish the Lambda function.

Navigate to the S3 bucket where the Lambda function is stored. When you download the package.zip file, you will see that there is just one library in the package. Other libraries will be obtained from the Lambda Layer that is configured to the Lambda function. Given that there is a cap of 75GB of storage limit per account for Lambda, using Layers will allow you to have increased number of functions deployed due to lower storage requirements.