Setup a Local Development Environment for Serverless Apps on AWS | Lambda, API Gateway, and DynamoDB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
working on the cloud is definitely great but it made your drawback during the development stages having to deploy your application each time before you can test the new changes in this video we'll be showing you how to set up a local environment to test your serverless applications without deploying to the cloud in particular we'll be setting up a local api gateway service integrated with lambda functions that operate on a dyno db table and of course there's many different ways to set up a local simulated cloud environment for service applications but for this video we'll be using some plugins attached to the serverless framework now i know you're all excited so let's dive right in so the first thing that we want to do is install all our necessary dependencies now the first of which is going to be the serverless module which just comes as serverless and this essentially gives us access to the serverless cli and other resources we also need serverless offline this essentially gives us access to a local version of aws lambda and api gateway and on top of that we also need serverless dynamodb local and this gives us a local version of dynamodb so these are all the node modules that we will be needing today aside from that an optional module that we can or sorry an optional dependency that we can install is also the awcli and we only need it for a small step but this is optional we'll go over in a second uh why it is actually optional so let's head over to editor we'll just be using vs code in this case and you may have noticed we're actually going to be building off an existing project which is the credit api that we built for dynamodb in a previous video um and the reason we're doing that is just so that we don't have to work completely from scratch um so we can just get started with this um so the first thing we want to do is i guess you can just open up your terminal um i'm going to close that so once you've opened up your terminal um you can just hit npm i or you can use yarn as well but we're going to install this globally so we'll attach that flag and we're going to type in all the three node modules that we're going to install so that's going to be serverless serverless dynamodb local and last but not least serverless dash offline so these are three modules that will install globally i've already installed them so we won't run this command again um once that is done we can actually begin using the aos cli in this step i'm just going to run through it quickly and then i'll explain why it's optional but once you have installed the aws cli according to the documentation page for aws you can just type in aws to activate the cli and call the configure command and we'll just let that run and this will essentially prompt us to set enable as access key id and obviously we will not be using actual aws accuracy id in here we'll just set a random uh like any string whatsoever i'm just going to call this access underscore key id you can name a one two three four whatever you want i will hit enter it will prompt us for the secret access key id um i'm gonna name it as such secret access key id we'll hit enter on the default region name uh we'll just use uswest-2 as you can see i've actually actually already set it but i'll just hit enter for the sake of demonstrating um you can include json here i'm just going to hit enter to keep it empty um and awesome so once all those credential values have been set we can go ahead and take a look at where these values are persisted if i can just magnify this okay awesome so what's what you can do is you can go ahead and use vim or whatever you wish um just open up the file under aws uh slash config and this will essentially tell us the general configuration stuff that we have set such as the region um if we set the format like such as json it would also appear here as well but aside from that we can also look into the credentials file under um the same path if we open that up we can see that on the aws access key id and secret access key id have been set to the values that we have just uh specified and of course the reason why using the aws cli is optional is because we can actually just generate these files ourselves as you can see it's not very um it's not very complicated the syntax and pretty much the format is all included on the database documentation page for the cli um so you can just copy and paste that and create the file yourself but of course the cli offers um other tools as well that might be handy so if you already have it definitely feel free to use it so now once the local credentials have been set successfully we can close this off and now we can actually head over to our code um so if you recall in the my previous video where we built this project we used dynamodb's client and we actually instantiated a new client here and depending on what aws resource you're using um this may be look a bit different for example you're if you're using s3 we would insert the same set of credentials and configuration setup fields uh into your s3 client initialization but we're just going to be using download db here and the fields that we want to include are such so the first thing is going to be the region this is just going to be the region of which uh your application would you want your application to be deployed to i'm just going to be using us west 2. um we also want to specify the access key id and as mentioned before as you saw earlier um the access key id would use is just axis underscore key underscore id and similarly for the secret access uh key id um we just uh wrote a pseudo value or any value for that matter um your value might be different but definitely don't use a real value here just because um we're only using this for local development purposes um yeah the last thing we want to specify is just the end point here um this is just going to be the end point that we have of which uh sorry i wish dynamodb will have access to um so this is going to be localhost running on port 8000 by default and yeah that's pretty much it for the db.js file or basically the configuration file of how your client will integrate into your local setup um so once that is done the last thing we want to do is head over to our serverless.yaml file and we're just going to specify a field field few fields here um so the first thing we want to do is actually just import all the plugins that we have installed earlier the first of which is going to be serverless dynamo db local the next of which is going to be serverless dash offline and we're going to hit save and each of these actually have its own has its own set of configurations that you can set up depending on your use case and like how grand how granular you'll want your lambdas to be or how specific you want your language to be um but in this case we're just using generic lambda as a generic api so we're going to keep at its very basic level uh and it's really basic level of settings as well um so the first thing i want to do is actually create a new custom variable which is what where these two plugins will read from in terms of its setup um so as for the diamond db setup uh we're just going to create a field called download db and we need one thing that we need to specify is the stages and this is going to be the set of stages that will allow our dyno db table to be activated on or instantiated on i'm just going to include dev here because we're currently in the development stage the next one is going to be called start and start is essentially the set of commands or flags that we want to specify when we run the service uh service dynamodb local start command uh the one field that we want to include is migrate and we want to set it we want to set this as true and essentially what this will do is that um because we have instantiated a or initialized um the configuration to deploy download db table below here uh when we set the migrate to field to true it will essentially deploy this table with all its schemas and properties associated to it to locally so it will set that up for us locally and we will have access to it so yeah we're going to leave it like that the last thing we want to set is the serverless offline configuration and we only need one thing here this is optional depending on the configuration that you're using for your alarm does um but basically it's called resource routes and you need to set this to true if you are using a http proxy integration for your alarm does so i'm just going to make a quick comment of that i'm going to set it true but of course depending on your implementation you may not need to and yeah so that's pretty much it in terms of all the setup we're going to quickly uh start this up and run it locally and we'll demonstrate how it all works so to get this started the first thing we want to do is head over back to the package page for the serverless dyno db local package and we just review the documentation basically it tells us that we need to run this command first sls download db install and essentially this will install a local version of dynamodb for us and basically set everything up and after that we can actually scroll down to the section that mentions how we can integrate it with servlets offline right over here and basically it says that um the next command we should be running a service down with db start but because we are using it with the serverless os line plugin we only need to start that up and automatically it should also start our dynamodb service as well so we're gonna head over to our terminal um i just have one open here um and we can enter the command server dynamodb install so we're gonna let that run uh it might take a moment here um awesome okay so now that is now that it's complete um and it's installed in our project directory so definitely make sure before you run this command um to be checked into your project directory that you want um to basically run your application on and so once that is done and all installed successfully we can just begin to run the command sls or serverless offline start and make sure to include the start command as well and we'll just give it a second to load up and this is basically we'll run all the other commands behind the scenes and launch our aws lambda as well as the api gateway service and the dynamodb tables and as you can see here download db local has been started um we'll just give it a second for the lambdas to launch as well as you can see here it's the point awesome okay so this is like a local deployment i realized i made a small um i guess you can call it an issue not really um basically i i forgot to change the region for the lambdas but that should be okay um the lambdas can be a different in a different region than you're done with db table it might not be as performant in a production environment but um just changes i guess to the same environment as your table um when you're shipping this to production um that's for development right now so it doesn't really matter as you can see here um all our lambdas that are listed now serverless configuration file are also listed here as they are now deployed locally um and all the available endpoints are also listed as well so we can just go ahead and quickly test this i've opened up postman um i have this brief configured and we just run our get endpoint right now there should be nothing in the table um especially nothing with uh post idea one i have a batch endpoint here as well i'm going to hit run as well and we can see that there's no items in the table right now and if we just hit post using our create endpoint as you can see here we have successfully created one and if we go back to our batch endpoint uh we can see that um the the entry that we have uploaded just now can be retrieved successfully and just for demonstration for purposes for completion let's just run the put endpoint i've already included sample outputs from my previous video we can see that it's been updated and if we look at the response here we can see that the number of views and the author has both have both been updated from as compared to the original values and last but not least let's just go ahead and delete this thing and once that has been deleted we can go ahead and check our batch endpoint and as you can see here everything is gone so as you can see all the endpoints work successfully if we just minimize this and look back at the terminal we can see all the logs of every single request that was made to all our endpoints so that's why it's nice to have another terminal window open here just to see any logs especially if you're debugging along the way now of course we've only demonstrated how to connect your dyno with db locally in this video but you've now learned the necessary configuration steps that can be extrapolated to other aws resources as well so that wraps up this video and as always i hope you're all staying happy and well out there but for now take care
Info
Channel: Jackson Yuan
Views: 15,319
Rating: undefined out of 5
Keywords: programming, tech, coding, local development, aws, cloud computing, amazon web services, aws lambda, dynamodb, api gateway, serverless, serverless framework, serverless offline
Id: -KRykmVIoV0
Channel Id: undefined
Length: 12min 30sec (750 seconds)
Published: Sat Jun 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.