Skip to content

Instantly share code, notes, and snippets.

@b01
Last active January 22, 2024 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b01/79ba76bcd5c301a22b11f512a2c6c827 to your computer and use it in GitHub Desktop.
Save b01/79ba76bcd5c301a22b11f512a2c6c827 to your computer and use it in GitHub Desktop.
Build Lambda Python Dependency ZIP Package with Containerization
# Directories
# -----------
/build-env/
/python/
# Files
# -----
dependencies.zip
#!/bin/sh
set -e
cd /var/task
# Les use Amazon linux for MAXIMUM Lambda runtime compatibility.
yum install -y python3 python3-devel openssl-devel cargo pip
# Uncomment and remove "cargo" above if by chance you need to install the
# latest rust when the package manager version is outdated for your needs.
#curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
python3 -m venv build-env
source build-env/bin/activate
# uncomment to install a pre-built binary
#pip install cryptography
# comment to prevent build from source
pip install -t ./python/lib/python3.6/site-packages cryptography --no-binary cryptography
zip -r dependencies.zip python > /dev/null
python3 -m pip freeze > requirements.txt

Question

https://repost.aws/questions/QUHITrviTHTsW3VpxkH9--NQ/how-can-i-build-a-lambda-layer-for-a-function-that-includes-the-cryptography-python-package-using-a-windows-machine

Answer:

Because Lambda does not supply a windows runtime you will not be able to run a package compiled for Windows on Lambda.

However, adding onto @Riku_Kobayashi's answer with an example.

You can try this on your local machine and then apply it to any of the automated build systems out there like GitHub Actions or CircleCI.

STEPS

  1. Install Docker Desktop or Rancher Desktop. Reboot if necessary.

  2. Review this script install-dependencies.sh and download it to where your python application is. Modify as needed. It will install the dependencies in the container, leaving a dependencies.zip in the directory where you run it.

  3. Build the ZIP by running the container in your project directory with the command:

    docker run -v "${PWD}:/var/task" --cpuset-cpus="0,1" --cpus=2 "amazonlinux:2023" /bin/sh -c "/var/task/install-dependencies.sh"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment