DEV Community

starpebble
starpebble

Posted on

Simplify a Lambda dependency with a Lambda Layer

Faster code deployments are better. A Lambda layer can reduce the time to deploy a Lambda function. A Lambda layer is simply a support layer to store code. Here's how the layer speeds a deployment up, code in a Lambda layer may be excluded from the Lambda zip file. It's that easy. Smaller Lambda zip files are the pathway to Lambda rock stardom! 🤩

Quick Start

Let's example how to quickly create a Lambda for the Python 3.7 runtime engine.

  1. Create a requirements.txt file listing one or more Python package dependencies
  2. Download the dependencies to a local disk with pip install
  3. Archive the dependencies into a single zip file
  4. Create a single Lambda layer in the AWS Console, uploading the zip file artifact to the console
  5. Add the single Lambda layer in the AWS console to list of layers for the Lambda function

Tips

The Lambda community is simplifying local install of tools like 'pip' or 'aws cli'. It's a long time in the making. No longer must we install tools. Docker containers with tools are available. Here are two containers to consider for this quick start. The containers are optional. The containers simply save time. It's up to you.

LambCI

LambCI is on a mission to improve serverless computing integration. The docker container lambci/lambda:build-python3.7 has a perfect installation of pip. Run step two with pip from this container.

Here is some community code to do it:

Example:

#!/bin/bash
export LIB_DIR="python"
rm -rf ${LIB_DIR} && mkdir -p ${LIB_DIR}
docker run --rm -v $(pwd):/foo -w /foo lambci/lambda:build-python3.7 \
    pip install -r requirements.txt -t ${LIB_DIR}

AWS CLI

The aws cli v2 is in a docker container. No need to repeat the install instructions, please simply read them here at Using the official AWS CLI version 2 Docker image. The cli is faster than the console.
It's simply a preference.

Here's another community code example:

Example:

docker run --rm -it -v ~/.aws:/root/.aws -v $(pwd):/aws amazon/aws-cli s3 cp s3://aws-cli-docker-demo/hello .

Top comments (0)