Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Making Spring Boot Applications Run Serverless With AWS

DZone's Guide to

Making Spring Boot Applications Run Serverless With AWS

Forget the cloud, it's time to go serverless. Using AWS Lambda and API Gateway can reduce costs and overhead, and it's easy to get your Spring Boot app running on it.

· Cloud Zone ·
Free Resource

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

In several previous posts, I described how to set up your Spring Boot application and run it on AWS Elastic Beanstalk. Although this is a great step to go from a physical server to one in the cloud, there is an even better setup possible! Going serverless. That means no costs for any server and no maintenance or configuring of servers! That sounds good, right? AWS has made it quite easy to go serverless with the combination of AWS Lambda and AWS API Gateway. In this post, I will describe what it took for my Spring Boot application that runs on Elastic BeanStalk to run the same functionality serverless.

The first step I took was getting rid of the Spring Boot dependencies, since we don’t need that container anymore. I replaced them with the dependencies for the Spring Core and Spring Configuration. Also ,the plugins were changed to build a JAR that can be used for the AWS Lambda.

The POM's most important parts went from this:

...
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>
...
...
<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
...


To this:

...
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>${spring.version}</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${spring.version}</version>
</dependency>
...
...
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <configuration>
    <createDependencyReducedPom>false</createDependencyReducedPom>
  </configuration>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
    </execution>
  </executions>
</plugin>
...


The next step is to modify the Java code so the RestController functionality is called by implementing the AWS Lambda interface:

public class LambdaFunctionHandler implements RequestHandler<InvoiceRequest, InvoiceResponse> {

    private static final Logger LOGGER = LoggerFactory.getLogger(EasyInvoiceController.class);

    private EasyInvoiceController easyInvoiceController;

    @Override
    public InvoiceResponse handleRequest(InvoiceRequest input, Context context) {

        easyInvoiceController = Application.getBean(EasyInvoiceController.class);
        InvoiceResponse result = null;
        try {
            result = easyInvoiceController.generate(input);
        } catch (ExecutionException e) {
            LOGGER.error(e);
        } catch (InterruptedException e) {
            LOGGER.error(e);
        }
        return result;
    }
}


With this class (and some plain Spring configuration) the RestController functionality that was first called with the incoming HTTP request is now called by a Lambda request. In my case, I was also able to get rid of my Spring Security code, since I didn’t need to secure the incoming request in the Lambda code, as this will be done in the API Gateway.

The next step is to upload the Lambda functionality (the generated JAR file in the target folder) and make sure it works by testing it. I made use of the S3 bucket upload facility and added some environment variables:
screenshot-at-nov-27-20-09-45

The last step is to call the Lambda from the API Gateway by defining the API. See the screenshot for an example:screenshot-at-nov-30-08-21-35


I must say that this serverless architecture might not be great for all use cases, but it should at least be considered when designing new applications/(micro)services or when changes in the architecture are made anyway. Another note is that it took me quite some time and effort to get the API Gateway working with the Lambda I created but I still think it is a great solution for certain cases.

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

Topics:
aws lambda ,spring configuration ,cloud ,serverless architecture

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}