Spring Boot — Serverless AWS Lambda With API Gateway

Introduction

In this article, we have learned how to configure & implement the AWS lambda function with a stand-alone java event.

Today, we will learn how to configure & implement the lambda function & trigger it using AWS API Gateway with a practical example.

Tool(s)/Framework(s)

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

Git Repository

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 AWS doesn't require it at runtime hence we can exclude that dependency.

Maven 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 Build — Generated Artifacts

Implementation

You might be thinking that it's all theory but how the implementation would look like & naturally, we start thinking that how we can pass the input params or get the response while triggering the lambda function via AWS API Gateway.

But it is really very simple than what we might have thought of.

Let's go through the classes listed below which are important to understand first.

APIGatewayProxyRequestEvent — If we will drill down a little bit to see this class, we can clearly say that we can get all the request params/variables from the instance of this class.

Spring boot — AWS APIGatewayProxyRequestEvent

APIGatewayProxyResponseEvent — this will hold the response & its status code details.

As we have understood the importance of the request/response wrapper around AWS API Gateway, Let’s look at the handler. we will implement the spring boot request handler which will work as a wrapper & will hold the request & response-related attributes &

Next, we will have to implement our functional bean “welcome” to accept APIGatewayProxyRequestEvent as a function’s target.

Similarly, if we can use APIGatewayProxyResponseEvent to get the response & any other important attributes like HTTP status code.

Very cool & easy stuff it was than we thought of, isn’t it? Let’s move to deployment now.

Deployment

Here, we will concentrate on how to set up AWS API Gateway & will skip the initial AWS Lambda function setup steps since we have learned the same in this article. so let’s start,

→ Navigate to the function created & click on “Add trigger” & choose “Trigger configuration” as “API Gateway”

→ Choose “Create an API” for the lambda function which we created & choose “HTTP API” as API Type keeping security as “Open” & click on “Add”

AWS API Gateway — setup

→ Once, the trigger event is added as an “API Gateway”, the AWS Lambda function would look like this,

AWS Lambda Function With API Gateway

Testing

Now, the question comes that all this is fine but how I can trigger the function & how about the request/response.

→ let's get the API Gateway URL first. Click & navigate to the “API Gateway” which will give us the API endpoint.

AWS API Gateway — URL

→ Copy this URL & access it with the request parameter “userName” which we have configured as part of our functional bean “welcome”.

For Ex:

https://bdq0e9rc58.execute-api.us-east-1.amazonaws.com/default/HelloWorldFunction?userName=Apurv

Output:

AWS API Gateway — URL Access From Browser

Hurreeeyyy … !!

Isn’t it easy & very straightforward? of course, it is.

If I can, anyone & everyone can !

References

https://docs.spring.io/spring-cloud-function/docs/current/reference/html/aws.html

--

--

--

AWS Certified Solutions Architect | Cloud Enabler/Enthusiast | Spring Boot | Docker | Kubernetes | Microservices | Java/J2EE | Terraform | AML | Banking

Love podcasts or audiobooks? Learn on the go with our new app.

S3 the best of 2 worlds

Can Doctor.ai understand German, Chinese and Japanese? GPT-3 Answers: Ja, 一点点 and できます!

A Better Way to Git Rebase

Kubernetes Deep Dive: Service Access

Unspoken Truth about Tech Education in India

Migrate Joomla to Wordpress using docker in 15 minutes

VR Maze Project

Chat With Your Data Using Aigents® Bots

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Apurv Sheth

Apurv Sheth

AWS Certified Solutions Architect | Cloud Enabler/Enthusiast | Spring Boot | Docker | Kubernetes | Microservices | Java/J2EE | Terraform | AML | Banking

More from Medium

Serverless With Spring Boot & AWS Lambda

Supersonic Java AWS Lambdas using Quarkus

Deploying a scalable application with AWS ElasticBeanstalk and Docker

Integrate AWS Secrets Manager With Spring Boot Application