How to build and use a Layer for your AWS Lambdas


AWS Lambdas are brilliant! They simplify deploying serverless applications. They allow us to really quickly build prototypes and automatically scale. One of the issues with having every function as a separate entity is that you need to include common code into every single Lambda.

If you have to do the same thing the same way three times, its time to automate it — Automation Rule of Three

Layers

Lambda Layers have been created to solve this repeated code issue. The way they work is that you deploy your common code into a layer. This can be your common code or NPM packages that you always use. When you connect this layer to one of your Lambdas, you can access all the common code from inside your Lambda.

This means that you don’t have to copy the same file into every Lambda folder or create your own ‘common’ repo that you require.

Setting up a Layer

Since a layer is just a collection of code, we can start by creating a new folder for this layer. I like to have all my layers in a folder next to my Lambdas folder. We can create a new layer folder called DemoLayer which needs to have a folder for the runtime that it is going to use. For this example, we are going to use nodejs, so we create that folder.

mkdir -p lambdaLayers/DemoLayer/nodejs

Using our terminal, we can navigate to the DemoLayer folder and initialize NPM.

cd lambdaLayers/DemoLayer/nodejs
    npm init

Accept all the default values in the NPM setup, and you’ll have the package.json file generated in your folder.

For our first layer, we are going to import the moment library. This is the same process as you would use to add any NPM package to the layer.

npm install --save moment

Deploying our Layer

Now that we have the common code in our folder we need to deploy it. To do this, we need to zip up the whole folder and then upload it as a Lambda Layer. To zip up the folder content you can navigate into the DemoLayer folder and run a zip command on the content of the folder.

cd ../
    zip -r demoLayer.zip ./*

You should now see a demoLayer.zip file inside your folder. We can now go the AWS Console create our layer.

In the AWS console, navigate to AWS Lambda, and on the left-hand side, we should have options including Layers.

Inside the layers menu, we have the option to create a new layer. Clicking this opens up the setup options where we can give the layer a name, a description, upload the zip file we just created and select the runtimes.

Testing the Layer

With the layer created we can test that it all works. To do this, we can create a new Lambda called DemoWithLayer that runs on node 8.10. Inside this Lambda we can add this code:

const moment = require('moment');
    
    exports.handler = async (event) => {
        let momentNow = moment.now();
        
        const response = {
            statusCode: 200,
            body: JSON.stringify({momentNow}),
        };
        return response;
    };

We can test what happens when we run this without the layer by creating a test event. In the top right of the Lambda console, click select a test event and then configure test events. This opens a config window where we create the JSON blob that is sent to the handler. As we don’t use the event object, we can pass the default values, give this test a name and create it.

We can now click the Test button to run our Lambda. Doing this results in this message:

This is because our Lambda doesn’t have the moment module installed. We can now add our new layer to the Lambda and rerun the test.

To add a layer, click the Layers button underneath the DemoWithLayer button. Scroll to the bottom of the page to the Referenced layers section and click the add a layer button. In the popup, we can select our DemoLayer from the dropdown and selecting the highest version.

Add this to the Lambda and make sure to save the Lambda changes. When we rerun the test, we get a success response. You can use this process to remove a lot of the common packages from your Lambdas.


If you’ve learned something, then make sure to bash that clap button and follow me for more serverless tips and tricks.

Sam Williams

Sam is a Serverless Obsessive who runs Complete Coding, helping developers learn Serverless and companies make the most of the competitive advantage that Serverless gives them. Previous projects include: - Designing a chat platform that currently resolves over 250,000 customer inquiries a month for international retailers and local government - Architecting a backend system to support a 3D clothing visualisation tool - Building a solution to manage millions of dollars of communication software - Designing a "Video Editing in the Cloud" platform, enabling everyone from movie studios to indie film makers to harness the power of the Cloud - without needing to be cloud experts. - Launching a profitable Serverless Startup in under 30 days He has also been teaching cloud-based software development for 5 years and has taught Serverless development to thousands of people. The Complete Coding Youtube channel now has over 15,000 subscribers and over 1 million views

Recent Posts