Introduction
In the previous article, we learned how to implement java's functional interface& serverless with spring. 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
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.
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
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,
- Write a function that depends on those AWS types
- 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.
→ Once we are on the Lambda service, locate the “Create function” button to create the AWS Lambda function.
→ 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.
Choose the “Author From Scratch” option to create the function.
Enter the appropriate function name.
choose runtime as java 8 or java 11 as appropriate.
→ 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.
→ Once the jar is uploaded, Let’s edit the runtime setting under the “Code” tab to configure the handler as shown below.
AWS Lambda — Runtime Settings-1
AWS Lambda — Runtime Settings-2
→ Now, let’s configure the function name in the “Configuration” tab under “Environment variables” as shown below.
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.
If we further expand the “Details” link further, we can see that message is being displayed as per the input provided.
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
Join FAUN: Website 💻 | Podcast 🎙️ | Twitter 🐦 | Facebook 👥 | Instagram 📷|Facebook Group 🗣️ | Linkedin Group 💬 | Slack 📱 | Cloud Native News ** ** 📰 | More .
If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇
Top comments (0)