Spring Boot — Serverless AWS Lambda With API Gateway
Tags: cloud, functional, springframework, java
Published: 2022-06-16 17:08:37

---
title: Spring Boot — Serverless AWS Lambda With API Gateway
published: true
date: 2022-05-15 02:08:37 UTC
tags: cloud,functional,springframework,java
canonical_url: 
---

### Spring Boot — Serverless AWS Lambda With API Gateway

### Introduction

In [this](https://faun.pub/serverless-with-spring-boot-aws-lambda-bc76c1de2b12) 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

[aws/awslambda-apigateway-example at main · shethapurv/aws](https://github.com/shethapurv/aws/tree/main/awslambda-apigateway-example)

### 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.

![](https://cdn-images-1.medium.com/max/796/0*Ll1n-Qn-v6zLP7A-.png)
_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

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

![](https://cdn-images-1.medium.com/max/428/1*o9cVV07iRbxpZEVQl-hiJA.png)
_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.

![](https://cdn-images-1.medium.com/max/816/1*T-x5FiMeWLrWiXyRH22-dw.png)
_Spring boot — AWS APIGatewayProxyRequestEvent_

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

![](https://cdn-images-1.medium.com/max/819/1*n3FGfmFj-PVpVX1iFCUs9Q.png)

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 &

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

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

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

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](https://faun.pub/serverless-with-spring-boot-aws-lambda-bc76c1de2b12) 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”

![](https://cdn-images-1.medium.com/max/865/1*xV7R66FWkoXPVqYBqBu8BA.png)
_AWS API Gateway — setup_

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

![](https://cdn-images-1.medium.com/max/1024/1*lds-MLg-OCFntJz1z9i0qg.png)
_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.

![](https://cdn-images-1.medium.com/max/858/1*408MKlpb-m37fuL6qb2Qvg.png)
_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](https://bdq0e9rc58.execute-api.us-east-1.amazonaws.com/default/HelloWorldFunction) **?userName=Apurv**

**Output:**

![](https://cdn-images-1.medium.com/max/835/1*Tr68W4fE1fqJe2L4siQmNQ.png)
_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](https://docs.spring.io/spring-cloud-function/docs/current/reference/html/aws.html)