Develop Lambdas Locally in VS Code Using AWS SAM | AWS Lambda Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we're going to create test and deploy an aws lambda function using a simple set of commands and this is all going to be achieved without even leaving vs code i'm not leaving now all of this is made possible by using aws sam and the toolkit extension as they have made the development of serverless applications far simpler than it used to be so if you're a complete beginner or you're new to aws sam don't hesitate in following along as i'm going to keep it very simple and easy to understand aws lambda is a serverless event-driven service that runs your code on demand but what does that actually mean let's have a look at aws's introductory content to help make this easier to understand here you can see there's a basic lambda function in node.js that takes an event as a parameter and returns hello from lambda clicking run will invoke the lambda and you can see the result this example gives us a sense of how lambdas are serverless we upload our code to aws lambda and it is only executed when our function is invoked or called so this is one of the true benefits of serverless computing which they've outlined here with lets you run code without thinking about servers it relieves you the developer of the responsibility of provisioning and maintaining servers imagine if we wanted to have the same lambda as a rest api call on a server we would have to create maintain and pay for a server to be up 24 7 while continually keeping an eye on the server's condition let's have a look at what makes lambda functions event driven now if we click on the next button we'll see an example of lambda's event driven nature i'm just going to click off the browser window now so the events don't get sent so we can see exactly what's going on here we've got multiple sources of events they're also known as triggers and we'll look at them again later we've got examples of a mobile notification streaming analytics and someone uploading a picture to an s3 bucket so as you may have seen a minute ago some events were traveling along the dotted line they made it to lambda and our lambda that we were looking at on the previous page was ran and the output was hello from lambda so once i've clicked back on the page we can see that the stream and analytics are still sending multiple events these cogs represent that multiple instances of the lambda are running and we can add even more instances by clicking on the notification or s3 bucket aws sam stands for the serverless application model and it is an open source framework for building serverless applications sam provides a shorthand syntax to specify serverless resources such as lambda functions api gateway and dynamodb you can use this syntax to model the application you want to create in aws using yaml okay so before we get started we're going to have to deal with some prerequisites the first of those being an aws account because you're not going to get very far without that one and the second is a couple of aws clis so i'm going to be using homebrew to install my dependencies but i'll leave a link in the description with how to install them on other operating systems so let's get started then we're gonna use brew install aws cli okay so once that's complete we're gonna do an aws configure and what this is going to do is it's going to allow us to authenticate to aws for using the cli so this will need your access key and your secret access key for programmatic access you usually get these when you create your account it currently remembers mine for when i had it last installed uh so i'm just going to click enter through all the all of these but obviously you'd enter your details if this was your first time um if you're not sure where to find these i'll leave a link in description and it will walk you through how to generate some new ones okay so the next thing we're going to do is install the sam cli and this is just a wrapper around the aws cli but it will give us all of the things we need to do to use sam okay let's just check that's installed so as you can see we've got the serverless application model cli and some commands which we're about to go through the final prerequisite is that you've got docker installed um i'm going to be using docker desktop as it's got a nice gui and i want to show you something that's happening during one of the demos coming up but any version of docker will do with the prerequisites done let's get started first thing you'll need to do is create a folder and open it up in vs code i've called mine sam lambda feel free to call yours whatever you like next thing we're going to do is go to the extensions tab and search for aws toolkit and install that extension cool and once that's installed you see a variety of aws services this is one of my favorite vs code extensions it's got all the aws services right in vs code which is really handy anyone that uses aws day-to-day knows it's a real pain trawling through the aws console looking for these things and some of the cli commands are hard to remember so hats off to amazon the next thing to take note of is the fact that we are connected to aws as you can see here it's picked up my default profile now this refers back to the configure step we did for the aws cli so if you're not connected at this point go back to that step and make sure that you got everything correct okay so the next thing we're going to do is command shift p to bring up the command palette i'm going to search for aws create lambda sum application we're going to use node.js 14 and we're going to create a basic sum up in the sam lambda folder and we'll just call it some first lambda call it whatever you want okay when that's finished building we'll go back to our directory and as you can see it's been populated with a basic sum up and some first lambda what we're going to do now is have a quick walk through the project structure uh starting with the template.yaml so template yamo is a sum template that defines all of our applications aws resources so this is what is given to cloud formation and it will create or update our applications infrastructure based on this the only thing we really need to worry about for the template at yaml is the resources section as you can see here we've got the hello world function which is of type aws serverless function so this is our lambda this also has an event of hello world and this is type api gateway so this is the main thing we need to worry about the rest of this stuff is boilerplate such as the outputs which is just defining some of the stuff that will be outputted once this has been deployed so we've got a couple of readmes get ignore we then look at the directory hello world and this actually contains our lambda we've got a package.json which will be used to install any of our dependencies for our lambda we've got the app.js which actually contains our lambda as you can see here we've got the lambda handler which takes an event in context but we're simply just returning a response object with a status code of 200 and a body with a message of hello world um pretty simple what i'm gonna do is just make a small change now this is optional if you're following along uh just to spice up our demos a little bit and i'm just gonna require the operating system module for node.js and what we're going to do is just say hello from os dot host name okay with that covered we've then got the tests folder which just contains some basic unit tests and we've got an events folder here which we'll cover in a second so once we're happy with the project structure and we've made some changes what we're going to need to do now is actually build our sum up so what we're going to need to do is go into the directory that actually contains the template.yaml and what we're gonna do is a sum build now what this command will do is create a new directory here there you see it is aws sum and what this is doing is creating some build artifacts for our application yeah as you can see here the build succeeded we've got the build artifacts into the build directory and we've got a build template so let's have a look in this folder as you can see we've got the hello world function which is our lambda so this is all ready to go we could zip this up and upload it straight to lambda and it would work fine but for sam both the build artifacts and the template are needed when we do a deployment of our sam app these build artifacts in the template actually get stored in the in an s3 bucket and that's where cloud formation reads our artifacts from and deploys our infrastructure from there we can also use docker locally to mimic the lambda runtime this also uses the build folder as you'll see in a second okay so once we've built our sum up the next one we're going to do is run it locally using docker and we're going to do that with some local invoke that's a bit squished but the reason i've done this if you keep an eye on the right you'll see that a container is created runs our lambda and tears down when it's finished there you can see it and that's complete so let's have a look at the output and see what went on we've got the start end and we've got this console output and you can see we've got hello from and this is the host name of our docker container so you can see that worked fine now let's revisit the events folder so as we know from earlier lambdas are event driven so what we can do is send an event to our lambda so let's have a look at event.json so this is the event.json that you get as part of the boilerplate code it's a bit bulky so we're just going to get rid of that and we're going to change it to have name and jonathan as i'm a narcissist and what we're gonna do is we're gonna change our lambda code to expect this event and then print that out to the console so what we'll do hello event.name so we will expect in the event so we'll say hello event.name from and then oso's name so important thing to remember is when you've made any changes is that you need to do a sam build again otherwise the build artifacts will be looking at your previous changes okay so once that's built we're going to run this in docker again using some local invoke but this time we're going to pass our custom event into the lambda so that's events dot event jason and as you can see from the output this time we've got hello jonathan which came from our custom event from and then the host name of the container okay so now we've made some changes we've tested it locally we're now going to deploy it to aws and we're going to use sam deploy and as this is the first time we've deployed we're going to use the switch guided and what this is going to do is it's going to walk us through the deployment so we've got stack name samat will do region's fine yes we want to confirm the changes we want to deploy so basically everything we've just done that's fine this is saying that the api gateway is going to be public that we're creating which is fine because it's just a tutorial we want to save all of our answers to these questions to a configuration file the reason it does that is the next time we deploy it we'll remember all these things so it won't ask us again okay so what it's actually doing now is as this is the first time we've deployed it's actually creating the s3 bucket that i mentioned earlier go to our cloud formation stack so this is actually creating the s3 bucket that's going to contain our build artifacts and therefore cloud formation can get them from there if we scroll up you can see that it has successfully created the bucket and now it's just asking us to review our changes and whether we want to deploy this change set so if we go yes and we return back to aws we'll see that the aws sam cli is complete and our sam app's in progress so let's return to the console see what's going on okay so now that that's complete before we rush off to aws let's have a look in the toolkit extension and we can see that our lambda now appears so that's the lambda we just created so let's see it for our own eyes in aws okay so now that's complete okay so now we're in lambda you can see that we've got our hello world function as part of summer and here you can see we've got our sum up hello world function we've also got the apr gateway that we created in the template.yaml so you scroll down a bit further you can see the source code with all the changes that i made so now that it's deployed in aws what we can do is test it so if we configure a test event we can call it test sam and as you remember we're now expecting a name event so we can call that bob if we save that we can now test our lambda on aws and see what the results are and there you go we've got hello bob from the host name of the lambda runtime which is an ip address um so although i think you'd struggle to develop a lambda in this editor one thing it is good for is if you need to make some last minute tweaks so for example if we had hello there you can see that the changes aren't deployed at the minute but if we deploy them it's updating our lambda function on aws it successfully updated it and we can do the test again and now you can see that we've got hello there bob from host name of the container so as i mentioned earlier lambdas are event driven and we've got an event api gateway these are also known as triggers and if you add a trigger you can see the whole host of triggers that we can add similar to the example we looked at earlier okay so that just about wraps it up sam's a great way to just get up and running with very few commands that's a whistle stop tour of aws lambda functions and some sam is a really great way to get up and running quickly this allows you to then focus on learning the details later one step at a time if you enjoyed this video please like and subscribe comment down below if you've got any questions or what you'd like to see me cover next cheers
Info
Channel: Jonathan Davies
Views: 39,097
Rating: undefined out of 5
Keywords: AWS, aws lambda, cloud computing, aws cloud
Id: mhdX4znMd2Q
Channel Id: undefined
Length: 15min 49sec (949 seconds)
Published: Mon Apr 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.