Search code examples
pythonaws-lambdapycryptozappa

Solve Unable to import module 'lambda_function': cannot import name '_AES'


I am trying to deploy my lambda function that makes a requests and decrypts data using pycrypto's module Crypto.Cipher AES. This is how my import looks:

import boto3
from botocore.vendored import requests
import gzip
from io import StringIO, BytesIO
import base64
import sys
from datetime import datetime
import time
from Crypto.Cipher import AES

I run my code in my local Mac Environment and it works perfectly, but when I upload my package to AWS Lambda and test it, I get the following error:

Unable to import module 'lambda_function': cannot import name '_AES'

I checked this question and downloaded the pycrypto package from this git repo, made the build and copied the results to my lambda folder, packaged it and still didn't work.

I checked the result of the build and I noticed that within this folder:

pycrypto-2.6.1/build/lib.linux-x86_64-3.7/Crypto/Cipher

The AES files are generated with an extension related to my Mac OS

Mac Cipher files

I tried building the package on a Linux EC2 instance, but still get the same error (although the files change)

Linux Cipher Files

I tried a new path based on the following question but still failed with the exact same error.

"errorMessage": "Unable to import module 'lambda_function'"

Why is AWS Lambda not able to read the AES module in the pycrypto package? I have deployed Lambda function with other external libraries and never faced this issue.


Solution

  • This worked for me now using python2:

    https://www.github.com/Doerge/awslambda-pycrypto

    I just downloaded this project and zipped my lambda_function.py file with both the Crypto and pycrypto-2.6.1.dist-info folders.

    I see that the compiled .so files in Crypto/Cipher/ (like _AES.so) lack the python version and OS architecture and distribution that mine had (i.e: AES.cpython-27m-x86_64-linux-gnu.so). I will update my answer if I find a way to build the package properly by myself rather than using a third person compiled library.