How to run Docker image with AWS Lambda Functions | Create Lambda function with container image.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello Tech Learners welcome back my Channel The Learning destinations I'm your host avinas and I hope you all are doing fantastic today so today once again I'm back with the very fantastic topic and under this sessions I'm going to cover that how you can create a Lambda function using container image now what makes me to conduct this topic guys there are two main reason first reason is that if you want to run your Docker image in serverless architecture then this is one of the best approach now the second reason is that if you create a Lambda function using the jip file or unj file there is a limit on file size so if your jip file is exceeding more than 50 mb and if your unj file is exceeding more than 250 MB then Lambda is not allowing you to create the function in such cases this is one of the best approach even if your Gip or unj file is exceeding more than the decided limit still you can create the Lambda function using the container image and how you can do it that is what I am going to show you in this tutorial now let's understand the problem statement by performing one simple practical example so here we have these codes okay now these codes have lot of dependencies okay so we have these dependencies so in order to successfully run this application into Lambda function we need to package all these dependencies okay so let me do it I R hyph app and then I need to pass the requirement. txt file and then I need to Define where exactly I want to uh install all the dependencies okay so for that I'm giving this folder name now I'm going to install all the packages so all the packages will be stored under DP uncore package so using that I will create a g file and if it is exceeding more more than 50 mb and then I try to upload into the Lambda functions it won't allow that is what I'm going to show you right now so it will take some time I'm going to pause this video and once this dependency installation is completed I will be back now installation has successfully completed and we have this folder where we have installed all the dependencies if we list down this folder okay so here we have all the dependencies now I'm going to make a g file from this unip file but before that let's check the file size of unip folder okay it is approx 389 MB according to the Lambda it is not allowable to upload and create the Lambda function because the size limit is only 250 MV okay so now we are going to uh create a jip file from this unip folder so for that I'm running the jip command and I give it name do Gip from the folder okay so now it will take some time okay so let get it complete this process and once it is completed I will be back all right so we have now J file ready let me show you okay let's check the file size of jip file it is approx triple 1 MB again it is exceeding what whatever the limit has been decided for the Lambda functions okay so now we are going to upload this file uh into S3 bucket and from there we try to create the Lambda function using this jip file so now I'm going to upload this Gip file into the S3 bucket let me check the size okay the size is triple 1 MB okay so as for the Lambda limitations if jip file is more than 50 mb it won't allow me to create the Lambda functions it won't allow me to upload this J file into my Lambda function so I'm going to upload this J file into this S3 bucket and for that I'm going to run uh let's say AWS S3 CP and then I need to give the file name which I want to upload S3 col double slash and then I need to pass the S3 bucket name okay it is uploaded and then we refresh and we received our file here okay so this is the file we copy the URL from here and same we pass into Lambda function so we come back into Lambda functions create function and I give it name test event it is a python application so I select Python and then I need to select the role I have already created one then create functions so Now function now it's time to upload our code and as for the Lambda limitations if Gip file is exceeding more than 50 mb then it won't allow okay so since my file is inside AWS S3 uh in S3 bucket I have placed it so now I'm going to give the pass the URL here and you see the error will appear okay so it is saying that unji size must be smaller than approx 260 MB right uh lower than 262 MP so if you are facing this kind of issues the best way to tackle this problem is use the docker image approach okay so now I'm going to deploy the same applications using as a Docker image into the Lambda functions all right guys so coming back to the code so we have this code here now I have already written the docker file because in order to convert this application into Docker image I need to write down the docker file so to save the time I have already prepared the docker file let me take you through this so firstly I'm taking this a specific Docker image guys here the point is to be noticed that you need to only use the official AWS Lambda Docker image if you want to pass simply like this let's say I'm passing simply this one right python 3.11 it won't work if you run this applications it will happen that your Lambda functions will will be unable to find your code okay we'll be unable to find the Lambda functions so it is recommended that whenever you are running Lambda functions as a container image you need to take only the official a uh official AWS Docker Lambda image okay there is a separate documentations let me show you AWS Lambda dock takes us directly there okay so this is the official Docker image that you the docker base image that you need to choose and here are different different versions that uh whatever the version is required let's say you want to test python uh 3.11 3.7 3.8 so there are various version and whatever the version you require it you can take it from there you can copy the URL from here okay so let me quck and reopen it again okay so this is the official image then after that I have uh uh list down all my dependencies under requirement. txt file and these are few dependencies that we need since I'm testing this applications for something else okay that's why I need to specifically give these uh dependencies okay but never mind you don't focus on it you just focus on it like all the dependencies I have written under the requirement. txt file and then here I'm copying everything at the root level of the Lambda functions and then on the command line I am passing this function name so this is my function name which I am passing it okay which my Lambda functions is calling the key point is that when you run your application as a container inside the Lambda functions you need to pass this uh Lambda functions okay so it should be the name of your applications okay uh let me tell you what it is exactly so my main application file name is this one and that is what I am passing here this is what I am passing here so it will call that Lambda Handler okay so now I'm going to create an image okay using this so I say Docker build then I give the tag name and I say ji I have previous version ready so I'm giving B9 dot okay so now it is building the docker image and then after that we need to push this Docker image into ECR Amazon ECR so let's get it complete all right so process is completed let me show the docker image so this is the docker image which I have just created okay jna and tag is v9 let me push it into ECR so this is my ECR and I need to First log in inside that okay so basically how will you do this okay so let me show you so when you create the ECR repository so I come into Amazon elastic container register I created the repository and after that when you go inside that there is the option view push command you need to come here and you need to copy this so and then after that you need to run this so you will get login inside your AWS ECR okay so now you can see the message login succeeded after that I need to tag it okay so I'll copy because the tag tag is already done the build the image build is already done now we need to tag it okay so my version is v9 my image name is Gen which is already given and but version is not latest it is v9 so I'm going to tag this one and now I'm going to to push this image okay so for that I will run Docker push command Okay so let's get it complete once it is completed I will be all right guys the process is completed successfully now we go to AWS ECR and this is the image we just now we post successfully we go here now we come back to the Lambda functions say create functions this time we select container image we give it name I Sayer Lambda and here we need to pass this container image URL okay so you will get it from here so this is this is our image v9 and this is the URL we copy and we paste it here and then we need to give the existing permissions so if you want to create a new role you can create it and if you have already the existing role like in my case I have already created one role so I'm selecting it okay and then after that create function that's it so in such a way you can see that I haven't received any error right and I uploaded more than 260 MB file okay that is the reason that if you face something this kind of situations where your file limit is exceeding okay so you go with this approach better you pack every you pack your application into Docker image and then you go create the lambra function using the container image this is one of the best approach okay now it's time if I run this applications uh test this applications I say event I know it is it won't run at this moment because my code is not yet ready it it it'll give me error because I'm still working on this I'm troubleshooting that I need to fix it okay so but this is one of the way so if I test right now see it is running but definitely I'll receive an error at this point because I know that the code is having some trouble and which I am working currently and actually when I fix this issue guys this this is going to be another very interesting top learn a rack system in serverless environment so that I will be releasing once I fix this entire code okay there is a time limit issues so I mean I know that there is a different issue that time limit can be fixed from here if I go by default it is a 3 second so you can just increase the time limit from here save it and then you can test it again so it is just still updating the functions okay now you can test it so this time I won't get time limit but I know this code is having some problem I'll get I will be getting the error so but anyway the point the key point is that this is the correct way when you have uh G file or unj file which is exceeding more than the limit defined then you can go with this app all right guys that's all from this sessions and I hope you thoroughly enjoyed this sessions apart from it there are many more sessions which are streaming in my YouTube channel AAS Kumar the learning destinations and recently I have covered a few various topics on uh generative VI so you go and watch it so if you like this entire video and entire sessions then don't forget to subscribe my channel and hit the like button as well if you have any questions you write me in the comment sections and if you feel any delay from my side you can directly reach to me on LinkedIn you can directly message me I'll respond to you there thank you so much for watching this I'll see you in my next tutorial till the time you have a very lovely day guys
Info
Channel: Avinashkumar - The Learning Destination
Views: 1,756
Rating: undefined out of 5
Keywords:
Id: XES3gXg13KE
Channel Id: undefined
Length: 14min 20sec (860 seconds)
Published: Wed Jan 24 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.