How can I build a Lambda layer for a function that includes the cryptography python package using a Windows machine?

0

Hello, I'm trying to write a Lambda function in python that requires the cryptography package. The documentation I've seen says that in order to use packages, I have to install them locally, then zip them up and upload them to Lambda as a layer. That works fine with other packages I've used, but cryptography is a compiled module so I can't use the Windows version in the Lambda Linux environment. Is there a way I can get the Windows package to work in Lambda, or otherwise is there a place I can install a version of cryptography that will work in Lambda?

  • Do you have or know how to use Docker on windows? Also how will you deploy your lambda package; Github Actions, CircleCI, something custom?

asked 3 months ago591 views
2 Answers
0

Hello.

The module called "cryptography" seems to be able to run in the environment described in the document below.
https://cryptography.io/en/latest/installation/

  • x86-64 RHEL 8.x
  • x86-64 CentOS 9 Stream
  • x86-64 Fedora (latest)
  • x86-64 macOS 13 Ventura and ARM64 macOS 14 Sonoma
  • x86-64 Ubuntu 20.04, 22.04, rolling
  • ARM64 Ubuntu 22.04
  • x86-64 Debian Buster (10.x), Bullseye (11.x), Bookworm (12.x), Trixie (13.x), and Sid (unstable)
  • x86-64 and ARM64 Alpine (latest)
  • 32-bit and 64-bit Python on 64-bit Windows Server 2022

So, I think it is possible to create a layer using a Docker container as shown below.
https://repost.aws/knowledge-center/lambda-layer-simulated-docker

As I answered at the URL below, if you can use Docker, you can also use Amazon Linux2 container images.
https://repost.aws/questions/QUmvP42wYQRiyOJmvDI5MVtA/error-while-running-python-code-in-aws-lambda-function#ANQ8ctoPV9Q72nMUr5mh8ulg

Recently, it has become possible to use Docker with CloudShell, so it may be a good idea to create a Lambda layer with CloudShell.
https://docs.aws.amazon.com/cloudshell/latest/userguide/working-with-aws-cloudshell.html#working-with-docker

profile picture
EXPERT
answered 3 months ago
0

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"
answered 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions