Home » Java » Enterprise Java » Deploying a microservice – Spring Boot fatjar to Amazon Elastic Beanstalk

About David Kiss

David Kiss
David is a passionate software engineer specializing in building scalable web-applications using Java, Spring, Hibernate and Apache Camel. He is based in Toronto, Canada and is the founder of David Andras Consulting Ltd.

Deploying a microservice – Spring Boot fatjar to Amazon Elastic Beanstalk

Recently I was working on a proof-of-concept web application that I wanted to deploy to a public cloud for a quick demo.

I decided to use Amazon as I already had experience with it. Amazon offers several different ways to deploy a Java web application.

Amazon EC2

EC2 gives us the flexibility to install and configure whatever we want to on the box. This is a very flexible solution for someone who wants to take controll over how their application is deployed and running, but it’s drawback is that we have to do most of the server installation & configuration and the application deploy tasks by ourselves.

AWS Elastic Beanstalk

The Elastic Beanstalk is an easy-to-use service that automatically handles deployment, auto-scaling, load-balancing and health monitoring. Using Elastic Beanstalk anyone can deploy a web application to the Amazon cloud with just a few clicks.

I decided to go with the quick and easy Elastic Beanstalk option…

Elastic Beanstalk has various ways to deploy Java applications:

  1. Using Docker
  2. Deploying a war file to a Tomcat server
  3. Deploying fatjar executed from the command line

In this post I’m going to cover the option using a fatjar, which is basically a single jar file that has all classes and jar dependencies bundled in it.

To make this working in Amazon, the jar file has to be placed inside a zip file.

Create JAR and ZIP files

In this post I’m using Maven to create both the jar and the zip files discussed above. Let’s walk through a sample pom.xml file to see how it’s done:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <relativePath />
    <!-- lookup parent from repository -->
      <!-- NOTE: We don't need a groupId specification because the group is org.apache.maven.plugins ...which is assumed by default. -->
            <!-- this is used for inheritance merges -->
            <!-- bind to the packaging phase -->

This file is based on a pom.xml file generated for Spring Boot web applications at http://start.spring.io/, and it includes a few additional changes:

  1. Make sure the packaging tag is set to jar
  2. The spring-boot-maven-plugin will take care of creating the jar file including all jar dependency inside the file itself
  3. Configure the maven-assembly-plugin to generate a zip file from the jar file using the src/assembly/zip.xml descriptor file

After configuring the maven-assembly-plugin in the pom.xml file, we’ll also need to configure the zip.xml descriptor that tells the assembly plugin how to construct the zip file itself.

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
 xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">

As you can see above, we configured the output to be zip, which is one of the many supported formats.

We also configured to include the jar file in the top level directory of the zip file. And that’s all needed by Amazon to run the application from the command line,

Sample microservice using Spring Boot

To be able to test the maven configuration, I created a simple Spring Boot web application (see below).


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

public class Application {    
  @RequestMapping(value = "/hello")   
  public @ResponseBody String helloWorld() {   
    return "Hello, World!";   
  public static void main(String[] args) {   
    SpringApplication.run(Application.class, args);    

This Application class serves the main class of the web application and will be the entry point when running the fatjar from the command line

Deploy to Elastic Beanstalk

If you haven’t signed up for AWS Elastic Beanstalk yet, you can follow this link: https://console.aws.amazon.com/elasticbeanstalk.

Select Amazon Elastic Beanstalk Platform

Select Amazon Elastic Beanstalk Platform

Once inside the console, click on create new environment for application. Then click on Create web server select Java in the platform dropdown and click Launch Now.

Set Environment Variable

Set Environment Variable

AWS configures an nginx proxy server to forward requests to our application running on port 8080. In order to tell nginx about the port 8080, in Configuration -> Software Configuration add an Environment Variable with key and value set to PORT=8080.

And that’s it. If everything went well, you should be able to access the sample web application  on AWS!

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments