For any developer looking to build serverless and complex functions, AWS Lambda is the perfect service to achieve this. Lambda is a lightweight, extensible, easy-to-deploy service that simplifies running your code on the web. You can import libraries and dependencies to extend your function’s capabilities further. While you can zip these dependencies as part of the build and deployment process, it’s often easier and more practical to use Lambda layers instead. In this post, I explain how Lambda layers work and how you can easily build complex Lambda functions using layers.

Overview of Lambda layers

Under the hood, the Lambda service is divided into two planes: The control plane and the data plane. For this blog post, we’ll focus on the data plane responsible for the invocation API that executes the function on a worker instance. Workers are EC2 instances that execute code in isolated and secure sandboxes, which can be reused for multiple invocations of the same function. A Lambda layer is an archive containing additional code, such as libraries, dependencies, configurations, or custom runtimes. When you include a layer in a function, the contents are extracted to the /opt directory in the sandbox environment, which is available to your Lambda function code through environment calls. You can include up to five layers per function, which count towards the standard Lambda deployment size limits.

Why use Layers?
  1. A serverless application may have many functions that use a standard SDK version. Instead of bundling the SDK with each function deployment, you can create a layer containing the SDK. The effect of this is to reduce the size of the uploaded archive, which makes your deployments faster and more consistent across invocations.

  2. Latency-sensitive applications with a large code base may need to be faster to deploy and invoke. Using a layer helps to reduce the size of the uploaded deployment archive by preparing the sandbox environment with the layer already uploaded; this makes it faster to invoke your application code.

  3. Many code libraries include binaries that are operating-system-specific. When you build packages on your local development machine, the binaries for that operating system are used by default. These may not be the correct binaries for Lambda, which runs on Amazon Linux. If you are not using a compatible operating system, you must ensure you include Linux binaries in the layer.

  4. No additional cost. There is no additional cost for storing or using Lambda layers. However, the layer storage does take up space in your total Lambda storage in your region (currently capped at 75GB).

Deploying Lambda Functions and Layers:

You require a bucket to deploy the lambda layer if your zip file exceeds 10 MB.

# create a layer (for example, create the lib 

zip layer.zip index.js 


# create a bucket for the layer 

aws s3api create-bucket \ 

    --bucket Example-Bucket-ap-southeast-2 \ 

    --region ap-southeast-2 \ 

    --create-bucket-configuration LocationConstraint=ap-southeast-2 


# deploy the layer for use 

aws lambda publish-layer-version \ 

    --layer-name mylib \ 

    --description "Lambda Layer for Requests Module" \ 

    --license-info "MIT" \ 

    --content S3Bucket=Example-Bucket-ap-southeast-2,S3Key=layer.zip \ 

    --compatible-runtimes python3.8 python3.9 
    

Now that this layer is available for use with any function, you can add it to your function.

JoshCGP1.png JoshCGP2.png

Conclusion

Lambda layers provide a convenient and effective way to package code libraries for sharing with Lambda functions in your account or organisation. Using layers can help reduce the size of uploaded archives and make it faster to deploy your code.

Layers can contain packages using OS-specific binaries, providing a convenient way to distribute these to developers. While layers are private by default, you can share with other accounts or make a layer public. Layers are published as immutable versions, and deleting a layer does not affect deployed Lambda functions already using that layer. There are many prebuilt layers to extend your functions capabilities; you can find them at the GitHub repo links: Awesome layers, AWS samples.

To learn more about using Lambda layers, visit the documentation.

References