AWS Lambda Deployment Package in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to pylons in today's video we are going to learn about building an AWS lambda deployment package wait but why do we need that what happens is when you are building a Python application you are going to use a lot of Python libraries however not all the Python libraries are present in the a diverse environment therefore in order to run your code you have to have those libraries as dependencies in your deployment package and then upload it as a zip file for example if your Python code uses the request library and if you go to the AWS lamda console and do import requests it is not going to work AWS lambda is going to throw your LF so in order to solve this problem we have to build something called as an AWS deployment package in this video we are going to learn about three important things related to building an AWS deployment package first the AWS configuration settings second building the deployment package itself and third how to schedule scroll jobs by using the AWS cloud watch events so let's get started once you have logged in to the AWS console go ahead to the lambda function console here we have to create a new function I am going to name my function find an inn request why because I want to experiment with the request library and I want my name of the function to be representative of what I am doing in the run time I am going to choose Python 3.6 however if you want to do it with python 3.7 be my guest you know don't shy away don't use Python 2.7 because python 2 is going to be deprecated very soon so let's go ahead and choose python 3.6 and now we have to talk about permissions now there are multiple ways in which you can set up permissions for continuing with AWS lambda now remember if you just want the most basic lambda permissions you can go ahead and choose this option the first one that way you will have the most basic lambda permission and you will have access to Amazon CloudWatch logs and you can run a simple script from AWS lambda however if you want your application to have access to an other resources like for example s3 events or access to AWS API gateway sage maker redshift stuff like that then you have to go ahead to the I am console and create a policy for yourself you have to attach all the relevant policies that will help you access all these applications in your lambda function and here you can go ahead and click on this option use an existing rule and in this drop-down you can choose the policy that you created however in this video I am NOT going to make it too complicated we just want to experiment with how to deal with libraries that are not present in the AWS environment so I am just going to use the basic lambda permissions let's go ahead and create our function so after waiting some time my lambda function PI Linden request has been successfully created now we can go ahead and experiment with our code however instead of writing code here I am going to upload a deployment package as a zip file let's go ahead and learn how to do it go to the code reader of your choice here you can see I'm using pycharm I have a directory called as eight of us deployment package and inside it I have a Python script called as testify now in the desktop I this is the code I have written the smallest piece of code easy to understand all I am doing here is I am inputting the request library then I have a function called as call piling in and I'm making a get call to my own website HTTP www.burlingtonkia.com quill two equal to 200 which means if the gate call is successful I am returning a string called it was successful okay now if I was to run this from here from pycharm on my local machine how would I do it I would to print call file in it right and if I run this it should work right it was successful however when we make it into a zip package we create a deployment package and we upload it to a SS lambda how is lambda going to know where to run the code where is the core where is the Python code that it should point to in order to do that we have to know something about something called as handler now if I go back to the AWS console here you can see something called as handler all right so basically in the handler the default value is set to lambda function not lambda handler which basically means by AWS lambda is going to look into a file called lambda function and then inside it it is going to look into a function called as lambda handler it's basically similar to how you call libraries you know import request dot get or import request dot status code in the similar fashion this is how a table is like lambda is going to access the code in your deployment package so here you can see we have a file called test dot pi and we have a function called as call Pilon therefore we have to replace this in our handler in AWS lambda so let's go ahead and do that so here instead of lambda function we have to set test and our function is called as compiled inin so I have to replace this as called pilot all right now let's go ahead and save this okay let's look at the next second important thing for the deployment package what happens is this is a simple piece of code that can run on your computer however in AWS lambda environment you need to pass in two parameters here one is event and the other is context these two parameters inside the function are really relevant because a SS lambda sends values through this sends arguments through this parameter we don't need to use them of course you should use them when you're writing code in a production environment they can be really useful however just for the sake of this tutorial we are going to set both of them to none so we are not the default value has been set to none and we are not going to use these parameters in our code now let's go ahead and create our deployment package go to any command line terminal I'm using item here on Mac and you can see I am already inside the folder where we have our test dot PI right so if I took LS you can see the test dot PI now what we have to do is we have to pip install the request library inside this folder see what happens we need to import request when you do input request Python looks for a request dot PI file in your path in the path where python is stored right like request is a library and then when you do people store requests it installs requested pi well all the files associated with the request library we should have something called as request or PI to the Python path but in AWS environment we don't have the request library so we have to manually add the request library to our deployment package in order to add request library to this folder go ahead tip 3 install request - P dot well not - t - t this basically shows that download the file to a particular folder for that you have to pass this argument - T and since I'm already inside the AWS deployment package and doing dot here which means that I want all the files to be installed in this folder now if I press this ok so it was successful and now when you come back to PyCharm you can see all the folders that are relevant for the request library have been installed to this folder so great the next step is creating the zip file so here you can see these are the folders ok let's go back to the folder destination again yeah here and now select everything inside this folder and then zip it I'm using compress here if you are using windows there must be something else so now we have created an archive dot zip file here you can see and we have to upload it to our a tub noise lamda environment so go ahead here choose upload a dot zip file then do upload and then choose this archive dot zip that we created just now alright ok let's go ahead and save it now if it is a smaller file less than 10 MB or something you can still see the code in line here you can see the core already these are the folders well that are required for the request library and now if I press test great so this says it was successful so that means now we are able to run our python code with libraries that EWS EMI environment does not support from AWS lambda now this was just a test example that we used we just wanted to see how we can use libraries in the AWS environment that are not already there and how we can use deployment packages to solve that issue however AWS lambda have a much higher purpose you can basically run your core any time in any schedule expression you want without having to manually trigger it it can trigger based on your core cron job expressions right so let's say you want to trigger the code every four hours so what you can do is you can have a clone expression and then e SS lambda is automatically going to manage that but how to add those prone expressions those you can add by the AWS cloud watch events so let's go ahead and set that up here if you come to the beginning of the console and here you can see what are the triggers that you can add so for example here we want to add the cloud watch events right and then you have to configure it let's go ahead to this page this basically tells us how to schedule expression for the Amazon Cloud watch basically by using Crone expression and rate expressions if you go through these fields if you are used to cron jobs there are usually five fields this grown in a turbulence lambda has six fields don't worry it's still very simple here you can see example Crone's and that says what if we want to run at ten a m-- UTC every day so this is the cron job that you would use runs every 10 minutes Monday through Friday this is the cron job you would use so then a lot of examples don't worry about it however in our particular example we want to say that we want to run our lambda function every four hours in that case instead of using acro we can also use something called as rate all right so basically you can see here that rate five minutes means it runs the lambda function every five minutes red one hour means it runs the lambda function every one hour in our case we have to say wait four hours right so let's go ahead and do that so here in the rules we are going to new rule let's say rule name Kron 4 hours you are not allowed any space in the rule MC it creates an error so remember that in the stable expression I can say read and then say for hours so basically now go ahead and press add now it says new trigger one unsafe changes so save it here great so now you have added this groan for hours and then it is automatically going to run your code every four hours so friends I hope this video was useful if you liked the video make sure to share it with your friends and subscribe to my channel for more such upcoming videos in the next video we are going to learn about a new feature that has been introduced by gator bluest layers thank you everyone and see you next time
Info
Channel: PyLenin
Views: 56,457
Rating: 4.9553218 out of 5
Keywords: aws lambda deployment package, aws lambda deployment package in python, aws lambda tutorials, aws cloudwatch, aws cloudwatch events, aws cloudwatch cron jobs, aws lambda example python, aws lambda tutorial python, aws
Id: rDbxCeTzw_k
Channel Id: undefined
Length: 12min 52sec (772 seconds)
Published: Sun Apr 07 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.