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
已提問 1 年前檢視次數 1530 次
2 個答案
1
已接受的答案

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
專家
已回答 1 年前
  • 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
已回答 1 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南