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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

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
  • How to Build a Chat App With Spring Boot and DynamoDB

Trending

  • Metrics at a Glance for Production Clusters
  • Data Quality: A Novel Perspective for 2025
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  • MySQL to PostgreSQL Database Migration: A Practical Case Study
  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
39.8K 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
  • How to Build a Chat App With Spring Boot and DynamoDB

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!