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
질문됨 일 년 전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
전문가
답변함 일 년 전
  • 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
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠