Intro to AWS Lambda with Python | AWS Lambda Python Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome to another cloud path tutorial now I've made a couple of AWS lambda videos in the past and those tutorials have been all based around nodejs JavaScript so recently I've been learning a lot about Python and using it at my job so I thought it'd be a good idea to make a new video showing how to work with AWS lambda functions in Python using the Python AWS SDK and just demonstrating how to accomplish a few common tasks so if that sounds interesting please stick around and let's do this [Music] alright guys so I'm not going to go too far into AWS lambda history or background or anything like that since I've covered that in other videos so if you're interested in a more high-level overview of lambda please check out one of my other lambda JavaScript videos all right now the other thing is you may hear me make some references and comparisons to JavaScript since I'm primarily a JavaScript developer who's learning Python so if you hear me make some references to JavaScript that is why however this video is great for anybody who's just interested in learning how to use lambda with Python regardless of your background or history all right with that let's get right into it all right guys so the first thing we're gonna do is head on over to aws.amazon.com and let's go ahead and sign into the console alright so we're gonna head on over to the lambda section and we're gonna create our first lambda in Python so let's go ahead and select create a function alright make sure author from scratch is selected and let's go ahead and name this one Python hello world okay we'll just look at a simple example first and then we'll we'll do some more interesting things after that so let's change the runtime from nodejs to Python 3.7 which is the latest version of Python as of this moment for choose or create execution roll let's just leave the default selected create a new roll with basic lambda permissions all right which will basically give us permissions to read and write to cloud watch so let's go ahead and create function all right so here's our Python HelloWorld function and if we just scroll down a little bit to the function code section we'll see that we've got some boilerplate code here that's just gonna basically print out hello from lamda now just to go over a few things here let me make my screen a little bit bigger not that big oh whoa whoa there settle down okay I think that should be good let's just go over this function line by line in Python here so first of all we have import JSON which lets us work with JSON and do various things one of the great advantages of the Python language in general is that it comes with a great number of modules right out of the box with the Python standard library so at the end of the day you don't have you don't have the need to use as many different you know like NPM modules as you would and say no js' because there's a lot of functionality right out of the box all right down to line three we have our function definition so we use the keyword def and python to define a function so we have our function name we have our event in context which is a lambda specific parameters that you may have seen from my node.js tutorials or you know other work with lambda now the one difference here between Python and JavaScript is our function body starts with a colon okay and then uses indentation to specify the basically the function block all right whereas with JavaScript you'd see curly braces so that's one main difference here one syntactical difference between JavaScript and lambda now as far as the spacing it doesn't matter whether you use two or four spaces from my experience for is kind of standard with Python but as long as you're consistent within each function block then you're going to be okay all right and then lines five through eight is simply your return statement so we're gonna return a status code and body which is gonna print out hello from lambda to the console so this should look pretty familiar if you're used to JavaScript or basically any other you know C style language now let's go ahead and run this function and just verify that it's working so if we scroll back up at the top and let's go ahead and click on test all right we don't really need to do anything here we're not going to use any kind of events we just want to run this function so code and click on create oh sorry we need an event name so let's just say python python lamda test event alright whatever you want to name that should be fine go ahead and click on create and click on test okay so it's executed the function success let's just expand details and here we go so here is our response body as we would expect hello from lamda alright so that is our basic hello world Python lamda example and what we're going to do next is write a simple lambda function to read from our s3 buckets and output a list of all of the s3 buckets in our AWS account so we'll get a look at the boat oh three Python SDK library and how to use that alright so the first step here let's head on over to s3 and just you want to make sure that you've got a couple of two or three s3 buckets set up to use in this exercise so it looks like I've got five of them already but if you don't have any just go ahead and create bucket and it's a pretty simple process just enter a name and go through the motions here and then click on create okay I shouldn't take more than a minute or two to set up a few test buckets and doesn't matter what's in them or how they're configured figure it or anything like that because we're just going to be looping through basically and reading the names and outputting them okay so once you've done that head back on over to lambda and we're gonna create a new lambda function so let's click on create function again let's leave it author from scratch and we'll call this one list s3 buckets okay and make sure this is set to Python 3 7 and we'll leave our execution roll set to default and go ahead and click on create function alright so that should give us another basic hello world kind of boilerplate function that we'll modify from here ok so let's scroll down and a lot of times when I'm developing with lambda all kind of develop the function the lambda function outside you know in visual code or whatever code editor and then bring it here but since these examples are pretty simple I'm just gonna use the inline editor here alright so let's just go ahead and delete everything that's in here we're gonna start from scratch actually we're still going to need the int the import JSON so we'll put that back import JSON now the the Amazon Python SDK is called moto3 okay and that's available it's just available for you when you're in your lambda development environment so no need to install anything we can just do import Bodo 3 all right now those are all the imports we need so let's skip a line go down the line 4 here so let's go ahead and set up our service object that we're going to be interfacing with in this function so we can do boto 3 dot resource and then specify the resources the parameter so that'll be s3 so that'll basically give us an s3 service object that we can then call various methods on and work with within our lambda function all right so let's set up the lambda function itself so again we'll use the DEF keyword to define a function and let's say lambda handler which is just a convention that will pass an event and context ok and then let's do a colon and return to get to the new line and this looks like it's using 3 or 4 space and 4 space indentation for us by default so what we're going to do is basically set up an empty now I almost said array because in JavaScript this data structure would be an array but in Python it's called a list so let's call this bucket list and we'll define an empty list all right so keep in mind that with JavaScript you'd usually preface this with a some kind of variable declaration keyword like var Const or let in Python we don't need to do that we just write the variable name straight away and we can go ahead and assign a value which in this case it's going to be an empty list all right so what we're going to do is create a for loop and we're going to call the s3 service object but s3 buckets not all to return us a list of all the buckets alright so in Python we can create a for loop like this so we use a for in structure so we'll say for bucket in s3 dot buckets not all whoops okay and then for each inside the for body now again we'll use indentation so for spaces let's just print the bucket name name okay and then we'll add that name to the array okay we're just printing it here just kind of to see what our output is going to look like but the important thing is here to add it to the actual bucket list I said array the bucket list list okay you know I'm sure I'll still be in the Java Script mindset for a while till I trained my brain to think more in Python so we can use append to add an item to a list whereas with JavaScript we'd you know use dot push to push an item into an array so it's very similar in structure just the syntax and naming conventions are a little bit different here and there with Python but in a lot of ways it's very similar to JavaScript so it's you know not too hard difficult to pick up if you've got that background okay so that is it we're looping through our buckets and printing it printing each one out and also adding each bucket name to our bucket list list alright so now we just have to return that okay and we'll use some more a similar return statement as we had before in our hello world where will first return a status code of 200 so status 200 that should be status code comma and then we'll we'll return the bucket list in the body okay so body : bucket list alright and we are done offering our functions so now we can go ahead and test it let's click on save let's scroll back up to the top and just set up our test event here so this could be anything you know let's just say list list s3 test event all right let's go ahead and save that click create and let's go ahead and run it execution failed let's see what's going on here error in module lambda function invalid syntax all right got something wrong here you guys I'm sure probably saw it as I was writing it lambda handler doesn't it like here I don't know that looks okay to me I'm gonna go back up to the error here syntax error and module lambda function invalid syntax line 8 yeah I should have looked at that first line 8 what do we have here for bucket in s3 buckets oh okay I'm missing my colon here so again you guys probably noticed this as I was writing it but you know whereas with JavaScript we we'd use curly brackets for a for statement body and Python we've got to use a colon similarly to how we designate our our function body all right so sorry about that let me save that and let's try it again run test okay we've got it another error it looks like access denied okay so what's happening here is our lambda does not have permissions to talk to s3 basically so let us quickly go over to I am and give our lamb to execution role permissions so I want to go to the top menu and let's type in I am and let's see if we can find that role that we've just created for our lambda function I got to make my a little bit smaller here so I can see this lambda s3 role alright so this is the one that lambda has created for us implicitly so let's click on that and of course in a real-world scenario we may want to I don't know we may do this a little bit differently but I'm just gonna heavily hand it give it a you know card blank access to s3 so let's search for s3 and I'm gonna give it Amazon s3 full access alright again we may restrict the access a little bit more surgically in a production scenario but I'm just gonna go ahead and quickly do this for demo purposes alright so now our lambda should have full access to s3 let's go back and test that theory back over to lambda back to list us three buckets and let's run our test again failed again what is going on here all right access denied hmm that should have worked I don't know why all right let's check make sure that we're using the right execution roll service roll list as three buckets that looks correct let's go back over to I am and make sure that our change took rolls oh did I add it to the wrong one looks like I did that must be an old roll that I set up for another demo sorry about that guys but I'm just gonna basically have to do the same thing again s3 full axis attach policy all right so sorry about that little diversion there let's go back to lambda and try it third time's a charm I think don't you alright list us three buckets and test there we go that looks much better all right and there is our list of s3 buckets just as we would expect so yeah just remember that you've got to give your lambda correct permissions to access you know really any AWS resource that it's gonna be talking to so all right that is basically how to output a list of s3 buckets using a Python lambda and what we're gonna do next is do some work with dynamodb so we're gonna create a table and write basically write some lambda functions to read and write to that table all right so from the top menu let's go ahead and head on over to dynamo DB and if you don't see it right here immediately to the left you can just type it in this search bar so what we're going to do is set up a test table just so we have some data to work with for our lambda functions so let's go ahead and click on create table all right table name let's say there's going to be the planets table whoops didn't mean for that to be in all caps planets all right so for the partition ID let's just say ID leave it as string and let's go ahead and click on create alright I don't know if my screen is or my fonts a little bit too big here let's see there there we go all right so it'll just take a minute or two for your table to be provisioned there we go and what we're gonna do now is just add some items so if you click on the items tab and then click on create item we should be able to manually enter in an item all right so let's just add some planets let's start with mercury okay and I want to add at least another property here so I'm going to from this plus a little icon here let's choose append and let's to string so let's add a temp field all right we're gonna add the temperature so this mercury is one of the hottest I think if not the hottest planets so let's say sizzling hot alright and click Save actually I think Venus may be hotter on the surface but uh just some side trivia there let's add a few more items okay I'm not gonna change that let's say Venus okay add a property string alright let's just say very hot or let's say extremely hot save create item alright let's add one for Earth okay append property temp and let's say warm alright save that and let's add one more let's do Mars and I've never been to Mars but from what I hear it's kind of cold there so let's say cold I think the high temperature there is like 72 Fahrenheit so and much colder at night so let's save that and there we go so we've got a couple of entries in our database to work with alright so that's all we need to do here for now let's head back over to lambda alright so I'm back over here at lambda and I'm gonna create a new function so again same settings as before let's just call this one get item okay set it to Python 3 7 defaults for execution roll and now you know from the past exercise that we're gonna have to give this thing permissions to talk to DynamoDB so we'll do that shortly let's go ahead and create the function we'll set up a function first then we'll go back to I am and give it the correct permissions all right so once that is created let's scroll down to our code editor and so I'll make this a little bit bigger here alright so this time I'm going to delete everything except for import JSON because we'll need that to so let's import our SDK report bodo 3 boto and that that's a really great name I love that so ok similar to what we did before with our s3 exercise we're going to create a service object this time it's going to be for dynamo dB so let's do dynamo DB equals odo 3 dot resource and then we'll pass in dynamo dB so pretty much identical to what we did before just using a different service object this time now another thing we'll have to do is create a basically a variable to hold our table the table in question the table that we'll be using which is the planets table so let's set a variable called table equals dynamodb dot table so then we can pass in a string value representing the table name so we called our table planets just pass in planets and that should give us a good reference to our planets table all right let's set up our function so def lamda handler and this part will be again identical to what we did before we'll pass in a event in context even though we're not really going to be using it in our function and colon enter okay now we're ready to start writing our function body so we're basically going to set up a response object that represents the item that we're retrieving from the table so the the specific planet that we want to retrieve so we'll set that up right away in our response object and then we'll just return that in our return statement alright so let's do response equals now here we can use our table instance table get item and oops that should be get underscore lowercase item and this is just you know the convention followed by the the boat oh three dynamo library you can look up all of the function names and everything that's available so table yet item will allow us to pass in an object to retrieve from that table all right so within this function will specify the item that we want to retrieve by key so let's do a key equals and this is um again here's another comparison with JavaScript where this would look like a sort of object syntax in JavaScript and in Python it's called a dictionary where you have a key value key value pair like this so ID equals and let's retrieve mercury so we'll set the string value to mercury okay and that should retrieve that item for us so let's just make sure that we've got the the correct item print the response just like that and then we'll add a return statement so return curly braces return a status code equal to 200 again and the body will just be our response object or I should say dictionary all right I think that looks good so let's go ahead and save that all right now what I want to do is probably open up I am in a new window this time so I don't have to go back and forth too much all right let's go to roles and this time we're looking for our get item execution role I think I see it but I just want to make sure so I don't make another mistake and add the permissions to the wrong role yeah that looks like it so let's go ahead and click on get item attach policies this time we're looking for dynamo DB so just type in dynamo helps if you spell it correctly dynamo and let's just give it the full access policy attached policy all right so we should be all set up to go ahead and run our function so I'm going to switch back over to my lambda open tab now let's make this a little bit bigger again all right so let's set up our test event say get item test event event there we go create alright now let's run this guy all right looks good so far expand details and here we go so here's our mercury item remember we here now here's the reason we added the temp just so we could see this and verify that we're actually getting the right thing so we had the temperature set to sizzling hot because it's very hot under Curie and that all looks good all right so a very simple thing here we just use the Moto 3 library to to be able to work with DynamoDB in this case we're getting an item I think we'll do one more example where we actually set an item so we'll add a new item to the table all right so let's go back to lambda and we're going to create a new function let's do create function and since we had get item before let's do you do add item where we could do put item it's a little more technical let's say put item there we go and set this to Python 37 again we'll use our default our default with the execution roll so it'll create a put item execution rule for us and we'll go ahead and create that function so this one's going to look very similar to our previous function except we're just going to be using the put item method except for get item in fact we can probably copy the previous function to save a little bit of time okay well alright let's just we'll just go ahead and write it again so again we're gonna import photo three okay we'll set up our service object dynamodb I just didn't want to switch back and forth again you know switch tabs and go back to the old function it's not too much to rewrite it Bodo three dot and it's good practice and again we'll pass in DynamoDB is our resource okay we'll set up our table reference DynamoDB table capital T and we'll pass in planets all right so we've got our our lambda function we're just using the boilerplate set up here alright so we're going to do table dot this time put item put underscore item all lowercase okay and parenthesis inside here we'll pass an item this is gonna be the new item that we're adding to the table so let's add a new planet let's say ID well skip uh Jupiter and Saturn and let's add Neptune just for fun Neptune now what is the temperature on Neptune I'm not sure exactly but I know it's pretty darn cold so let's say super cold on Neptune there we go okay and our return statement is almost done all we have to do is change this JSON message to let's just actually just write a string message will say item added okay and then just to verify that it actually worked we'll go back to dynamodb check the table and make sure our new entry is there alright so far so good let's go ahead and save this code and let's go back over to I am so I'm just going to switch to that tab but I still have open select roles and let's look we're looking for our put item lambda execution roll uh-uh smaller okay where is it okay I don't I don't see the put item roll what the heck I need to refresh this yeah there it is now so yeah if you don't see it right away I'm just learned from my experience there just click refresh if you don't see it and it should pop right up yeah that's cuz I still have the tab open I didn't refresh all right so attach policies again we're looking for dynamo DB we're gonna give it full access just for demo purposes so attach policy and we should be good so let's go back over to lambda again I'll make this a little bit bigger okay let's scroll back up to the top let's add our test event so we'll call this one put item test event all right and no further changes what didn't it like here should be fine I click on create all right and let's go ahead click on test again to run our function Oh something didn't like lambda function Yoel while scanning a literal string literal line 16 let's see what's going on here body item added alright so I'm thinking the problem might be that I'm returning a string is the body instead of an object so let's just try to set up a response object here that contains that message so we'll do something like message I'll set that equal to item added let's see if it likes this better so instead of that string literal I'll just add that response object let's see if this works so save that that would make sense it should be an object alright let's try to rerun our function yeah you like that much better all right cool mystery solved so let's head on over and just verify that our object has been created in the table so I'm going to go over to my other tab here and I'm going to click on dynamodb let's go back to our planets table planets and we should get a list of items if we click on the items tab there we go and there's our Neptune super cold so our lambda has effectively added this item to the table alright guys so that wraps up this tutorial on AWS with Python so I hope you enjoyed it this is just an intro you know to get your feet wet in writing lambda functions using Python and maybe I'll do some more kind of complex work with Python in the future in future tutorials so thanks guys for watching and hope you enjoyed it if you like this video and my other videos other content content on YouTube please consider subscribing to the channel and I will see you in the next video take care guys bye [Music] [Music] you
Info
Channel: Cloud Path
Views: 137,216
Rating: 4.9335227 out of 5
Keywords: AWS Lambda, lambda function, lambda python, python lambda, aws lambda python, aws lambda function python, python boto3, python lambda tutorial, aws lambda python tutorial, aws lambda dynamodb, dynamodb tutorial, aws lambda s3, aws lambda python s3, aws python sdk, aws python boto3 tutorial, aws lambda with python, lambda python hello world, python hello world, aws lambda python hello world, aws lambda python example, aws lambda tutorial
Id: -8L4OxotXlE
Channel Id: undefined
Length: 32min 1sec (1921 seconds)
Published: Sat Jul 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.