Why You Should Use AWS Lambda Layers πŸ“¦

awedis - Aug 26 - - Dev Community

If you have worked with AWS Lambda functions, then this article will be beneficial for you.

From my experience, many developers face challenges with code size and dependencies, especially when working with AWS Lambdas. This article offers a straightforward solution that might help you. 🀘

Multiple reasons why you might consider using layers: 🎯

  • To reduce the size of your deployment packages.
  • To separate core function logic from dependencies.
  • To share dependencies across multiple functions.
  • To use the Lambda console code editor.

So let's dive deep. We are going to create two small applications, the first one without using layers, and the second example with layers. To keep the stack simple I will use AWS SAM (Serverless Application Model)

If you are new to SAM, here is a short description of it; it's an open-source framework for building serverless applications. It provides shorthand syntax to express functions, APIs, databases, and event source mappings.

Stacks

Image description

Example 1️⃣

template.yaml (SAM code, where it creates our AWS Lambda function)



AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: 'Lambda Function with SAM'

Resources:
  HelloWorldFunctionPython:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: 'app.handler'
      Runtime: 'python3.9'
      CodeUri: ./lambda-package.zip


Enter fullscreen mode Exit fullscreen mode

requirements.txt



numpy


Enter fullscreen mode Exit fullscreen mode

main.py (Simple Python code to just return a response)



import json
import random

def handler(event, context):
    random_number = random.randint(1, 10)

    return {
        'statusCode': 200,
        'body': json.dumps(f'Hello from Python! Random: {random_number}')
    }


Enter fullscreen mode Exit fullscreen mode

We need to place the dependencies and the code inside the folder called lambda-package, we run the following cmd to install the dependencies. And later we zip all the files inside that folder into lambda-package.zip file.



 pip install -r requirements.txt -t ./lambda-package --platform manylinux2014_x86_64 --no-deps


Enter fullscreen mode Exit fullscreen mode

β€œTo package and deploy, run the following commands:



sam package --template-file template.yaml --output-template-file packaged.yaml --s3-bucket lets-build-1


Enter fullscreen mode Exit fullscreen mode

s3-bucket -> The name of the Amazon S3 bucket where this command uploads your AWS CloudFormation template



sam deploy --template-file packaged.yaml --stack-name hello-world-lambda --capabilities CAPABILITY_IAM


Enter fullscreen mode Exit fullscreen mode

capabilities -> A list of capabilities that you must specify to allow AWS CloudFormation to create certain stacks.

However, we are unable to view our code inside the editor.
Image description

What if the dependencies get larger? How we can tackle this? So using the Layers, let's continue with example 2 where I will start using Lambda Layers.

Example 2️⃣

Here we go, the biggest change will be in our SAM code, so below I want to split the code into two parts, the first one where I create the layer and the second part where I create my Lambda function using the layers.



AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: 'Lambda Function with SAM'

Resources:
  NumPyLayer:
    Type: 'AWS::Serverless::LayerVersion'
    Properties:
      LayerName: 'NumPyLayer'
      Description: 'NumPy layer for Lambda functions'
      ContentUri:
        Bucket: 'lets-build-1'
        Key: 'python.zip'
      CompatibleRuntimes:
        - 'python3.9'

  HelloWorldFunctionPython:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: 'app.handler'
      Runtime: 'python3.9'
      CodeUri: ./app.py
      Layers:
        - !Ref NumPyLayer

Outputs:
  HelloWorldFunctionPythonArn:
    Description: 'Python Lambda Function ARN'
    Value: !GetAtt HelloWorldFunctionPython.Arn


Enter fullscreen mode Exit fullscreen mode

Now make sure to place the python dependencies in the right path:



python/
    └── lib/
        └── python3.9/
            └── site-packages/
                └── numpy/
                └── <other dependencies>


Enter fullscreen mode Exit fullscreen mode

Once you place them you can upload the python.zip to your S3 bucket.
Also important when you are installing the dependencies use the following cmd:



pip install -r requirements.txt -t ./python/lib/python3.9/site-packages/ --platform manylinux2014_x86_64 --no-deps


Enter fullscreen mode Exit fullscreen mode

Let's do a quick test for our lambda function using AWS cli:



aws lambda invoke --function-name hello-world-lambda-HelloWorldFunction-CKOzfRY55eaj output.txt


Enter fullscreen mode Exit fullscreen mode

You should see the output ('Hello, World!') inside the output.txt file. Additionally, because we are using Layers, we can edit our code directly from the console.

Conclusion:

AWS Lambda Layers are crucial, especially when dealing with numerous dependencies in your code. I hope this article was helpful. For more content, follow me through the links listed below. 😊


πŸ‘‰ Follow me for more πŸ‘Ύ
LinkedIn
X

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .