How To Create Lambda Layer (2 Min) | AWS | Package Python Libraries For Lambda Functions
ฝัง
- เผยแพร่เมื่อ 1 ส.ค. 2024
- In this tutorial, you'll learn how to create a lambda layer to package libraries and other dependencies that you can use with your AWS Lambda functions. Using layers reduces the size of uploaded deployment archives and makes it faster to deploy your code.
docs.aws.amazon.com/lambda/la...
-
Facebook: / gokcedbsql
-
Video Transcript:
-
Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn how to add a Python Lambda Layer in AWS. Let's start by navigating to the Lambda service then click on create function and give it a name.
Select Python for runtime then hit create function. In the code Source section, I'm going to copy and paste pre-written Python code that imports the request Library. On lines 5 and 6, I'm grabbing the HTML text from gokcedb.com and printing it out on the console.
Hit deploy then click on test and give your test event a name then hit save. Click on test again to execute your Lambda function and you should receive an error message saying unable to import module name requests. To fix this error, we're going to create a Lambda layer.
I'm going to copy and paste the virtual environment folder from an existing project which has the requests package installed already. Let's rename this folder to Python which is the requirement according to this AWS guide. Feel free to remove any unnecessary folders or libraries that you don't need in this layer.
Right-click on the Python folder and hit compress to create a Python.zip file. Let's go back to the Lambda console then click on layers in the left menu then hit create a layer. Give your layer a name then upload the Python.zip file that we just created.
Select comparable architectures and runtimes then hit create. Now go back to your Lambda function then click on layers in the function overview section. Click on add a layer button then choose custom layers.
Choose the custom layer that we just created then hit add. Click on the test button and this time your Lambda function should execute without any errors. As you can see the HTML was printed on the console.
There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next time.
Thanks so much! Spent forever trying to find out how to do this.Very helpful.
Glad I could help!
Thanks, this video solved my issue of import error after trying a lot many. Thanks bhai
Thanks a lot for this short and to the point video, subscribed!
Very useful! Thanks a lot!
Thanks, this really helped!!!
Glad it helped!
thank for the tutorial
bro thank you, spend two days, getting work with pandas, finally run it using layers
great!
How did you call the lambda layer from another lambda function?
@@shomiksingh5929 check this video: th-cam.com/video/nElDfGVgvy4/w-d-xo.html
Hey Gokce! Thank you very much for the tutorial. Do you know what are the options when yours layers consume more than the limit?
thank you
I have a Question. Once we make a webhook (in my case its webex). For any reason I happen to delete and re-create a new one, would that affect my message to be pushed to AWS Lambda? Should I be making any changes to code-base or aws service when I change/ delete webhook id
Updated tutorial can be found here: th-cam.com/video/nElDfGVgvy4/w-d-xo.html
Really useful, it's a pitty on Windows you need to rename the Lib folder and create a new folder inside it, to get it working.
A cool workaround I found was creating a requirements.txt then installing all the packages from that in the python directory:
pip freeze > requirements.txt
pip install -r requirements.txt -t python
@@shekelboi Hey what do you mean by this? I'm using windows and I'm trying to do the work around and I can't get it to work
@@mistromoves If you run those two commands it should install the packages in the appropriate (python) folder and from there you just need to zip it up and upload it. Let me know where you're stuck so I can help.
@Abi Asija What if the zip file is around 0.3Gb size? AWS returns error: Failed to create layer version: Unzipped size must be smaller than 262144000 bytes.
Any idea how to deal with it? Thank you
Maybe create multiple layers than don't exceed the limit or reduce the zip size.
Did u get any solution?
No one creates layers like this. Show how to do it from cmd line
Thanks for the video, it was very clear and precise, In the same way, I wanted to use the azure-identity package with my aws lambda function so for that purpose I created a directory and installed the azure-identity package using the pip command then converted it into a zip file and uploaded it to AWS lambda function along with function.py file. When I ran the script I got the import error stating that
Error Message: {
"errorMessage": "Unable to import module 'lambda_function': cannot import name 'x509' from 'cryptography.hazmat.bindings._rust' (unknown location)",
"errorType": "Runtime.ImportModuleError",
"requestId": "3e17800f-0330-40f5-af54-69d860fbfdc1",
"stackTrace": []
} which is the dependent package for the azure-identity package. I would be grateful if you could please help me on how to fix this issue.
See if this helps: stackoverflow.com/questions/75757846/aws-lambda-runtime-import-module-error-cannot-import-name-x509-from-crypto