Deploy Spring Boot Serverless CRUD API to AWS Lambda 🔥 | API Gateway | @Javatechie

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to Javi in this tutorial I will guide on deploying springboard application to AWS Lambda if you are a looking for an affordable way to host your spring boot project then you are in the right place this tutorial covers everything you need to know including the benefit of serverless computing and a stepbystep deployment guide okay all right before we jump into the deployment process to AWS Lambda let's take a moment to discuss why we might want to choose this approach so if you think traditionally we do deploy our springboard application packaged as a jar or War file or if it is a containerized application using Docker we deploy it to the cuetes for example let's say I have this spring board app then I need to generate the jar or War file then I need to deploy it to any server it could be your tcat or it could be elastic B stock okay similarly if my springboard application if I'm using containerized platform then I need to create the docker image then I need to deploy it to The Container platform like kubernetes or if it is a AWS I can go for eks so this approach works well then what is the problem here simple right so let's consider an use case you have deployed your application to a single server forget about this container platform we'll discuss that in other tutorial now consider your application is deployed to the single server which can handle 10 million request per second but in future your application is getting more request let's say 100 million per request okay then this particular server will not able to handle those many request in a single go so for that what you need to do you need to take the heck of scale up and scale Down based on the number of request coming to your app correct now that's a challenging task for us isn't it it comes with the responsibility of managing and maintaining your own server including patching security and scaling so being a developer you need to take care of managing your server which is really a tedious job isn't it so how to overcome this so that is where this AWS Lambda came into the picture okay AWS Lambda on the other hand is a serverless compute service that allow you to run the code without the need to provision or manage the server so you just generate jar or R file give it to AWS Lambda Lambda will take care of managing the server and scaling It Up and Down based on the need so with AWS Lambda you only pay for the time you have consumed it which makes it an IDE solution for project with variable traffic that can scale to Zero by deing your spring board app to the AWS Lambda you can offload the Bon of server management and focus on developing and delivering your app that's quite Innovative isn't it now that we understand why AWS Lambda is great for hosting our application now let's quickly demonstrate this AWS Lambda by considering a crowd API example so what we are going to perform we are going to create a spring boot app let's say spring then we'll deploy it to the AWS Lambda then we can try accessing this application which is hosted to the AWS Lambda which is serverless but rather than doing that we'll also add one API Gateway so that user can send request to this API Gateway and from API Gateway request will route to the corresponding Lambda function okay which is nothing our Crow app fine this is what the simple scenario we are going to develop so without any further delay let's jump into the code let's get started [Music] so first let's create a spring board project from the scratch I can create the project from the intelligent idea from this particular page but let's not do that rather let's create a spring boot project by using the mavin Arc Type designed for AWS Lambda deployment okay so if you'll go to your intellig idea and if Will click on file new project give the project name AWS now choose the correct jdk version you want to use now in catalog you need to choose the mavin central then if you see here there is a section called Arc Type and here you can find the drop down down just type spring boot 3 okay or just type spring boot if you see here this Arc Type for spring boot 3 this is for spring Boot 2 and this is for lower version since we are using spring boot 3 we'll choose this particular Arch type to create a Lambda specific project and then we'll validate what all things was inbuilt by this mavin Arc Type okay just click on okay if you see here it will take few second to download the dependency now if you go to the pom.xml which was generated by this Maven Arch type you can find something a serverless Java container spring boot 3 which enable running spring boot app on AWS Lambda and apart from that if you'll scroll down in the build plug-in section you'll find something called assembly hyphen Jeep which will generate the G file of your app rather than generating a jar file it will generate a compressed G file which you can upload to AWS Lambda we'll we'll learn that in a moment but this is what the few inbuild thing will be added by this Maven Arc Type when you are creating a Lambda project fine now also if you'll verify the project structure you can go to the SRC main Java and if you can see there is a class called stream Lambda Handler which was autogenerated if you'll open this class you can find something stream Lambda Handler Implement request stream Handler okay so basically what this particular class will do it will act as a entry point for AWS lamb Lambda to Route request to a springbot app using the AWS Lambda proxy integration all the request will landed to this particular Handler now Handler will forward that request to your Lambda function or the spring boot app what you have design okay apart from that if you also observe there is something called template. yml This is the file also autogenerated so if you read the content of this file what it says okay this is of AWS serverless function and this is the Handler will receive all the request what Java version I mean there are few information right this template simplifies the deployment process by automating the setup necessary AWS resources and configuration which requir to host a spring boot app to serverless API on AWS Lambda okay so this simplifies your task to make the things ready for your deployment I hope you see how the MAV AR type for AWS Lambda and spring boot streamline our work if so let's go ahead and start writing our crowd API so first what I do I'll create couple of packages so by default it also created One controller class with some static message you can taste it but since we are going to perform the crowd API let me create another controller class here let's say we'll deal with the course okay so I'll Define course controller okay so let's start create our dto first go to the D create a new class called course then let's define couple of field ID name and price so let's add the lbook specific annotation to avoid the getter and Setter writing it manually so it seems we don't have the lbook dependency let's add the dependency first go to the p. XML then just scroll down and add the dependency so I will add it here anywhere it's fine right it's just a dependency so just add it here just update your project now just Define The annotation I need all argument Constructor I also need no argument Constructor I'm not going to use the database rather I will create a list and we'll perform the crowd operation on that list so that is the reason I have not Define any entity it's just an just my dto class okay so you can give a try by adding a database instance from the cloud or from your local you can give a try but for now let's go with the list now let's start create the service class course service annoted this at theate service then let me add the crowd operation specific code since we are playing with the list there is not complex logic just add it here just import the course okay all good so there is no recet science in this code we just adding the course object to the list retrive all from the list retrive a course by ID by just doing the filter then update the course just update the index in the list then delete the element from the list okay that is what the simple logic we have now similarly let's create a controller class it's already there let's annotate this at theate rest controller at theate request mapping Define the root URL SL courses okay now just inject the service class and Define the endpoint so let me simplify this by adding the code we just need to import all the required classes all good right so again this is the simple end point we have the post mapping cre mapping put and delete to perform the crowd operation okay from controller we are just calling each and every method from the service that's the simple logic we have correct so all good since we are done with our project now next let's package it so that we can deplay that package to our AWS Lambda okay that is what we understand right so we just need to create the jar or G file which we can directly upload in AWS Lambda so you know how to generate the jar file right or you know how to package your app so just go to the mavin then go to the life cycle just install it if you don't want to do in this way you can also run mvn clean install or mvn clean package okay it is up to you so I'm using this approach to build the app so build is succeeded now if you'll expand your target folder let me Zoom this for you so if you expand this target you can find it created a package. g okay so this particular G file contains all your packaged class which is required to run in AWS Lambda with this file ready we can proceed to deploy it to the A s Lambda so to do that to deploy your app to the AWS Lambda you need to go to your AWS website okay you need to go to your AWS console then First Step you need to create a function okay makes you to choose the correct region I have choosed here a Pacific Mumbai so apy South iph 1 now here I need to create a function just click on it then just add all the required field first choose it author from scratch then give the function name so I'll give course ien service then choose the runtime for us the runtime is Java 21 right so we have used jdk 21 so runtime is correct now nothing to do just click on create function once your function created you you will see this page now if you'll scroll down in the code section you'll find a option to upload from just click on this upload a do Gip or jar file click on it click on upload then you need to give your jar file okay so which we just generate so we have created this project right just open it go to the Target and give this G file If You observe the size of this particular Gip is 17.1 MB okay just click on it click on open then click on save so you'll see these messages once your G file is upload successfully now what you need to do you need to come to the configuration not here if you'll scroll here you need to edit your runtime setting Okay so you need to set the Handler right so if you remember it created a by default Handler for us which is stream Handler this is what the Handler it created for us okay we need to provide this Handler information in the runtime setting so that all the request will come here then it will delegate to your application Okay cool so the package is org. do example copy the class name you need need to provide fully qualified path of it just go here this is the class name also I need to copy the package org. example org. example stream Lambda Handler and call the handle request method okay all good click on save so the function is updated successfully now all good now since our application posted successfully to AWS Lambda what we can do we can test the API okay so just click on this test then just give the event name which you want to test let's say course API now just choose this API Gateway a proxy now here just provide the path of your endpoint for us the endpoint let me minimize this if you'll go to the controller class the end point is/ course courses right so let's try accessing this gate mapping to get all the course and we'll see the result okay just go here change the url and HTTP method of type get all good now just test hit on the test button can you see here the word is empty because it is not a DV right we have used the hardcoded list object here so if you'll fetch anything from the list currently we have not added anything to this list that is the reason we are getting empty response so let's add something to the list then we'll validate that okay so rather than play with this particular Lambda function function let's integrate to the API Gateway so that we can happily access all our Endo okay so you can see here currently the result is good our spring boot app is up and running in AWS Lambda you can see the spring boot Banner right okay so this is not the way to access your Lambda this is just for the testing purpose but to access it let's quickly integrate the API Gateway so to create this Gateway just just I duplicate it then go to the service again search here in the service Gateway you can find this right click on this API Gateway so this API Gateway will act as a proxy forwarding incoming request to our Lambda function okay so this is the homepage of API Gateway scroll down so yeah you need to click on this rest API click on bu build give the API name I will give something like course service or course R AP anything you can give select new API then give your name don't touch anything just click on create API fine you'll find this page now here let's click on create resource give the path here okay so what I'll do just def here proxy plus okay in this way any request coming to API Gateway will redirect to my Lambda okay that is what if you see here in the template. yml file see here this is the path from API Gateway fine just go here create the resource next just click click on create method method of Type n because I will trigger the request from Gateway to the Lambda function so I can define define the method type as any rather than defining individual HTTP method and here I'll choose the Lambda proxy If You observe here I got the option I mean it was getting my Lambda function here right choose that then all good click on create method now you can see this dashboard right so from here you need to deploy your API just click on deploy API new stage give any stage name so let's consider it as a Dev or PR U whatever you can Define Okay now click on dplay now if you'll expand this you can find different typee of HTTP method but let's try to access our course endpoint from the root URL this are the root URL okay which we deployed so just copy this go to the postman new HTTP request paste the URL first let's add couple of course object then we'll try to face them so let me build the course subject give some name then price give something okay now let's send the request we are getting this error region we have not specify our endpoint URL okay so go to the controller class copy this then SL paste it now send the request you can see the result here right course is added to the list or you can consider added to the system now let me add another course to let's say hynet we have added two record let me add one more record so the post API is working correctly we're able to perform or add the recuit to our system now to face that all the course available course you can change it to the G remove the body send the request you can see all the three record here the gate end point is also working now if you want to filter a single course subject just add it send the request we can see the result right so this is what endpoint we are exposing now if you want to check the put mapping what the input we are giving course subject and the ID which you want to update just go here let's say I want to update price of this course which will be of type put ID is correct 101 so let's say I want to update price fine now if I'll send the request it is updated now to validate did that I can copy this go to the browser you can test from here but you'll find the better visibility in the browser see here the record got updated correct now the last one let's try to access the delete API so we'll delete this particular record okay so go to the postman ID is on3 I want to perform delete remove this send the request no content now if you'll refresh it you'll get only two record okay all the crow API create update retrive and delete successfully you are able to access from the API Gateway we send request to the API Gateway API forward that request to the Lambda function now Lambda function forward that request to the stream Handler class and stream Handler connect to our end points okay we are success sucessfully deployed our application to AWS Lambda which was designed using spring boot 3 and we are able to access it by integrating to API Gateway right yeah so that's it if you have any doubts specific to this Lambda function or integration of API Gateway or anything or any concern feel free to raise your hand in a comment section I'll definitely look into the comment and I'll will try to respond them that's all about this particular video guys thanks for watching this video meet you soon with A New Concept
Info
Channel: Java Techie
Views: 15,100
Rating: undefined out of 5
Keywords:
Id: J0aEfUUervE
Channel Id: undefined
Length: 25min 41sec (1541 seconds)
Published: Fri Jun 21 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.