skip to Main Content

I have following python lambda function

lambda_function.py

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad,unpad
import base64

#CBC with Fix IV

data = 'random text to be encrypted decrypted'
key = 'xxx' #16 char for AES128

#FIX IV
iv =  'yyy'.encode('utf-8') #16 char for AES128

def encrypt(data,key,iv):
        data= pad(data.encode(),16)
        cipher = AES.new(key.encode('utf-8'),AES.MODE_CBC,iv)
        return base64.b64encode(cipher.encrypt(data))

def decrypt(enc,key,iv):
        enc = base64.b64decode(enc)
        cipher = AES.new(key.encode('utf-8'), AES.MODE_CBC, iv)
        return unpad(cipher.decrypt(enc),16)

encrypted = encrypt(data,key,iv)
print('encrypted CBC base64 : ',encrypted.decode("utf-8", "ignore"))

decrypted = decrypt(encrypted,key,iv)
print('decrypted data: ', decrypted.decode("utf-8", "ignore"))

def lambda_handler(event, context):
    encrypt(data, key, iv)

I created a lambda function using runtime python 3.12 and created a layer called custom_encrypt_decrypt and added this layer to the lambda as shown below,

enter image description here

When I try to execute the lambda, it throws error,

"Unable to import module ‘lambda_function’: No module named ‘Crypto’"


Firstly, I found that I have to use pycryptodome python library.

How I uploaded this library to lambda

  1. in my local computer I added the library,

    pip install pycryptodome

It added these two folders in my local

enter image description here

  1. I selected both the folders and created a .zip folder.
  2. uploaded zip folder to custom_encrypt_decrypt layer (and layer is already attached to lambda) – Note: pointing to right layer version.
  3. Executed lambda again and getting the same error again n again.

NOTE: If I run this python code locally, it works using same pycryptodome library!

2

Answers


  1. If the the zip file wasn’t packed with the correct structure, AWS won’t find your libraries. The structure has to be created as following:enter image description here

    You can check full desciption in link below:

    https://docs.aws.amazon.com/lambda/latest/dg/packaging-layers.html#packaging-layers-paths

    Login or Signup to reply.
  2. I was facing the same issue after some troubleshooting, I was able to fix the issue.

    • Check the python version in local machine and Lambda (It should be similar) as few packages are not imported properly when different version is there.

    Create Zip file in below mentioned path:

    • Create Virtual environment for python3.
    python3 -m venv venv
    source venv/bin/activate
    
    • Create a directory with name "python"
    mkdir python
    cd python
    
    • Install you packages in this folder.
    pip3 install pycryptodome -t .
    
    • Once installed Check the packages.
    • Create ZIP file for python directory
    cd .. 
    zip -r deployment_name.zip python
    
    • Now you can upload the .zip file to you AWS Lambda Layer.

    Make sure the ZIP file is created for all folder available in python folder Don’t miss out any files or folders it may be our main package may be dependent on it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search