Working with Lambda Layers

Length: 00:06:07

Lesson Summary:

Lambda layers allow functions to easily share code. Upload a layer once, and reference it from any function.

Layers can contain anything (dependencies, data, configuration files, etc.) and can be shared publicly or with specific AWS accounts.

Collect common components in a ZIP file and upload as a Lambda layer.

A function may reference up to 5 layers.

Working with Lambda Layers

  1. Create a new virtual environment using Pipenv and install the required libraries:

     pipenv --python 3.7
     pipenv shell
     pipenv install requests
    

    Note: If you are on macOS, you can install Pipenv using Homebrew:

     brew install pipenv
    

    On Amazon Linux, or another environment, you can install using pip:

     pip3 install pipenv --user
    
  2. Create the ZIP deployment package:

     PY_DIR='build/python/lib/python3.7/site-packages'
     # Create temporary build directory
     mkdir -p $PY_DIR
     # Generate requirements file
     pipenv lock -r > requirements.txt
     # Install packages into the target directory
     pip install -r requirements.txt --no-deps -t $PY_DIR
     cd build
     # Zip files
     zip -r ../requests_layer.zip .
     cd ..
     # Remove temporary directory
     rm -r build
    
  3. Create the Lambda layer.

     aws lambda publish-layer-version \
     --layer-name requests \
     --compatible-runtimes python3.7 \
     --zip-file fileb://requests_layer.zip
    

    Note the LayerArn in the output.


This lesson is only available to Linux Academy members.

Sign Up To View This Lesson
Or Log In

Looking For Team Training?

Learn More