skip to Main Content

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:

  • mkdir lambda_layers
  • cd lambda_layers
  • mkdir tuya
  • cd tuya pip3 install
  • tuya-connector-python -t ./
  • cd .. zip -r tuya.zip tuya

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:

  • mkdir lambda_layers
  • cd lambda_layers
  • mkdir tuya
  • cd tuya
  • pip install requests -t ./
  • cd ..
  • zip -r python.zip python

Any insight would be much appreciated.

2

Answers


  1. For Lambda to pick the layer right, you will have to use the directory name as python. You can use any name while creating ZIP file. Steps to follow are below:

    mkdir python
    pip3 install -t python/ tuya-connector-python
    zip -r tuya.zip python/
    

    Now create a layer with the above using command:

    aws lambda publish-layer-version --layer-name tuya-layer --zip-file fileb://tuya.zip --compatible-runtimes python3.10
    

    You may want to add --region <region> if needed to above command

    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 tuya-connector-python -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.


    For Windows

    • Using power shell or Command Prompt perform following steps (admin privileges):
    mkdir layers
    cd layers
    mkdir python
    cd python
    pip3 install tuya-connector-python -t ./
    
    • Now open the python folder in file explorer.

    • Zip the python folder along with its child folders.

    • Now you are good to go and upload it as layer in AWS.

    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:

    • I have Created Similar Scenario in my environment:

    • I installed the Crypto library in my local machine(Ubuntu) as per above mentioned steps.
      Installation Image

    • Created a Zip.
      Zip File

    • Uploaded the ZIP file to create layer and added layer to lambda
      Lambda Function

    • It worked as Charm:
      Output

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

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