Skip to content

Instantly share code, notes, and snippets.

@codifyer
Last active September 11, 2023 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codifyer/1a04d3cb95b3eff084a6e7264a0b68f8 to your computer and use it in GitHub Desktop.
Save codifyer/1a04d3cb95b3eff084a6e7264a0b68f8 to your computer and use it in GitHub Desktop.
Create and publish python lambda layer using bash

Summary

This is a bash script I wrote to quickly create a python 3.7 layer on AWS.

The first part creates a zip file.

The trick to create a correct zip file for python lambda layers is to ensure the unzipped file has a directory structure like

python/lib/python3.7/site-packages/

and the actual package(s) is within site-packages folder. Otherwise lambda code gives an error like

{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'requests'",
  "errorType": "Runtime.ImportModuleError"
}

Unfortunately, AWS documentation isn't very explicit about this.

The second part publishes the layer on to AWS using aws cli.

Assumptions

  1. This was created on a mac running python 3.7 but should work on linux also.
  2. AWS cli is installed and properly configured.
#!/bin/bash
# prepare zip file for lambda layers
# if [ ! -n "$1" ]
# then
# echo "Usage: `basename $0` package_name"
# exit 1
# fi
# Inputs
package="package_name"
runtime="python3.7"
description="details about package like ver no"
# aws cli related inputs
AWS_REGION="us-east-1"
AWS_PROFILE="profilename"
cwd=$(pwd)
mkdir -p "$package/python/lib/$runtime/site-packages"
cd "$package/python/lib/$runtime/site-packages"
# install package to the site-packages directory
pip install "$package" -t .
cd $cwd/$package
zip -r "$package.zip" python
aws lambda publish-layer-version \
--layer-name $package \
--description $description \
--compatible-runtimes $runtime \
--zip-file fileb://$package.zip \
--region $AWS_REGION \
--profile $AWS_PROFILE
cd $cwd
@katariaashish
Copy link

Line 29 gives this error which can be fixed by adding mkdir python just before 29.
zip warning: name not matched: python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment