Developing AWS Lambda Functions Locally in VS Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everybody it's travis here from travis.media today i want to show you how to develop aws lambda functions locally in vs code this means creating building updating testing locally everything can be done from your local dev environment you should never ever be coding in that cold gray lifeless aws console that lambda console should always do it in vs code or whatever your code editor is there was a time where i did that but i've since learned about a great aws application called sam yes sam what is sam well i'll give you aws definition the aws serverless application model sam is an open source framework for building serverless applications it provides shorthand syntax to express functions apis database and event source mappings with just a few lines per resource you can define the application you want and model it using yaml during deployment sam transforms and expands the sam syntax into aws cloud formation syntax enabling you to build serverless applications faster so what does that mean well lambda functions api gateway dynamodb these are all serverless and what that means plainly is that there's no servers to manage with these services in addition there is a sam cli and in our case that's going to help us create build deploy test lambda functions locally but that cli has a bunch of commands that you can use with serverless services in addition sam transforms and expands the sam syntax into cloud formation syntax so in our project we're actually going to define the resources like the lambda function the api gateway whatever we're going to define those so when we deploy it it's going to either deploy them or it's going to update them and that happens in cloud formation so it's an entire framework for building serverless applications and you're going to see all that here in a minute i'm going to make it really simple for you and you're going to love it so all we're going to do today is we're going to create a lambda function locally and i'm going to show you how to build its dependencies deploy it update it as well as test it locally so you can do everything with your lambda function locally and like i said not have to use that console so let's get started okay so step one let's install the aws sam cli so go to google type that in and you'll get this search result click on it and it'll give you instructions on on how to install the aws sam cli on linux mac os and windows so pick your operating system install that first second you need docker running so the easiest way to do that if you don't have it already is to just download docker desktop it's for mac windows and linux so type that in in google and you get the search result click on it download docker desktop and make sure that's running and the third prerequisite is that you authenticate to aws now if you have the aws cli it's as simple as just typing aws configure and then put in your access key which if it's already there just confirm it your access key your secret access key your region and your output format that's going to copy that into your credentials file and you're going to be good to work with aws if you don't have these values you can get them in the console i'll put a link below on how to do it if you don't have the cli you basically just open up your credentials file and put it in manually um i have mfa on my aws account so i have to do it a little different i have to generate a token so i have a script that i use for that so i'm going to run that real quick and if you also have mfa and you need to do this i put that script in my github repo in the gists section just look for a cli automation or something like that and all you do is run it and enter in your six digit uh token which i'm using authy for but you might have authenticator or something like that on your phone and that's all the prerequisites so let's uh clear this and create a new project so cd desktop make directory i'm going to call it sam project you can call that folder whatever you want i'm a cdn to it now the sam cli has a lot of commands you can run so if you type in sam h you'll see all the commands uh the one we're gonna start with is sam init so that kind of gives us a quick start template for a project to get started quicker than doing it from scratch so there's sam and knit there's build there's deploy there's delete there's package lots of stuff we're going to be looking at all of these in this video so let's type sam init to create a new project which template source would you like to use we're going to choose number one the quick start templates what package type would you like to use we're going to choose zip because we're not really uploading an image to ecr and by the way we have to use docker or sam uses docker containers to simulate the aws lambda runtime in your local environment that's why we need docker i didn't mention that earlier so yeah let's choose zip here uh which run time would you like to use i'm going to use python and project name i'll leave it at that and it's going to clone the quick start template from github so this takes a second next we have to choose a template let's choose the hello world example because it's the most basic and something we can start with choose that and our application has been generated so if we ls there's the sam app folder which is the name of your project cd into that and code dot to open it in vs code so let me explain real quick the project structure so let's first delete this tests folder because we don't need it we're not going to get into that in this video and i think that's good so let's start with template.yaml so what's cool about sam is that it actually deploys and maintains all of your resources it's very very similar to cloud formation and in fact it uses cloud formation to deploy your resources and to create change sets to update your resources so this may look really familiar to you we're going to delete most of it to keep this simple because all we need is a lambda function this template actually creates an api gateway as well and puts in some outputs so let's start at the top here so the aws template format version this lays out the capabilities of the template and actually there's only one and it's this date um so you you'll only use this so keep that the same transform is what version of sam you're using to keep that the same description you can just you can put a description here we're going to delete it uh globals you can define globals let's delete that and by the way um if you go to the documentation and you go to aws sam specification you can look at all this stuff so go to template anatomy and it's going to give you the outline in all the parameters and everything so the only required section is the resources section which we're going to keep the rest of this like outputs conditions all of that can be added parameters but you can take a look here it's really helpful but like i said we're just going to be sticking to resources so let me come back here um so if you look at resources there's one resource called hello world function it is of type aws server list function that that tells us it's a lambda function there can also be api or i think it's a simple table for dynamodb but anywhere we're going to stick to function because this is their lambda function so the resource is named hello world function it's of this type and it has properties there's a code uri the handler and the runtime all of which you're familiar with and right below that is events so this defines our api gateway see right here it's called hello world but we're actually going to get rid of that because we don't need an api gateway for this in outputs it's outputting the api gateway url the lambda function arn in the role arn so we don't need that either it's really helpful but we don't really need it and when you deploy that it's going to output that stuff for you is kind of helpful for you to have that information but we're going to stick to just the lambda function and we're going to save this keep going up we have a readme git ignore and init nothing in that and then we have our actual lambda function so here we have an app.pi and all it's doing is returning the status code of 200 and a body of and a body with a message of hello world the rest of this is just comments and my linter is going crazy select linters let me disable that go back to the terminal so like i said most of this is uh documentation here's some commented out stuff um returns parameters yeah let's just delete all this keep it simple so we have a lambda function that just returns that import requests we don't need that either and we're going to save it requirements is just your normal python requirements file saying hey requests is a dependency we'll just leave that and finally there's events so we can send test events to our function to test it locally and i'll show you this in just a minute so if we have our function open we made some changes um we always want to we always want to rebuild it and to do that you just type in sam build and this creates like a build folder of our latest lambda function basically so you'll see up here we have a dot aws dash sam folder in the build folder within that in our lambda function this hello world function and see it has all of the requests dependencies so we could actually just right click open it and zip it up and upload it in lambda it's it's everything you need all of your dependencies in one folder and then we have the template.yaml updated so if i were to come down here and change something uh let's change the name like hello world let's just change this to hello hello function if i were to change that and rebuild i should see the build folder i should see the contents in the build folder change to match that so click on build and it says hello function instead of hello world function so i'm going to come back down here change that back rebuild it so every time you make a change or you're finished making changes be sure to rebuild it now let's say this is the first iteration and we want to deploy these resources it's as simple as and you see it right here in the commands to deploy sam deploy guided guided kind of walks us through the first process of deploying it so let's type that in sam deploy guided let's get this up in aws stack name you can name it whatever i'm gonna leave it sam app aws region us east one confirm changes before deploy this this basically says hey before we deploy it we're gonna give you a chance to confirm it we're gonna show you all the changes down here and you're gonna confirm it that's all so choose yes allow sam cli i am role creation yes save arguments to configuration file we want to do that so that when we update it we already have this configuration saved for us that's going to create a file over here a configuration file so choose yes for that the sam configuration file name we're going to leave it at sam config.tomal sam configuration environment just leave it default and that's going to deploy our resources so let me jump here in aws now if we go to cloud formation we should see two stacks if this is the first time you've deployed with sam you'll see two stacks now the first stack is the sam cli managed default and what this is it creates an s3 bucket to store your artifacts so this is a one-time thing once you do this the first time you use sam then the subsequent deployments your artifacts will just be added to this the other stack named sam app is your resource this is what we're deploying so it's a status of review in progress so let's go back to our project and see that it's waiting for me to confirm it so you see here we're going to add a iamroll and a lambda function of these logical resource ids this is what we're adding choose yes to confirm it and it should deploy the lambda function in the role if we're doing an api gateway or whatever else serverless we would see it deploying as well so now we come back here refresh it should be creating create in progress and this is what's neat as you update your lambda function locally and build it and deploy it it's going to create change sets and just update the parts in only the parts that you've changed so successfully created and updated the stack and if we go to lambda functions we should see that function let me refresh there it is sam app hello world function great so that's how you deploy a lambda function or anything serverless with sam yeah so here's our lambda function here it's got all of their dependencies that are requests brings along with it we got our app.pi a requirements.txt great so next how do we manage our lambda function locally so we've learned how to create one how to build it how to deploy it how do we test it how do we update it so first let's look at testing so go back to your lambda function be sure you're not working in the build folder go back to your lambda function your app.pi and let's add some stuff to this let's imagine there's an event coming in we're going to put a first name equals event first name so there's some event json coming in and we're just going to capture the values and put them in variables last name equals event last name i can't type event last name and then let's say there's a message and then down here uh for body let's just put this the message is let's do an f string message first name last name [Music] so all we're going to say we're going to send a message like hello and then first name last name so how do we test this locally well there's a folder here called events with an event.json file in it and this was created with our template it's got a ton of information so it's got body resource path it's got a lot of stuff in relation to the api gateway that it created but let's just delete this and create a simple one [Music] let's add our values first name let's put travis last name let's put let's put media and message good morning now imagine this is an event coming from somewhere so whatever our event is we just need to grab the json that that generates and pop it in here and then we can test it locally so go back to my app pi i think this is good so the first thing we want to do is build it to have an updated lambda function and right here you'll see it invoke the function with sam local invoke let's try that out sam local invoke we call this uh hello world function and we're going to pass this e parameter you probably see it in my auto suggestions but we're going to pass this e parameter and put the path to our events json so if you look at our folder it says events and the file is event dot json and that's how we pass in this event data to the lambda function so let's run this and look at this mounting uh this function as delegated inside runtime containers using docker here to simulate that lambda runtime in the result down here we get a status code 200 and a body with a message of good morning travis media and that's how we test it very simple just sam local invoke now let's say we're satisfied with this and we want to update the lambda function in aws we don't need to do sam deploy guided again because we have this sam config tomo file which says hey here's the stack name here's the s3 bucket here's the region confirm change set capabilities really cool you can also put in deployment parameters and build parameters or configurations really neat but we're just going to do sam deploy hey we already have a configuration let's just update it so sam deploy and it's actually in aws going to create a change set let's actually watch this for a minute so it's uploading initializing deployment waiting for change set to be created deploy this change set well what's the difference well it's going to modify the hello world function the aws lambda function replacement false so it's not going to replace it it's just going to update some information deploy this change set yes and we should see here in cloud formation the change set created let's go to sam app go to change sets and see right here it's an update in progress so it's actually still running successfully created and if i refresh i should see the last executed change set was yeah one minute ago 1555. and that's it that's your workflow for developing uh lambda functions locally in vs code so just to recap you're going to create the function you're going to define the resources and then you're going to open up your lambda function and you're going to develop when you're happy with your changes you're going to build it you're going to deploy it if you need to test it locally just create an events json with your event data and invoke it locally so test it build it and when you're ready deploy it very simple now if you want to delete your stack you can't do it from the sam cli the sam cli is just a wrapper around the aws cli anyway i forgot the command i probably have it in my auto suggestions aws um aws cloud formation i don't have it in autocorrect but it should be something like delete stack let's try this sam app oh stack name of course so if you want to delete um your resources you can't do it with the sam cli you can do it with aws cli or you can go in the console manually and just delete it so you can come here to stacks click on your stack and just delete it and it's going to delete all your resources there may be a way to do it with sam i the only thing i've read is that you can't do it with sam if you know of a way to do it let me know below otherwise just run this command this should work boom now it should be deleting yeah delete in progress cool so that's it as always if this video was helpful consider hitting that thumbs up i appreciate all of you watching and i'll see you in the next video you
Info
Channel: Travis Media
Views: 566
Rating: 4.875 out of 5
Keywords: aws lambda, develop aws lambda functions locally, test aws lambda functions locally, travis media, develop lambda functions locally, aws lambda functions vs code, aws lambda functions vscode, running aws lambda functions locally, aws lambda vs code, code aws lambda functions locally, how to create a lambda function, create your first aws lambda function, learn aws lambda functions, coding aws lambda functions in vs code, aws sam, aws sam cli, aws lamda functions with sam, vscode
Id: fEZE3rm8Ma8
Channel Id: undefined
Length: 20min 21sec (1221 seconds)
Published: Wed Sep 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.