amazon-web-servicesaws-lambdaaws-lambda-layerstuya

AWS Lambda Layer import Tuya Connector


So all I have for my Lambda Function so far is:

import json
import os
import tuya_connector

And when I click "Test", I'm getting the following error: "Unable to import module 'lambda_function': No module named 'tuya_connector'",

I tried to resolve the error by creating a layer, but I believe I may be doing it incorrectly.

These are steps I took to create the layer:

I've tried going through the same steps above to see if "import requests" would work and it seems to work fine for that. Here are the exact steps I took to create a layer for that:

These are steps I took to create the layer:

Any insight would be much appreciated.

Edit 3/29/2024 Followed Zoro's instruction on Window and got this error instead: { "errorMessage": "Cannot load native module 'Crypto.Cipher._raw_ecb': Not found '_raw_ecb.cpython-312-x86_64-linux-gnu.so', Not found '_raw_ecb.abi3.so', Not found '_raw_ecb.so'", "errorType": "OSError", "requestId": "", "stackTrace": [ " File "/var/lang/lib/python3.12/importlib/init.py", line 90, in import_module\n return _bootstrap._gcd_import(name[level:], package, level)\n", " File "", line 1387, in _gcd_import\n", " File "", line 1360, in _find_and_load\n", " File "", line 1331, in _find_and_load_unlocked\n", " File "", line 935, in _load_unlocked\n", " File "", line 994, in exec_module\n", " File "", line 488, in _call_with_frames_removed\n", " File "/var/task/lambda_function.py", line 4, in \n import tuya_connector\n", " File "/opt/python/tuya_connector/init.py", line 5, in \n from .openpulsar import TuyaOpenPulsar\n", " File "/opt/python/tuya_connector/openpulsar.py", line 19, in \n from Crypto.Cipher import AES\n", " File "/opt/python/Crypto/Cipher/init.py", line 27, in \n from Crypto.Cipher._mode_ecb import _create_ecb_cipher\n", " File "/opt/python/Crypto/Cipher/_mode_ecb.py", line 35, in \n raw_ecb_lib = load_pycryptodome_raw_lib("Crypto.Cipher._raw_ecb", """\n", " File "/opt/python/Crypto/Util/_raw_api.py", line 315, in load_pycryptodome_raw_lib\n raise OSError("Cannot load native module '%s': %s" % (name, ", ".join(attempts)))\n" ] }

Ended up just downloading linux on my computer and going through the steps with no problem.


Solution

  • I was facing the same issue after some troubleshooting, I was able to fix the issue.

    Create Zip file in below mentioned path:

    python3 -m venv venv
    source venv/bin/activate
    
    mkdir python
    cd python
    
    pip3 install tuya-connector-python -t .
    
    cd .. 
    zip -r deployment_name.zip python
    

    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.


    For Windows

    mkdir layers
    cd layers
    mkdir python
    cd python
    pip3 install tuya-connector-python -t ./
    

    It will great if you have Linux Machine or Create an EC2 Instance with Ubuntu AMI and Copy the Content to S3, the you can upload it from S3 bucket to AWS lambda layer directly.

    Update:

    Make Sure to add the layer to lambda My Python version was 3.10 for local and Lambda's python