AWS Lambda Tutorial: Lambda + Serverless = HAPPY

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

In under 10 minutes, we'll have a production and staging server running on AWS Lambda using Serverless. This AWS Lambda tutorial shows how powerful functions as a service are and how easy it is to get up and running with them. The Serverless framework helps you get up and running in production in 3 commands. (2 if you already have the AWS CLI installed!)

👍︎︎ 1 👤︎︎ u/curly_brackets 📅︎︎ Oct 12 2017 🗫︎ replies
Captions
in this video we are going to dive into using lambda from AWS and we're going to use the serverless framework for it which is a great combination super simple to use and extremely powerful so if you're not familiar with lambda it's this really cool new paradigm that provides functions as a service you don't manage servers you don't manage scaling servers instead you provide AWS with your functions and they schedule those across servers and you pay for the amount of time they run and you pay for the amount of requests they receive so let's go ahead and look over at their pricing basically with lambda you get a million free requests per month and 400 thousand gigabytes seconds of compute time per month so what that equates to is the more memory you want your function to have the less free seconds of running time per month you have so if you're doing nodejs which we're going to do in this example then your server will not be running all the time you'll provide it a node.js function and that function will execute whenever it's triggered the trigger could be an HTTP request the trigger could also be something like hey an image arrived in an s3 AWS bucket let's go ahead run our node.js process resize those images and then it's done we're not paying for it all the rest of the time it could also be a schedule job that runs every two minutes or every day or something like that functions as a service is an extremely useful paradigm that in a way that solves problems very easily that would otherwise take a lot more involvement to solve so here's a use case for how much you'd actually pay if you had a 512 megabyte memory function and it ran 3 million times a month and each time that's about one second you're looking at eighteen dollars to handle those three million jobs so that's a really cool pricing structure it saves a lot of money in certain use cases and gives you a lot of flexibility so what I've done is I've signed up for a brand new AWS account and now all I need to do is have no js' installed on my machine and you can do NPM install g4 global server list so server list will be installed on my machine now there we have it and now I can go server list create and this will not work it's going to say hey you have to give me a template are we doing AWS nodejs Python Java so in this case I'm going to do AWS note jf - t4 template and give it the template I'm going to use there we have it we've now created a template go to close this out don't save and so this is my function that I'm providing I'm going to leave this as is for now if you've done any kind of node J s anything this should look decently familiar basically whenever our trigger event happens it's going to run this function and we're going to have all the event information which would be hey was this an HTTP event if so what path what parameters what was the post data all that and then we can send back a response and then we also have this server list Yamma which configures the function so they give us a lot of commented out stuff you don't really need most of it all you have to do really is change your name so we'll call this lambda test and then we're going to leave the provider as is no js' and you can see all this other stuff you can configure its access privileges within AWS if you're used to AWS you know what that all means you can give it custom environment variables if you want some including and excluding rules and then here's the stuff that's actually important your events so let's go ahead and uncomment some of these let's go ahead and snatch an HTTP event here that's the one we want to use we're going to have this thing fire on HTTP event so you can see also it's very easy to have this thing run on a schedule every 10 minutes that's easy to read it could run on s3 bucket changes so whenever a file gets uploaded to an s3 bucket say an image let's go to resize that image to all of the sizes that we need lots of great things you can do with that and that's it let's go ahead kill all the rest of this just make sure it's nice and clean and that's my configuration for my function can do a little bit of white space cleanup here and we're good to go now we're ready to deploy it all I have to do is get some credentials from AWS so I can actually deploy this so I'm going go to my security credentials and you'll see I get a warning that there are better places to do this by creating an IM user with limited permissions but for now for this example I'm just going to do this this totally works these keys will just have access to my entire AWS account so in a lot of use cases for a lot of companies that's way too much access you want to create users with limited permissions so there we go I'm going to copy that access key and I'm going to go serverless config credentials and then I'm going to do provider AWS key there's my key and my secret key that's configured you can see they stored it in user /a WS credentials I can cap that file and there it is that's the file that it created for me so if you've already configured uws CLI this step has already been done for you I can either download that key file or close it and I'll never be able to see those keys again so now what I can do is I can simply go serverless deploy okay there you have it that took right about 60 seconds and it created my service and gave me an HTTP endpoint let's go and click on it and there you have it there's my message my function ran and this is the response you can see over here in the handler that my response is the message and then the entire event so here's the entire event I got the pass I got the method the headers all that stuff everything it would take for me to actually act on this HTTP event so instantly within seconds I have a service up and running Oh whoo that's super great so now I can go ahead and make some changes let's just make this be 1.0 I've got the service up and running now I can just run service deploy again ok there we go that took a lot less time this time around it took about 20 or 30 seconds now I can go ahead and refresh my endpoint and there we go my message is version 1.0 and the other great thing about this is you can real-time create as many stages as you want I can go stage production let's say what I've been working on now is considered the development stage well now I can create a production stage and this is going to send my code out to a production environment it's the exact same thing but I'm going to get a different URL for it so well that's deploying let's come in here and let's add another function so we have our hello function let's go ahead and configure a different function here let's call this one image resize now you can see that my path right here with slash users slash create let's just make it a regular slash now and then let's make this a slash image resize so now we have two paths right now they're both pointing to handler j/s hello method which is the handler j/s hello method so let's go ahead and copy and paste this we'll call this image resize and let's say we're going to do something completely different we're going to post an image up here it's going to resize that image and the response will be the new image so we'll just pretend that this happened it will say resized your image resize your image so that's image resize handler image resize and now we're good to go do you see my production deployment already happens and I have this new URL here so here's my production URL and my deployment my development URL so let's just go ahead and run another service deploy ok there we go that took a couple seconds again and you can see that we now have two different functions we've got our dev function and our image resize function or we've got our home function and our image resize function so I can go here and I can go image resize and resize your image it called a completely different function that function could have been a completely different file or that could be a completely different server list project altogether if that's how we wanted to configure it so if I go over to production you can see there is no route I can't go just slash that's going to stay missing authentication token which means that does not exist so then I could deploy that to production as well and then I'd have both of those functions on production so I know I really flew through that but basically in a matter of a couple minutes I have created a dev and a production environment this could totally be a REST API this could be a lot of other things let me go over here to lamda now and show you that these functions have been created I now have three functions but I'm not paying for them at all because they're not executing I'm only going to pay for them as they execute and I'm only going to pay for the execution time so it's a really great paradigm a really really cool paradigm and it's a lot of fun I'd encourage you to mess around with serverless and lamda when you have some free time it's really going to stretch your imagination on exactly how your web apps can work so that's it for service to lamda I hope you enjoyed the video go ahead thumbs up subscribe you'll get notifications whenever I come out with great new videos and have yourselves a great day
Info
Channel: LearnCode.academy
Views: 306,807
Rating: 4.921741 out of 5
Keywords: serverless, aws lambda, aws, lambda, aws lambda tutorial, serverless tutorial, web development, node.js, node.js tutorial, rest, restful api, restful, api, node.js lambda, lambda node, lambda node.js
Id: 71cd5XerKss
Channel Id: undefined
Length: 10min 4sec (604 seconds)
Published: Mon Feb 27 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.