How to install a Python Dependency on AWS Lambda (2023)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what is up so today I'm going to show you how to create your own Lambda functions with dependencies so things not included with the default python so like maybe if you want to use pandas numpy how do you use that with Lambda you can see I've been struggling quite a bit with this you can see all of the different Lambda functions I made I'm kind of new to AWS myself so this has been something that I've struggled with a lot so I'm going to show you the easiest way I found to add in dependencies quickly into a python Lambda all right so first thing let's just create a function and we'll call our function I'm just going to call mine my example my example YT all right so I'll do click python 3.9 and just leave I'm going to leave the rest of this alone author from scratch all right so we have our new Lambda function created go to your desktop and just make a new new folder I'm going to call this Lambda testing so you can write your python code we can edit this later so don't worry too much about it um yeah so here we go I have this python file called example I'll explain it more in a little bit but the really important thing now this does matter so you need to go into this folder all right in a command prompt so if you're on Windows you could just click and type CMD and it'll open command prompt already in this path Mac you just use terminal so I'll just show you how to get there so you need to just all right so you just need to get into this path Lambda testing so since it's on my desktop CD desktop CD Lambda testing and we're inside of here and now we're going to pip install the package we need so I need open AI for this example maybe you need a numpy or um pandas or whatever so I'm going to write pip install open AI Dash t full stop and what this is going to do is it's going to install everything we need for this package into this folder all right so I've finished installing and you can see we have all of this stuff in the folder all right so next uh you need to compress or just turn this into a zip file so after you turned it into a zip file you just click upload from click zip file click upload and then click your zip file now when you upload this zip file it's going to overwrite everything in here so it's going to delete all of these other files and code and stuff in your Lambda function so just be aware of that so I'm going to click save all right so now that you uploaded your zip file in here you have the name of the Lambda so my example YT then you have the name of the zip file and then we have all of these packages now I had some issues with this so what I found I had to do is select all of these and then move them up into here so it's no longer in Lambda testing all right the next thing is let's work on our actual Lambda function open AI That's What I'm trying to import maybe you want to import pandas whatever you want to import now that you uploaded all the stuff you needed when you picked installed it you can use it so then you define your function here you can call it anything but you need to have event and uh contact in it all right so I finished making some changes to my code so now I can use openai I can do open AI dot API key and here's my API key I'm going to delete this once this video goes up so you can't use my API key then we have a engine I picked a prompt and then we use open AI dot completion.create so we can use open AI ask you a question what's the meaning of life and then we output that message so the next thing we need to do is you need to note the name of this python file so I call the example you might have something different and then um the name of the function here Lambda AI so we need to go down to the Handler section and change this so this first part Lambda function should be the name of your python file so it should be example and then after the dot you should have this Lambda a i all right so let's CL you click edit and we had it was example that was the name of our python file and the name of the function I believe was Lambda AI so we'll click save all right so next uh just click deploy to save your changes first time you click test you just give it event name so I'll just call my test and let's run it okay so we just had a few syntax errors and indentation errors and stuff like that so now everything's fixed uh that's probably why you shouldn't use a text editor um yeah anyway if I run it now you can see that we had the prompt what is the meaning of life and it actually answered that it said some people believe that the meaning of life is to find a higher purpose blah blah blah blah blah but the main point is we're now using a dependency in Lambda so this is something I really struggled with a lot and I'm pretty happy to finally be able to use dependencies because I mean whenever you write a python script you you need pretty much always need to use some sort of dependency it's pretty rare that you're able to run the script with just the default python built in things and I tried a lot of solutions online like I tried layers and it didn't work for me so this works one issue with this is that if you upload a lot of dependencies you won't be able to edit it here so I'll have to figure out another solution and for that I but anyway hope this was helpful
Info
Channel: Tech with Hitch
Views: 27,115
Rating: undefined out of 5
Keywords:
Id: iluJFDUh-ck
Channel Id: undefined
Length: 7min 44sec (464 seconds)
Published: Tue Jan 31 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.