SpringBoot Serverless REST API - ApiGateway+Lambda, deployed using AWS SAM
Tags: serverless, devops, aws, automation
Published: 2023-08-11 17:04:50

**1. Introduction**

Let's discuss the benefits of using AWS Lambda and Spring Boot 3 before we begin. You're probably accustomed to packaging your programmes as JAR files and distributing them to servers. You might even deploy the application to Kubernetes in a containerised form using Docker.

Scalability, security, and patching are all important considerations when managing your own server. You must take into account the volume of traffic your application will experience when you deploy it to a server. You might need to scale up to accommodate the traffic if your server is running at 100 percent of its capacity  

If your server is only being used 10% utilization, you might consider scaling down to save money.

AWS Lambda is a serverless compute solution that allows you to run code without the need for server provisioning or management. You only pay for the compute time you use. This indicates that my own project, which will not receive a lot of traffic, is a great choice for AWS Lambda. This use case is ideal for applications with variable traffic or that can scale to zero.


Most people are familiar with this concept when it comes to deploying functions as a service (FaaS), however in this article we will deploy a whole Spring Boot REST API. According to the AWS Pricing Page, the free tier of AWS Lambda includes one million free requests per month and 400,000 GB-seconds of computing time each month. This means that for the majority of the side projects I'm working on, I can deploy them to AWS Lambda for free.


**2. Prerequisites**

- Read the Quick Start here: https://github.com/awslabs/aws-serverless-java-container/wiki/Quick-start---Spring-Boot3
Thanks to Mark Sailes for all of his help with this tuto

- Java 17
- Spring Boot 3.1
- AWS CLI
- AWS SAM CLI
- GIT


You clone my repository because I have everything ready for you.

git clone https://github.com/Assassin010/SpringBoot-Serverless-Api-G.git

git clone https://Assassin10@bitbucket.org/assassin10/springboot-serverless-api-b.git

git clone https://gitlab.com/Assassin010/springboot-serverless-api-l.git

**3. Agenda**

In this article, you will build a new Spring Boot 3 application, as well as a CRUD REST API, which you will package and deploy to AWS Lambda.

- start.spring.io
spring boot 3.1.0
maven / java
web, webflux

- Build CRUD REST API
post package
Post Record
PostNotFoundException
PostController

- Blog Posts Loader
JsonPlaceholderService

- Test locally using http client before packaging for production


**4. Maven Model**

The Maven Archetype is a project template that you can use to create new projects. The AWS Serverless Spring Boot 3 Archetype is a Maven project that you can use to create a new Spring Boot 3 project that includes an AWS Serverless container for Spring Boot 3. This will also create the exact Maven configuration needed. needed to create an AWS Lambda deployment package. If you are using IntelliJ IDEA, you can use the Maven Archetype to generate a new project but you can also use Spring Initializr. `aws-serverless-springboot3-archetype`

If Maven is already installed, you can use the following command to create a new project:  

```
mvn archetype:generate -DgroupId=my.service -DartifactId=my-service -Dversion=1.0-SNAPSHOT \
       -DarchetypeGroupId=com.amazonaws.serverless.archetypes \
       -DarchetypeArtifactId=aws-serverless-springboot3-archetype \
       -DarchetypeVersion=2.0.0-M1

```


**5. AWS Resources**

To deploy your application to AWS Lambda, you must first create the AWS resources listed below.

- AWS Lambda
- API Gateway

You can create the resources manually using the AWS Console, or you can use code. In this Article you will use the AWS Console, but I have included some resources if you want to learn more about SAM and CDK.

**AWS SAM**

SAM stands for **Serverless Application Model**. It's an open-source framework for creating serverless applications on AWS. It simplifies the definition of AWS resources (infrastructure as code) required to run your application. It also includes a command line interface (CLI) for packaging and deploying your application to AWS.


SAM defines your application using a template file (`template.yaml`). The AWS resources required to run your application are defined in the template file. You should go over that template file and review it before deploying your application.

`mvn clean package sam deploy --guided`
https://aws.amazon.com/serverless/sam/

**AWS CDK**

CDK stands for **Cloud Development Kit** 
AWS CDK is an open source software development framework that allows you to define the resources of your cloud application using familiar programming languages. Because the AWS CDK allows you to define your infrastructure in standard programming languages, you can write automated unit tests for your infrastructure code in the same way that you do for your application code.


**Manual steps**



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gc9n2w11hlag0ce1pku1.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rda1ydbovgvjicq58ssi.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dl5aycaof9q3u4lhdmta.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/olv96gbv6h4mmd03csj8.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihhgj3mrnf8ddsvwtayi.png)


run this command to generate zip file `mvn clean package`




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h6urc8ni8keoe9cry166.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0jwdte1budbij2olo2dm.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99dqmp6v00mutmohdmlw.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekmcob2seicswnr329t4.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p97ffu2kuc2uu614sywk.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ewx35xjqf4dhevai4eu.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pmllexayq0y3xvng0k7z.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3m2pa8zrfwbc4dpik43e.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztnjkg6deh5sne557682.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y9qfwkml5qkth8x4z3lp.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jowu9v9g49nie39rt9y5.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ym7m7bjtohvezn1bq40l.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2sc3c3fmak881w32muh.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hv7pzs5e3mcoowak9ozi.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hzz1fapo40ku20gkfcrw.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3i79c0aewjfww1xzwdq4.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkk7sqlhuo5l0ucn5j9o.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/stbnosvdkdvmempk46n3.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdyich1cd7i0k2g6dah6.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h7pkf6jif450q25ks15c.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uri7nvgp85m0b11xlkr9.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8k4w8gpgdj8def5dsv4g.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8e3g82mqk7w004ld01lt.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j77lp65hdi6p87na3ki9.png)



If you want to use SAM CLI, I created for you a template called template.yaml. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6xjk9q0cgl5xo5qz9tws.png)




What you require is to run the following commands:


`mvn clean package`

`sam build `

`sam deploy --guided`









**5. References**

https://aws.amazon.com/lambda/
https://aws.amazon.com/api-gateway/
https://aws.amazon.com/serverless/sam/
https://aws.amazon.com/cloudformation/
https://aws.amazon.com/s3/
https://spring.io/projects/spring-boot
https://start.spring.io


Happy coding!
END - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

If you like my work and want to support me…
The BEST way is following me on dev.to here

Feel free to give likes, or writing comments so I know how helpful this post was for you.

Gauthier Kwatatshey__
Connect with me on [LinkdIn](https://www.linkedin.com/in/gauthier-kwatatshey-b9a66715b)

[Medium](https://medium.com/@gauthier.kwatatshey/springboot-serverless-rest-api-apigateway-lambda-deployed-using-aws-sam-974085a75ea1)