DEV Community

Chathra Serasinghe
Chathra Serasinghe

Posted on

Understanding AWS Lambda layers

What is a lambda layer?

It is an achieve type durable storage option for lambda which is typically use to store reusable code, such as libraries, dependencies, custom runtimes etc.

What are the storage options supported by Lambda functions

  • lambda layers (extracts into /opt)

  • /tmp

  • S3

  • EFS

What is the benefit of using Lambda layers?

  • It is one of the best practice of sharing same code libraries with multiple lambda functions e.g:-Include AWS SDK in a lambda layer and reuse it
  • Dependency can easily be updated

  • It reduce code duplication

  • Faster uploading your lambda code.

How to create a Lambda layer?

  • Typically you need to include the reusable code/binaries in a lambda layer so which can be used later by multiple lambda functions.
  • Once you decided which code/binaries to be included in the lambda layer, first step is to create a.zip file of it.

  • You need to adhere the correct directory structure based on your programming language(runtime language) of the lambda function
    Eg:- if the runtime language is Python, then you may follow directory structure python/<your code or binaries> when creating the zip file.
    Refer this table to understand Layer paths for each Lambda runtime configuration-layers-path

  • lets say you want to have boto3 and dependencies as a lambda layer. Then you can go to a terminal and run following.

pip3 install -t python boto3

Image
This command will download all the binaries to target directory python/.

Then you create the zip file

zip -r layer.zip .

After that you can create the lambda layer using zip file as follows.

aws lambda publish-layer-version --layer-name "AWSBoto3layer" --description "My boto3 layer" --zip-file fileb://layer.zip --compatible-runtimes python3.8

Image

You can see the new Lambda layer created in the AWS console and its version is version 1.

Image

If you try to update the same layer it will create a new version. Then then version will be version 2.

Image

Then you can use following cli command to attach your lambda layer to lambda function.

aws lambda update-function-configuration --function-name <lambda function name> --layers <arn of layer>:<version of the layer>

Note:- You have to make sure to link the correct version of the layer

Where does AWS lambda function extract layers in its excecution environment?

When setting up the function's execution environment, Lambda extracts the layer contents into the /opt directory.

Image

What are important facts you should know before using Lambda layers?

  • You can only add up to 5 layers to a lambda function

  • Layer should be in the same region of lambda function(But it can be in a different account)

  • Lambda layers are immutable.Therefore when a new version of the layer is published, you would need to deploy an update to the Lambda functions and explicitly reference the new version.

  • Maximum size of lambda layer is 50MB.It make sense because the purpose of this storage option is to store static code or binaries and for most of the cases it is sufficient.

  • Unzipped package size lambda function including lambda layers should be 250MB.This is a hard limit for lambda function. It means lambda layers doesn't solve completely solving the sizing problem.

  • A layer is private to your AWS account by default. But you can expose it other account explicitly by adding a statement to the layer version's permissions policy

References:

Top comments (0)