Best Practices - Lambda Layers for Better Serverless Architecture

Gulmohar
Nggawe Nirman Tech Blog
4 min readSep 2, 2020

There’s no doubt that AWS Lambda and Function as a Service (FAAS) have been game changers in the microservice architecture landscape. Serverless deployments are a fraction of the cost of traditional servers or EC2 instances and have much less infrastructure overhead, leading to a quicker project ramp up time.

Utilizing serverless effectively allows you to focus on what matters most: robust, quality code and application logic. Serverless also works very well within any microservice architecture.

Layers are very useful if you have various Lambda functions using the same dependencies since the dependencies will be imported into the Lambda function at runtime. The main benefit of this is that you utilize your dependencies more efficiently, resulting in smaller deployment packages. And with smaller packages, you get faster deployments.

A powerful extension of AWS Lambda is Layers. In this post we’ll explore the following.

  1. What are Lambda Layers?
  2. What are they good for?
  3. Layers to the Rescue!

What are Lambda Layers?

A Lambda Layer works very similarly to a folder containing a library in a function code. The difference is that, instead of having to package this library within the function code, it can be packaged separately. Lambda will load the Layer together with the function when its invoked.

Lambda Layers are a way to share code and external dependencies between Lambdas within and across different AWS accounts.

Layers can be code, data or dependencies packaged separately for use within your lambda functions. Dependent layers are unpacked in the deployed lambda’s /opt directory for use. Layers are also versioned by default which allows you to make breaking changes in new layer versions and only reference the new version when ready.

Why Layers are Useful

Re-use code across multiple functions

The biggest strength of Layers is the ability to keep your project structure and repository leaner by extracting large, less frequently updated dependencies out into their own layer. Some project dependencies may require a separate, more complicated build process and can add complexity or slowness to your build. Those larger dependencies are an obvious candidate for their own layer.

Isolating common features in Layers makes it easier to share the same codebase across multiple functions. This avoids having to replicate the same code in different places, which is a bad practice.

Keep Lambda Package Small

Having a large codebase per function can be a problem. It can make it more difficult to maintain and test the application, for example. It can make deployment slower as well.

This is especially true for large dependencies, such as mathematical/statistical packages, video processing libraries, etc. Also when the same code features are replicated in multiple functions.

To keep packages smaller, Layers allow to isolate features that are commonly used by two or more functions. Instead of deploying the same code multiple times, the Layer is deployed only once. The Lambda platform will make sure it’s loaded with the functions that depend on it.

Simplifies management and deployment of the main function

By keeping function’s package smaller, deployment times are faster. When using large dependencies, the main function code may have only a couple megabytes, while the entire package ends up with up to 50 MB.

Since these large dependencies rarely change, packaging them as Layers and deploying only once will relief from having to deploy every time the main function changes.

Having Layers isolating features in single places also makes it easier to manage code. Whenever a feature needs to change, only one Layer has to be updated. Its consumers may also choose to upgrade when it best suits

Layers to the Rescue! Getting Started with Layers

AWS Lambda allows each function to have up to five Layers. There is no limit into how many times the same Layer can be reused across different functions.

Beware that the Lambda code package limits apply to Layers as well. The function must stay below 50 MB (250 MB uncompressed), which is counted including the size of its layers.

Creating a Lambda Layer

The Folder Structure for Layer is

To create the layer “MomentNodeModulesDependencies”, create a serverless.yml file and write the following code in it

To create the layer “UuidNodeModulesLayer”, create a serverless.yml file and write the following code in it

The basic project to test the layer is as below

Deploy the each one of it independently. Don’t forget to update the layers arn in basic project serverless.yml file.

--

--