Skip to content

Instantly share code, notes, and snippets.

@katariaashish
Forked from codifyer/README.md
Last active September 11, 2023 05:14
Show Gist options
  • Save katariaashish/091d674ab10b6f891480250753e5957e to your computer and use it in GitHub Desktop.
Save katariaashish/091d674ab10b6f891480250753e5957e 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.10 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.10/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.10 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.10"
description="details about package like ver no"
# aws cli related inputs
AWS_REGION="ap-southeast-2"
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
mkdir python
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment