Serverless With Spring Boot & AWS Lambda
Tags: cloud, functional, java, aws
Published: 2022-06-16 17:09:28

---
title: Serverless With Spring Boot & AWS Lambda
published: true
date: 2022-05-11 17:04:10 UTC
tags: cloud,functional,java,aws
canonical_url: 
---

### Introduction

In the previous article, we learned how to implement [java's functional interface](https://apurvsheth.medium.com/java-functional-interface-67c27a4cbf79)& [serverless with spring.](https://apurvsheth.medium.com/serverless-with-spring-f65cbb8bea51) Utilizing this knowledge, we will learn how to build our spring boot microservice application compliant with serverless AWS Lambda.

### Tool(s)/Framework(s)

Editor — IntelliJ Idea/Eclipse  
Language — Java 8 or above  
Framework — Spring boot  
Cloud Provider — Amazon Web Service(AWS)  
Build Tool — Maven

### GIT Repo -

[https://github.com/shethapurv/aws/tree/main/serverless-aws-lambda-service](https://github.com/shethapurv/aws/tree/main/serverless-aws-lambda-service)

### Maven Dependency

Please refer to the maven dependency given below which is required for the spring boot cloud function to work on AWS Lambda.

Here, if you have observed, we don't need maven dependency for Spring Cloud Function Web adapter since it's not required by AWS at runtime hence we can exclude that dependency.

![](https://cdn-images-1.medium.com/max/796/1*-JQ8aSfHQfm1wRf9UmC0uQ.png)
_Maven Dependency — AWS Lambda_

### **Maven Build**

To deploy our spring boot microservice on AWS Lambda, it has to be **shaded** which will generate two separate jar files,

— one jar which ends with “-aws” is to be deployed on AWS Lambda &

— another jar that will have the spring cloud function web dependency

![](https://cdn-images-1.medium.com/max/887/1*cBVZ9MlG1Voc3GLrJwelAg.png)

![](https://cdn-images-1.medium.com/max/344/1*TLrj-yYkIvHUKojuQVDo_A.png)
_Maven Shaded Build Plugin — generating two jars_

### Implementation

So now, the question comes of how spring boot interprets the response datatype from the AWS Lambda function. For this purpose, AWS has platform-specific data types which is much more efficient than processing each one individually. so, to make use of these types, there are two choices,

1. Write a function that depends on those AWS types
2. Rely on Spring to extract the data from the AWS types and convert it to a Spring Message and convert it to a Spring “Message”.

Here, we will write a handler as shown below. which we will use while setting up AWS lambda function.

```
**public class** HelloWorldHandler **extends SpringBootRequestHandler** <String, Object> {
}
```

### Deployment

After understanding the basic concepts, let’s jump into deploying the jar on AWS lambda.

→ Login to AWS console. Search & navigate to Lambda service.

![](https://cdn-images-1.medium.com/max/1024/1*8zQB--A7V0pWMVESeP8abw.png)
_AWS — Search Lambda Service_

→ Once we are on the Lambda service, locate the “Create function” button to create the AWS Lambda function.

![](https://cdn-images-1.medium.com/max/1024/1*24KR42fuMkwBkZ5H1lDBAA.png)

→ Next, we will see the function creation page where we need to configure a few parameters as shown below & then click on “Create function” button.

1. Choose the “Author From Scratch” option to create the function.

2. Enter the appropriate function name.

3. choose runtime as java 8 or java 11 as appropriate.

![](https://cdn-images-1.medium.com/max/1024/1*MgGViik-6vEIBLjkLb7_zQ.png)
_AWS Lambda — Create Function_

→ As the function is created now, let's upload the spring-boot jar by clicking on the “upload from” button which will ask us to choose the jar file.

![](https://cdn-images-1.medium.com/max/1024/1*tptMbuLpROwWdL8MQ72tlg.png)
_AWS Lambda — Upload JAR_

→ Once the jar is uploaded, Let’s edit the runtime setting under the “Code” tab to configure the handler as shown below.

![](https://cdn-images-1.medium.com/max/1024/1*lCmPXwCPAHR0qvlUql9mdA.png)
_AWS Lambda — Runtime Settings-1_

![](https://cdn-images-1.medium.com/max/861/1*ODqtllKvYPapxNJqN3i5Hg.png)
_AWS Lambda — Runtime Settings-2_

→ Now, let’s configure the function name in the “Configuration” tab under “Environment variables” as shown below.

![](https://cdn-images-1.medium.com/max/864/1*3HnNo7rlQ8OISV5T7Ma49Q.png)
_AWS Lambda — Environment variables_

Now, we are all set to test our first Lambda function.

### Testing

Yey guys, so are you excited to test our first lambda function after having the adventurous implementation! Let's try it out :)

Let’s navigate to the “Test” tab, enter the required parameter under the “Event JSON” text area & click on “Test”.

We can see below(green area) that our AWS Lambda function test was successful.

![](https://cdn-images-1.medium.com/max/1024/1*jOAwyx88z4YW2x1nSamrng.png)
_AWS Lambda — Test_

If we further expand the “Details” link further, we can see that message is being displayed as per the input provided.

![](https://cdn-images-1.medium.com/max/1024/1*oOi9V8fLAWwdm4SKiLryrw.png)
_AWS Lambda — Test Details_

Hope this post will be useful for many of us.

### Key Takeaways

- Dependency — Maven dependencies required for AWS Lambda function to work
- Build — maven build changes required to make our application AWS Lambda compliant.
- Request handler — Spring boot & AWS Lambda request handler to handle the responses.
- Test — how we can test the AWS Lambda function.

### References

[https://docs.spring.io/spring-cloud-function/docs/current/reference/html/aws.html](https://docs.spring.io/spring-cloud-function/docs/current/reference/html/aws.html)

[![](https://cdn-images-1.medium.com/max/1024/1*BCiLLad3dvZLwBa-B5cAVQ.png)](https://faun.to/bP1m5)

Join FAUN: [**Website**](https://faun.to/i9Pt9) 💻 **|** [**Podcast**](https://faun.dev/podcast) 🎙️ **|** [**Twitter**](https://twitter.com/joinfaun) 🐦 **|** [**Facebook**](https://www.facebook.com/faun.dev/) 👥 **|** [**Instagram**](https://instagram.com/fauncommunity/) 📷|[**Facebook Group**](https://www.facebook.com/groups/364904580892967/) 🗣️ **|** [**Linkedin Group**](https://www.linkedin.com/company/faundev) 💬 **|** [**Slack**](https://faun.dev/chat) 📱 **|** [**Cloud Native**  **News**](https://thechief.io) ** ** 📰 **|** [**More**](https://linktr.ee/faun.dev/) **.**

**If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇**

* * *