Python bases AWS Lambda function with pysftp module not working

0

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?

Tamirm
demandé il y a un an1525 vues
2 réponses
1
Réponse acceptée

Hi,

Have a look at this: https://repost.aws/knowledge-center/lambda-python-package-compatible

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
EXPERT
répondu il y a un an
  • 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.

0

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.

Manu
répondu il y a un an

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions