Create Your First AWS Lambda Function | AWS Tutorial for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi friends thanks for tuning in to learn about aws lambda let's spend just a couple minutes talking about some theory what it is why you'd want to use it and then we'll dive into some hands-on imagine you're working on a project called tiny flicks a platform that caters specifically to short films under five minutes the basic flow looks like this a user uploads a video and then the application needs to do a few things create a small thumbnail for the video a large thumbnail and then also create an hd version of the video and once all that's done the video will display on a page where users can watch it let's talk about how aws lambda might work in this scenario first you've probably heard that aws lambda is serverless computing but what exactly does that mean obviously something has to run your code right so it can't really be serverless well i think it's helpful to say that it runs on a server that you don't have to buy or manage it's still running on a server though but all of the underlying work of provisioning that server setting it up allocating memory and so forth you don't have to worry about that the server stuff it just magically happens in the background so it runs on a server you don't have to buy or manage what exactly runs though well code a piece of code like a chunk of javascript or python or net or what have you and each lambda function will do a discrete task now when does this code run it'll run in response to some event like something gets uploaded to an s3 bucket or a change in a dynamodb table happens for example that triggers your lambda function to run and do its thing and just to wrap up this section on definition some people suggest a better name would actually be aws scripts or functions if that helps you understand a little bit better what they do so small pieces of code that do a specific thing and triggered by some event back to our scenario of tiny flicks and what needs to happen when a user uploads a video these three tasks here in the middle are actually perfect for lambda functions we upload the video it hits the s3 bucket that will trigger these functions to run and create the thumbnails and process an hd version of the video just a quick review of benefits here when you use lambda it really forces you to structure your code in a way that's modular where you have short scripts that do just a single thing which leads to the second benefit which is that you can fine tune the amount of memory a function needs which can help you optimize performance and also reduce your costs and finally kind of related to that first point of having each lambda function do just one thing this usually means that your application can also support parallel processing meaning you can do 100 things at a time when you need to but at whatever point that jumps to needing to do a million things at a time the underlying compute power can support that and it's all handled for you and then when you only need 100 things at a time again things can be scaled down so scaling up and scaling down is a breeze all right with that out of the way let's do a hands-on demo we aren't going to build out the full functionality of a tiny flicks project but we'll do the basics where we upload a file to s3 that'll trigger our lambda function and the lambda function will output the content type of the thing that we uploaded whether that's a video an image a text file that kind of thing now a quick word about price if you're going to be following along here here on the main lambda page there's a link to the aws free tier and you'll see that you get one million free requests per month and there's additional details here we definitely won't be hitting any of those limits with this demo but if you're going on to build something bigger perhaps for your job just make sure you're aware of the additional pricing details down here so check that out all right now over to the aws management console if you need to know how to set up an account and to get here check out the video linked above but we're going to start in s3 the simple storage service this is where we're going to be uploading our files and we want that to trigger our lambda function so we'll create a new bucket i'll call this tiny flicks and then today's date for region you can pick what you want but just one potential gotcha this does need to be the same region where your lambda function is so just keep that in mind i'm going to go with us east 2 and then everything else we'll just leave the defaults and say create bucket i do also have an s3 video check out that link above if you want to learn more okay and then clicking into our bucket here we'll upload something in just a minute but first let's go work on our lambda function so i'll just open up a new tab here in the console and we'll go to lambda i don't have any functions at the moment but really easy to create one so create function and then you've got a few different options on how you're going to create it we're going to start from scratch just because i think it's really helpful to kind of learn what's going on behind the scenes but there are some other things you can use here like a blueprint this will give you some sample code for common use cases there is actually one that works with an s3 trigger here if you want to try that out on your own there's also container images an app repository and so on but let's start from scratch first we need to give our function a name i'll call this my tiny flicks function for runtime there's several options for languages here i'm going to go with python 3.9 but you'll see that you can also use net java ruby and so on scrolling down i'll leave the defaults for architecture but we will need to update permissions you see by default lambda will create an execution role with permissions to upload logs to cloudwatch we'll take a look at those a little bit later but we need to modify this to give permissions to read from the s3 bucket as well so here we're going to say create a new role from aws policy templates role name i'll say my tiny flicks role and then policy templates these are optional but for what we're doing we need one for s3 so i'll just type in s3 here to filter down and we'll do amazon s3 object read only permissions and that's selected there there's some additional settings but we're just going to go with all the defaults and say create function and success so let's scroll down here to look at the code this is just some boilerplate code hello from lambda i'm going to replace this with some other code though that i have uploaded and linked down in the description i actually got this code from one of the blueprints that's available for lambda i'll copy this and just replace everything in here still pretty simple code here so how this works when lambda invokes this function the lambda runtime is going to pass in two arguments to the handler here that'll be event and context event is a json formatted document that has data about your function to process for example this is where we're going to get information about the s3 bucket where we upload our file and then the second argument context this has methods and properties where you can get information about the runtime environment the function and the invocation basically all we're doing here we're grabbing the bucket information from the event and then the specific object or file we uploaded and then we're going to print out its content type such as a video file an image a text file and so on so pretty simple now very very importantly anytime you change your code here you need to deploy it so we'll hit deploy and we should be good to go all right so we have our piece of code remembering back to our definition though it's a piece of code that's triggered by some event so we need to add a trigger here and that's going to be when a new file is uploaded to the s3 bucket so over here we'll say add trigger select the trigger i'll type in s3 to filter that down and then we need to choose the bucket that we created earlier that was tiny flicks and the date and then the specific event that we're going to be listening for we'll leave it at all objects create but there's other things in here as well such as when something gets deleted or restored from glacier for instance but we'll go with all object create events leave everything else the same and then just make sure you check this down here this doesn't really apply to us but it's basically saying if you have an input bucket like our tiny flicks bucket and then you also output something to that every time you output that's going to trigger the input again you'll basically get into this recursive loop so best practice to have a separate input and output bucket but again we're not doing the output so we'll click that okay and hit add okay trigger successfully added now it's not super obvious but adding that trigger wired up a couple things on the back end that are going to enable all of this to work so if we come over to the s3 bucket here come into properties and scroll down you'll see now here under event notifications that we're wired up for the event to notify the lambda function when things are created so that just auto magically happened because we added that trigger in the function and then back on the lambda side here under configuration and then permissions if we scroll down a little bit open up this resource based policy this policy says that our s3 bucket the tiny flicks with the date is allowed to invoke this lambda function so again all of this is happening for you because we added that trigger so super easy all right there's some additional things here under configuration they're a little bit more advanced for what we're doing for what we need we just have the code we have our trigger and now we need to go upload something to the s3 bucket to make sure this works so back to the bucket here and we'll take a look at objects there's nothing in here at the moment so let's go ahead and upload here on my desktop i've got my logo and image file just because this will upload a lot faster than a video i'll drag this over and then we'll hit upload and as we do that that should trigger our lambda function so back to lambda we'll want to come into monitor here and here's where you can view your cloud watch metrics it might take a minute or two for this to come in so don't worry if it's not showing up immediately you can adjust your time frame here though let's say we want to just look at things that have happened in the last five minutes and you can refresh we'll give this a minute to come in while we're waiting though if you're finding this helpful so far i'd really appreciate you hitting that like button so it can reach more people and also consider subscribing for more aws content like this all right i'll hit refresh again and what we're looking for is invocations over here on the left should be 1 meaning we invoked our lambda function once because we uploaded that one file i'll hit refresh again and there you go there's the one invocation the duration in milliseconds success rate was one we didn't have any errors and there's some additional things here as well if you come into logs over here you may also need to hit refresh and more information will show up here you can click into the log stream this will take you to the cloudwatch console and you'll see here the content type this is what our function was outputting is image or png so that's the file that we uploaded to s3 and there you go now if you're following along you might want to just go delete your bucket here back in s3 i'll select this and delete now the bucket is not empty so you'll need to use the empty bucket configuration to clear out all of the files you need to confirm by typing permanently delete and then once that's done you can go to delete bucket configuration which will delete the bucket just type in the name of your bucket and that should be good back to lambda if you come up to your functions you can delete this function just by coming up to action and delete you're not going to be charged for anything if you just leave it here you only get charged when your code is running but just to keep things tidy i'll get rid of that as well so there you have it your first lambda function that works with an s3 trigger thanks so much for watching and i hope to see you in the next video
Info
Channel: Tiny Technical Tutorials
Views: 126,307
Rating: undefined out of 5
Keywords: technical tutorials, technical training, technology, amazon web services, aws, aws lambda, lambda functions, aws serverless computing, serverless computing, aws serverless, s3 trigger for lambda
Id: 3Ar1ABlD_Vs
Channel Id: undefined
Length: 12min 44sec (764 seconds)
Published: Mon Dec 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.