SIMPLEST SERVERLESS APP WITH AWS CDK (API Gateway, AWS Lambda and DynamoDB)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello world and welcome to another episode of fubar in today's episode we are going to get started with cdk for serverless if you want to know more about serverless cloud computing or software engineer practices subscribe in my channel in the red button below i post videos on tuesdays and on thursdays so let's get started so this is something new uh i've been trying out that is cdk uh it's not new because it's been there for like over a year but i have never explored it until now uh when i was talking with one of my guests martin bibi and he showed me the power of cdk uh to build target applications and then i was like wow i really need to try out by the way his uh talk is coming in the next week so stay tuned for that subscribe and all that things so you are reminded when that video comes out but yeah so i got inspired by by talking to him and i started trying it myself and i find that there was a lot of material and a lot of things out there to get started so i started building my serverless applications so the first thing i noticed that for making the typical application for serverless maybe cdk is not the optimal way at least in my opinion i'm way more efficient with some and it's kind of easier to build a typical api lambda dynamo thing combo with some but when we get to more um non-server-less components then cdk starts shining so if we need to build an application with bpcs rds so for example one demo i wanted to do for a long time is to well not for a long time because it's been generalizable for a few weeks it's the earliest proxy demo to show you how you can connect a lambda function to an rds proxy but building that with cloud formation it's very very painful and when i saw how easy is to do it with cdk i was like wow another thing i want to show you is how to do lambda with a this elastic file system that was announced a few weeks ago as well and also with confirmation is very painful but with cdk is really nice so those are videos that will be coming out in the future but before doing that i want to start from the beginning on building serverless applications with cdk showing you a little bit on where you can find information and things like that so let's go to the screen and get started what i want to build is a simple app api gateway i love the function and a dynamo i will not put really a lot of code in that function it will be just the plain hello or something super simple because that's not the point of the video but i want to create the whole infrastructure and i think with cdk is more complicated than using some but i think making this video will open the door to do other things with cdk and serverless that it's way easier to do it with cdk than with serverless so that's what i want to start from the beginning and get started so the first thing you need to do is to install cdk you can find the information on how to install it in their github repo cdk is an open source project so everything is there make sure that you have uh updated your uh cdk components because it's changing all the time so make sure that you have the latest versions what i'm showing you in the video is for sure not the latest version that you will be seeing it's changing all the time so to install it just follow the instructions here and then configure your aws account and you will have it installed it's a node project so it's as simple as running npme in your globals with the aws cdk nothing complicated so what we are going to start is with a basic application so i will create a new directory and then i will initialize an application there so let's copy that and go to the terminal so i will do make there cdk serverless get started and then i will go cdk server let's get started and there i will paste this command cdk init sample app is defined it's the sample application you will get everything you need it's a template and then you need to find a language i will be using typescript it's as similar as javascript so for me it's the easiest one to go you can choose between all the languages supported by cdk so now it's installing and basically we will get a full application that if we deploy right away it will be working but i don't want to deploy it right away i want to remove everything that it builds and just keep the structure and start adding my own components the api gateway the dynamodb table and the lambda function so i will open this in visual studio code and we can check what is inside let's make the font bigger and check what it has created for us so the first thing in the bin folder you will find the root of your application so it needs to start from somewhere and here is the name of our cloud formation stack that is represented in this kind of module the cdks serverless get started stuck if we have multiple stacks we can define them here as well and this is inside the stack definition is inside the leaf and here we can see that there is some resources that i will remove there is a queue and a topic and we don't need it so i will just remove everything and here we will write our infrastructure code but we will get that there in a moment then i have my known modules and i have a test folder this comes out of the box basically these tests will not work after i deploy so i might remove them as well but if you want to know how is to write tests for infrastructure let me know i have never used cdk in a production product in production so i don't know how to write tests for infrastructure using ctk but if you're something you're interested i'm pretty sure i can find someone that can talk about that so just let me know in the comment box below then we have our different files and the cdk configuration the testing configuration the node configuration and yeah some type script configuration good so this is the file that we are interested in so i will remove everything and just leave this file so we want api gateway dynamo and lambda so those are the first things that we need to import so this is a language so we need to import the libraries and i will bring lambda dynamo and api gateway to our project then i need to go to the terminal and i need to add them in my uh project because these are uh it's an old project so i need to bring these modules over and i just do mpm install and i add all these free modules and then after that we have the modules ready and we can start using them so this takes a little while while it's downloading all the modules into our project and then we can start using so the first thing i want to create is a dynamo table we will not really use it in our function but i want just to create it so you can see it uh dynamo i will start because it doesn't have any dependencies with anything so it's pretty straightforward to create this is kind of nice because this creates the table for us with almost all the uh the false that it can so we just defined like a constructor is called uh construct the dynamo construct and we give a name to table in this case hello and then we define what is the name of our uh partition key that is the main key the only thing we need in a uh nosql database in the diagram of the face that is key and value we need the key and this is type is a string of the name name and here you can see that we have some errors and these arrows is something i found out yesterday and i led it to show it to you so you can maybe you're having the same problems and you don't know how to debug it so if you look here i will remove these three this is our package json where all the dependencies are listed in our node project and you see that the last three packages we installed are version 1. 1.51 and the one for the core is 150 so we just need to update that there and then if i go to the terminal and do npm install that will fix the problem for us so if you are facing that problem the cdk version changes all the time you might need to uh update it so whenever you are getting the latest packages from the internet then yeah those are coming as well so that's something i leave there for you to know so now if we save then we will see this happening so i will put here well it doesn't oh yeah i need to put it out of there it should be inside the constructor and here we comes table and there we have our dynamo table now let's create our lambda function and we do the same we create a new construct using this lambda function we pass a name dynamo lambda handler whatever then we have defined the run time we say where the code is and where the handler is and then we can pass environmental variables this might look very similar to some or other things in the same things that we need to pass uh so now we said here that there is this a folder called um functions so we need to maybe call created functions and it should be in the root so i will move it there and inside here we have a file called function js and inside here we will have our method that is doing the kind of whatever the handler is doing and i want to keep it as ridiculously small as possible so i will just print hello in the screen nothing fancy because this is not the point of this video but you can see that there is a functions folder with a function file with a handler method that's basically what we want and then if we want to get access to the table name we could get it from the environmental variables so now we have the lambda we have the table and we want to write on the table in the hypothetical place our function is doing something interesting we need to give permissions to the function and for that we are using this method that is a table grant read and write data so this is a cool thing of this types language that you can see what is available so if i type brand you can see that there is full access read access read write data read stream there is all the kind of policies defined there for us so this is kind of nice we don't need to write them and it's all kind of auto documented there so this is something i really like from these type languages so after we have that then we want to have the api gateway and for the api gateway the first thing we want to do is to create an api gateway and i will create an api gateway that is basically called hello api and here i need to start adding routes to it so i will add a route with one method and one path and i could add as many as i want with uh that they are integrated to lambda so basically this is as simple as doing this api dot root and then add the path for hello and the method get and then you just do the lambda integration there with the lambda that we just wanted so if we want to add more um roots then it's just as easy as say api root and then add blah blah blah blah so and the last thing we can do is create outputs so we can see things in our when we deploy we want to see the url then we can just add this output that will get the url for the api gateway and yeah that's basically all the infrastructure is a little longer than what we will do with some to create this type of infrastructure but still is pretty readable and i think this starts shining a lot when we are doing things with vpcs and more complex stacks that are not only using serverless components so let's deploy this and see what happens so i will do a cdk deploy i could do cd play cdk thin and this will print us the cloud formation that this will output this is kind of handy if we want to do some kind of ci cd that we get the cloud formation and then we deploy the cloud formation independently but from our computer we just do cdk cdk deploy and this will start deploying first it will ask if everything that is being created is good for us so we can see all the resources that are being created for us and we can review them and we can see all the different permissions that are coming as well so that's good and we can say yes and then this will start deploying our application for us so it takes a little while it's first is doing the synth for us and then is deploying it using the cloudformation service so there is no much uh nothing new there is like some that it does that also the conversion to cloud formation and then it gets deployed with cloud formation so if we go to our aws account to cloud formation we should be able to see this cdk serverless get started thing here and we can start seeing that there is things being created and they're in process so it takes a little while while it happens and when it's ready then we can try it out with postman and see if it's working but it's a very very simple way to create a super simple application and in from the lambda perspective from the code nothing really change everything stays the same because there is nothing new there so you can write your lambda functions as you used to the only thing that it changed is how we define the infrastructure so that's kind of what i wanted to show you today so let's wait for this to complete and and then we can check it out good so we got our output here that well kind of cdk also output it's for us so we don't need to worry and we can copy that in postman and we can put hello and then we can see that we get the hello back so if you want to start learning these things to do go to the aws repo and check it out and read it through another thing you can do go to the aws cdk documentation and read it through there is a lot of things there on how to do things a lot of examples that you can basically copy paste if you're very lazy and you don't want to do anything you can go to this project called cdk patterns there is i don't know how many patterns there divine there is an also kind of open source thing you can add your own patterns here and there is many here that are solving simple problems or common problems i will say because some of them are not simple at all um you can check it out as well and build your cdk infrastructure just by copying pasting and as well i will leave this repo i just created in my github as always so you can access it and tell me how you feel about cdk i will be doing in the future videos about it because there is a lot of topics that i really want to get into with uh more advanced topics as i said working with bpcs and rds and things like that that i've been avoiding because i'm super lazy to write a confirmation for that but now with cdk it's way easier so i will start showing you some more advanced demos i think uh with cdk than with some but it's also it's something i need to learn i don't know much about it so it's going to be interesting i will be doing an interview where we will go into cdk and fargate i will leave it in the description box when it's available and also linked in the resources of this video so you can check it out i will be talking with martin bibi and we will be learning about a lot of things about cdk in way more detail but i want to do more things for serverless so well that was the video for today i hope you like it if you did give a big thumbs up and tell me what kind of use cases you would like me to see working on with cdk sure don't tell me build ec2 machines i will not go that track uh but tell me about serverless use cases what kind of things are you interested in and if you like to know more things about testing that infrastructure also let me know in the comment box below i will try to source someone that knows how to do it but yeah that's it for today and i see you in the next episode of uber [Music] [Music] you
Info
Channel: FooBar Serverless
Views: 20,465
Rating: undefined out of 5
Keywords: infrastructure as code, cdk, aws, amazon web services, aws cdk, aws cloud development kit, aws cloudformation, aws tutorial, aws cdk tutorial, aws cdk demo, amazon cdk, devops, aws cdk typescript, aws cdk typescript tutorial, aws tutorial for beginners, aws cdk tutorial javascript, aws cdk tutorial typescript, aws cdk serverless, aws cdk lambda, aws cdk api gateway, aws cdk dynamodb, aws cdk typescript example, aws cdk typescript lambda
Id: XVHGq2uJu9s
Channel Id: undefined
Length: 18min 18sec (1098 seconds)
Published: Thu Jul 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.