Sean B. Durkin
Sean B. Durkin

Reputation: 12729

Deploy AWS Lambda package with Python 3.6 and Cryptography module

I am trying to deploy an AWS Lambda package. The language is Python 3.6, and it includes external modules (flask-ask and thereby indirectly cryptography). When I do so, and test the function, AWS reports error:

No module named 'cryptography.hazmat.bindings._openssl'

My development environment is Windows 10. My function works perfectly well on my development environment. I package the function up with Powershell script ...

& $pip install flask-ask -t $projectDir

... and then zip up the result and the function to make the zip package. My development version of Python 3.6 has 64-bit bitness.

Questions

Why does the function work locally, but not when packaged and sent to AWS? What is missing? and how do I fix it.

Similar questions

I've found other people had similar questions here:

I am loathe to switch to Python 2.7 . The second one only has solutions for a linux development environment.

Upvotes: 4

Views: 2785

Answers (1)

bimsapi
bimsapi

Reputation: 5065

Late answer, but a key thing to understand in AWS Lambda development is that the run-time environment, independent of language, is a stripped down Amazon Linux container. Python code with native dependencies like cryptography makes use of OS- and architecture-specific binaries.

When you run pip install cryptography, it's working in the context of your local machine, which means that any dependent binaries will be installed according to the OS flavor and CPU architecture. You can't package those up and run them in the AWS Lambda environment.

The solution is to either use tools that assemble your dependencies from pre-packaged distributions (Zappa does this), or perform a build step that installs/compiles dependencies in a compatible environment (AWS SAM CLI does this).

I personally prefer the AWS SAM CLI, but YMMV.

Upvotes: 4

Related Questions