Python bases AWS Lambda function with pysftp module not working


I have a python based Lambda function that suppose to log into a SFTP server and downlaod a file. To do so I use a python module named pysftp. I uploaded the pysftp module (and its dependencies) I installed on my machine with the Lambda function source code file to the AWS Lambda directory, but when I tested the function I got the following error: "[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': cannot import name 'asn1' from 'cryptography.hazmat.bindings._rust' (unknown location) Traceback (most recent call last):"

I tried to search about the error and some people say the problem is having native libraries in the installed modules files that are installed differently on my windows OS than the AWS OS. The propsed solution was to use a Docker, but I am not familiar with it at all so I wanted to ask if it is in fact a problem involving native libraries and different OS's, and if so how do I implement the Docker solution or any other solution?

asked 5 months ago563 views
2 Answers
Accepted Answer


Have a look at this:

Essentially it mentions yo specify platform flag when installing dependencies, as certain libraries, if installed locally from windows, will conflict as they are expected with Linux binaries.

Hope it helps ;)

profile picture
answered 5 months ago
  • It worked, thank you very much. I had to install some of the dependencies for the AWS platform using pip and upload them with the rest of the modules and the source code.


AWS Lambda doesn't provide the cryptography library for Python so you have to package the library with your Lambda function, which means you have to package/compile it somewhere outside Lambda and then import it into Lambda. I recommend you package the libraries inside the Amazon Linux-based EC2 instance as the versions will be compatible. I have faced a similar issue and got it fixed by getting all the library files inside the Amazon Linux-based EC2 instance.

Using Docker is another good solution to ensure that you have the same environment on both your local machine and the Lambda environment. Essentially, you will create a Docker container that replicates the Lambda environment, including the necessary dependencies such as pysftp and cryptography. You can then use this container to run your Lambda function, which should resolve the dependency issues.

answered 5 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