• Fat Cats Boardgame
  • Wicket UI Library
  • About Me

Java and Wicket web development thoughts, tutorials, and tips

roman@coderdreams.com
Coder DreamsCoder Dreams
  • Fat Cats Boardgame
  • Wicket UI Library
  • About Me

Deploying Spring Boot app to AWS Beanstalk with Nginx customization

July 23, 2020 Posted by Roman Sery aws, Spring 1 Comment

An application I’m working on uses Spring Boot 2 and produces a fat executable JAR that run with an embedded Tomcat server. I needed to deploy it to an AWS beanstalk environment using Corretto running on Amazon Linux 2. I also needed to customize the config for Nginx, the reverse proxy used by beanstalk.

It turns out this process wasn’t that simple. To hopefully spare some people struggling with this more pain, let’s go through the steps you need to take.

Deployment structure

To make things clearer, let’s first take a look at the end result we want to achieve. To make things work you want to upload a ZIP file that has the following contents:

coderdreams-app.zip
|-- coderdreams-1.0.jar
|-- Procfile
`-- .platform/
    `-- nginx/              
        |-- nginx.conf
        `-- conf.d/
            `-- nginx_custom.conf
  • coderdreams-1.0.jar is the fat JAR produced by running Maven package.
  • Procfile can be a simple command that tells beanstalk which JAR file to execute.
  • nginx_custom.conf – custom config for NGINX. Must follow this exact folder structure.

Prepare our Spring Boot app

In the root of your Spring Boot project create a folder called aws which will contain the following:

~/aws/
|-- Procfile
`-- .platform/
    `-- nginx/                
        |-- nginx.conf
        `-- conf.d/
            `-- nginx_custom.conf

Procfile: (@jarname@ is a placeholder which will be replace with the actual jar file

web: java -jar @jarname@

nginx_custom.conf: (as a simple example)

client_max_body_size 20M;

Maven ant task to the rescue

Now to spare us any manual work each time we build our project, we need to create some Ant tasks to compose the final ZIP file. We will do this in the pom.xml:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <executions>
    <execution>
      <id>prepare</id>
      <phase>package</phase>
      <configuration>
        <tasks>

          <property name="buildName" value="${project.build.finalName}.jar"/>

          <copy todir="${project.build.directory}/aws-build/" overwrite="false">
            <fileset file="${project.build.directory}/${project.build.finalName}.jar"/>
            <fileset dir="./aws" />
          </copy>

          <replace file="${project.build.directory}/aws-build/Procfile" token="@jarname@" value="${buildName}"/>

          <zip compress="false" destfile="${project.build.directory}/aws-build/app-to-deploy.jar" basedir="${project.build.directory}/aws-build"/>

        </tasks>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution>
  </executions>
</plugin>

It seems like a lot but is simple if we break it down into steps:

  1. We store the name of the build JAR file into a variable (Line 11)
  2. We create a new directory called aws-build in our build folder and copy the final JAR file and the /aws folder into it (Lines 13-16)
  3. We replace the token in the Procfile with the value from step 1.
  4. We create the final ZIP file, called app-to-deploy.jar, from the contents of the aws-build folder. (Line 20)

All Done!

Of course the last and final step is to upload app-to-deploy.jar to beanstalk. Enjoy!

1 Comment
Share
3

About Roman Sery

I've been a software developer for over 10 years and still loving Java!

You also might be interested in

How to add a data access layer to your Wicket app using Hibernate and Spring JPA with minimal code

Dec 17, 2019

Learn how to quickly add database communication to your Wicket app using Hibernate and Spring Data JPA while writing almost no code!

Please dont use client-specific release branches

Aug 25, 2020

Why you should avoid a release strategy involving client-specific branches.

Wicket quick tip #2: Print to PDF

Apr 11, 2020

A series of quick tips aimed at improving your productivity. Generating PDF's from Wicket pages.

Categories

  • aws
  • customization
  • database
  • debugging
  • enum
  • java
  • models
  • performance
  • projects
  • react
  • software design
  • Spring
  • tool
  • Uncategorized
  • wicket

Recent Posts

  • Rent Day
  • Self-contained Wicket Fragments
  • Pros and cons of unit testing
  • Themeable React Monopoly board
  • Please dont use client-specific release branches

Recent Comments

  • TCI Express Thanks for sharing such insightful information. TCI Express truly stands out as the best air logistics company, offering fast, secure, and efficient air express and cold chain transportation services....

    Tracking down a bug in production Wicket application ·  March 25, 2025

  • Tom Error: A zip file cannot include itself Can you please correct the plugin part so it doesn't use the same folder as input?

    Deploying Spring Boot app to AWS Beanstalk with Nginx customization ·  September 3, 2021

  • Golfman: Reality always wins I've used both Wicket and front-end JS frameworks and, having worked extensively on both, I can tell you that "Speed of development" is definitely NOT with the JS frameworks. You basically end up...

    Five reasons you should use Apache Wicket ·  August 29, 2021

  • Kiriller Sorry can not agree with you, wicket might be a well built technical framework. But the advantages of using a front-end framework like react.js and vue.js can not be beaten by Wicket nowadays. - Speed...

    Five reasons you should use Apache Wicket ·  August 23, 2021

  • Bernd Lauert Sorry but i have to refute your claims with the following arguments: 1. the Wicket community may be small but it is also very responsive, you usually get a helpful answer from the core devs on the...

    Five reasons you should use Apache Wicket ·  July 1, 2021

Archives

  • May 2021
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019

Contact Me

Send Message
Prev Next