DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Develop a Spring Boot REST API in AWS: PART 4 (CodePipeline / CI/CD)
  • AWS Spring Boot Tutorial, Part 3: CodeDeploy, Blue/Green Deployment
  • Spring Boot: Handle AWS RDS Password Change or Rotation Without Restarting
  • End-to-End Event Streaming With Kafka, Spring Boot and AWS SQS/SNS (Production-Ready Code Guide)

Trending

  • Tactical Domain-Driven Design: Bringing Strategy to Code
  • Top JavaScript/TypeScript Gen AI Frameworks for 2026
  • The Network Attach Problem Nobody Warns You About
  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  1. DZone
  2. Coding
  3. Frameworks
  4. Making Spring Boot Applications Run Serverless With AWS

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.

By 
$$anonymous$$ user avatar
$$anonymous$$
·
Dec. 18, 16 · Tutorial
Likes (14)
Comment
Save
Tweet
Share
40.1K Views

Join the DZone community and get the full member experience.

Join For Free

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.

AWS Spring Framework Spring Boot

Published at DZone with permission of $$anonymous$$. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Develop a Spring Boot REST API in AWS: PART 4 (CodePipeline / CI/CD)
  • AWS Spring Boot Tutorial, Part 3: CodeDeploy, Blue/Green Deployment
  • Spring Boot: Handle AWS RDS Password Change or Rotation Without Restarting
  • End-to-End Event Streaming With Kafka, Spring Boot and AWS SQS/SNS (Production-Ready Code Guide)

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook