Spring Boot — Serverless AWS Lambda With API Gateway

Apurv Sheth
Towards AWS
Published in
4 min readMay 15, 2022

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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in Towards AWS

Where Cloud Experts, Heroes, Builders, and Developers share their stories, experiences, and solutions.

Written by Apurv Sheth

AWS Community Builder & Certified Solutions Architect | Cloud Enabler/Enthusiast | Spring Boot | Docker | Kubernetes | Microservices | Java/J2EE | Terraform

Responses (1)

What are your thoughts?