top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Serverless maven magic - deploy spring boot RESTAPI to Aws Lambda using APIGateway



Hello Readers,

Welcome to our step-by-step guide to deploy Spring Boot RestAPI to Lambda on AWS and exposing it through API Gateway. While exploring the guide why would we use aws lambda?

AWS Lambda is one of the most popular AWS service and a core component of a serverless application. It has more than 1,000,000 active customers with trillions of invocations per week.


WHAT IS AWS LAMBDA?


AWS LAMBDA provides serverless event driven platform which allows you to run code without any concern of managing the servers. The only prerequisite is to upload your code to AWS lambda and it does all the work which you require to execute and scale your code on highly available cloud. 


AWS Lambda is also known as Function-as-a-service (FaaS). It runs the code only as the function is required, and no EC2 instances need to spin up in our hosted environment.


WHY IS AWS LAMBDA SO POPULAR?


AWS LAMBDA is so popular because it is ‘pay as you use’ model. It provides fully managed infrastructure which makes easy for rapid development of the product. It is highly scalable and highly available.


Here, In this article, we are going to see how to create a simple spring boot 3 application using Maven Archetype which we will package it as AWS Lambda function and we will build and deploy using AWS SAM CLI then we are going to expose it through API Gateway.


Before get into action, Make sure that you are set with all of the below prerequisites,

  1. apache -maven 4.0.0- beta  

  2. Java 21,

  3. Spring Boot 3.1,

  4. AWS CLI

  5. AWS SAM CLI


Lets Get started with creating Spring Boot application,


We are going to create a spring boot application in IntelliJ (Make sure that you have 2024.2.1 version). We are going to use aws serverless spring boot 3 archetype which is specifically designed for lambda deployment.


  1. Go to IntelliJ -> file -> new Project

  2. Give Project Name

  3. Select Java 21 in JDK tab

  4. Select ‘Maven Central’ in catalog (It will take few secs to upload archetype)

  5. Then search aws serverless, in list of options select ‘com.amazonaws.serverless.archetypes:aws-serverless-springboot3-archetype’

  6. Version 2.0.3


 

Now click create, Now you get the project with already created structure which is designed for lambda.



This is the project structure we will get after creating the project. Now lets see how can we create Lambda function in AWS.


For this, you need to have Aws Account, Login into your console. Search for Lambda it will go the Lambda page. 


Click Create Function, Then it will go to create function Page. In that choose 

  1. Author from Scratch

  2. Give Function Name - Ex:lambdaDemoTestFunction

  3. Java 21

  4. Click Create Function


Now, You have successfully created Lambda Function. Now we need to upload our Spring boot application’s jar file. You can get the .jar by giving  ‘mvn clean package’ in IntelliJ ‘Terminal’

You can see the jar file which is created in the target folder in your project. It is highlighted in the image below.


Now Click your Lambda Function which we created before. You will see page as shown below.


In this page, In Code Tab, We need to upload our Jar file by clicking 'upload from' button. Select your project path and go to target folder, there you can see Jar file. select it and click save.

Now you will get message like ' Successfully created lambda function' . Then Go to Runtime Settings and give your lambdahandler name. This you can get it from 'template.yaml' which is created by default.

Get the above name and give in your Runtime settings as shown below. Now click save.



Now go to Test tab, Give Event name in Event name tab. Then go to 'template optional' dropdown box. In that select 'API Gateway AWS Proxy'. Change the path to your API's endpoint and httpMethod to your request method. In the picture below, we have changed the path to "/ping" and the httpMethod to "GET". This we have given in pingcontroller class.




After changing, click the test the button to see whether the function is working properly. You can see the message Executing function succeeded. If you click the details, You can also see the status code, content type and the body of the your api request which has been called.





Hurray! You have successfully created AWS Lambda function. Now we need to deploy this in API gateway. We are going to deploy API Gateway using AWS SAM CLI. For this you need to be installed the aws cli and aws sam cli in your system. After installing aws cli you need to give aws token (Access Key, Secret Key)to access your account. Give 'aws configure' in terminal and give your token to access your account. Now through cli command we can build and deploy our project.


Go to target folder of your project's path in terminal. For example, if your project is 'cd/users/ideaProjects/lambdaTestProject/target' and give 'sam build'

it will ask some set of questions, give answer as given below.

Now you have created cloudFormation stack resource. Now you are good to create API Gateway and deploy our function. Lets see how to do that.


Go to your aws account. Search for API Gateway, in API Gateway page, click 'create api' and choose RestAPI and click build. Next you will see the below page, give your api a name and click create api.


After creating api, you need to create resource and method for api. you can see create resource tab. click on it. Now in the resource tab give {proxy+}. This way all the request recieved through the gateway will be forwarded to Lambda.


Now create resource and Method. In Method tab choose 'ANY' and give lambda function name which you have created before in the lambda function tab. now click create Method.

Now click deploy api.

Select new stage and give stage name.

And click Deploy.

Now you can see the url in the stage details page. Hurray! You have successfully deployed your api.

Now Get that URL. Try the url in postman to test the api request.

You have successfully created an api and deployed using AWS Lambda through API Gateway!! Thanks for visiting my blog!! Happy Reading :)



38 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page