top of page

Using external libraries with AWS Lambda Layers

  • robin05156
  • 2023年7月25日
  • 読了時間: 3分

更新日:2023年7月26日


Lambda is a mechanism that allows common libraries to be called from Lambda function code. Separately packaged libraries can be commonly used by different Lambda functions. Lambda will load the Lambda layer along with the function when it is invoked.


Advantages of Lambda Layer

Reuse code across multiple functions

Separating common functionality into layers makes it easier to share the same library across multiple Lambda functions. This eliminates the problem of duplicating the same code in different places and having to update and debug them separately.

Traditionally, the solution to avoiding code duplication has been to implement common functionality as separate Lambda functions. In some cases, it may even be the ideal solution. But the problem with how it was implemented was that it required an extra call from function to function. In some cases, functions had to be executed in parallel, increasing the cost of executing the function and the debugging effort.

Layers let you share common libraries without making additional function calls.


Keep function package code small

Writing large amounts of code for each function can make your application more difficult to maintain and test. Deployment can also be slow.

To keep your application's package small, layers allow you to separate functionality that is commonly used by two or more functions. Instead of deploying the same code multiple times, layers are deployed only once.


Simplify management and deployment of main features

Keeping your function packages small reduces deployment time. When using large dependencies, the main function code may only be a few megabytes, but the entire package can be up to 50MB.

These large dependencies rarely change, so packaging them as a layer and deploying them only once avoids having to deploy them every time the main function changes.


How to implement Lambda Layer

Official information can be found on the following pages.

Based on this, I will briefly summarize what I have done.


Layer code is expanded under /opt, and some directories under /opt are already in PATH.

Expand the Python library to /opt/python to make it a layer. Then it will load at will.


Write code to layer and zip

Write the code to make it a layer as follows.


python/layer.py

def test():
    return 'Layer call successful!'

zip this code

$ zip layer.zip ./python

Layer creation, ZIP upload

From the Lambda console, select the layer from the menu and press the "Create layer button" on the right.

  • Name: I named the layer "sample-layer"

  • Description: Optional, so you don't have to write it

  • Zip File: Upload the ZIP file you created earlier

  • Compatible architecture: this time using "x86_64"

  • Compatible Runtimes: Runtimes to run. This time, select "Python3.10" and "Python3.9"

  • License option: Not required this time, so not entered



Press the [Create] button to complete layer creation


Add layer to Lambda function


Select Layer from the Lambda function you want to use the layer and press "Add Layer".


This time, we will add our own layer to the function, so select “Custom Layer” for the layer source and select the layer to add from the custom layers.

The selectable layers change depending on the runtime of the lambda function to which the layer is added.

If there is a version, select the version and press the "Add" button.


Invoke layer from Lambda function

The created layer can be called with Python's import.


sample-layer.py

import layer
def lambda_handler(event,context):
  print('Run Lambda!')
  return layer.test()

When I run sample-layer.py



Run Lambda!

Execution result:
Layer call successful!

I summarized the flow of trying Lambda Layer. I think it would be nice to be able to use Layers to avoid creating many similar functions.

 
 
 

© Copyright ROBIN planning LLC.

bottom of page