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
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:Now create a layer with the above using command:
You may want to add
--region <region>
if needed to above commandI was facing the same issue after some troubleshooting, I was able to fix the issue.
Create Zip file in below mentioned path:
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
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.
Created a Zip.
Uploaded the ZIP file to create layer and added layer to lambda
It worked as Charm:
Make Sure to add the layer to lambda
My Python version was 3.10 for local and Lambda’s python