How to Deploy Spring Boot Application to AWS Elastic Beanstalk

How to Deploy Spring Boot Application to AWS Elastic Beanstalk
Sharing is Caring:

Dear Reader, Hope you are doing well. In this tutorial, I will help you deploy Spring Boot application to AWS Elastic Beanstalk using AWS console.

Additionally, I will also show you, how you can customize Spring Boot configurations using environment variables(In case you have a DB config or you want to change port).

Little bit of Background

Spring Boot is a java based framework. It allows you to create production grade, stand alone spring based application that you can simply run. As a result, It is a very popular java framework among developers for API development.

With the ever increasing demand of cloud computing, I have seen many people asking this question in various online forums. That’s why ,I decided to come up with an explicit post on how to deploy Spring Boot application to AWS Elastic Beanstalk.

Elastic Beanstalk is a fairly easy to use orchestration service by AWS for deploying and scaling web applications and web services. At the time of writing this tutorial, it supports languages such as Java, .NET, PHP, Node.js, Python, Ruby and Go.

Moreover, Elastic Beanstalk lets you deploy your applications on servers such as Apache, Nginx, Passenger, Tomcat, IIS and Docker.

How do you deploy an application on Elastic Beanstalk?

To be honest, deploying an application on Elastic Beanstalk is very easy. All you need is to upload your code and Elastic Beanstalk does all the heavy lifting for you like-

  • Deployment
  • Capacity provisioning
  • Load balancing
  • Auto-scaling
  • Application health monitoring etc.

In this blog post, I’ll leverage Elastic Beanstalk’s support for Java 8. I will not be using Java with Tomcat because Spring Boot bundles an embedded tomcat server suitable for production workloads.

So let’s get started

Steps to Deploy Spring Boot Application to AWS Elastic Beanstalk

  1. Create a Spring Boot project using Spring Initializr
  2. Import the project into your favorite IDE
  3. Add a RestController to be able to test
  4. Test the application locally
  5. Prepare the final jar file
  6. Login to AWS Management Console and open Elastic Beanstalk
  7. Create an Application
  8. Change the port of application
  9. Test Spring Boot Application Deployed on Elastic Beanstalk
  10. Update your Spring Boot app

Step 1: Create a Spring Boot project using Spring Initializr

If you already have a working Spring Boot application, you can jump straight to Step 5 to start with the deployment. However, If you don’t have one ready, please follow along with me to create a new spring boot application.

let’s go to sprint initilizer page and quickly bootstrap a simple spring boot web application.

Open https://start.spring.io/ and fill up details such as Group, Artifact, Name and dependency.

Deploy a Spring Boot Application to AWS Ec2

Note: Please note that I have added a single dependency Spring Web as you can see in above screenshot.

After filling up details and selecting web dependency , click on Generate

This will download the zipped project into your local system.

Step 2: Import the project into your favorite IDE

Unzip the downloaded project and import the project into your IDE. I will be importing to eclipse for this tutorial.

Click on File -> Import -> Maven -> Existing Maven Project

Hit Next

Click on Browse and navigate to your unzipped project folder and select that. Selecting that should show the pom.xml file as shown in below screenshot.

Deploy a Spring Boot Project to AWS EC2 1

Click on Finish to the project into the IDE.

Step 3: Add a RestController to be able to test

Now, we have the project imported into the IDE. Let’s add a Rest Controller into our spring boot project so that we can test it.

Click on your project and create a controller class with endpoint of /. We will use minimal code like below.

package com.cloudkatha.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

@GetMapping("/")
public String health() {
  return "Hello & Welcome to CloudKatha !!!";
}
}

Important Notes:

Elastic BeanStalk’s load balancer uses path “/” by default for health checks. If you don’t have that path defined in your controller, your application will keep failing health checks and will show Severe as status in dashboard. You can either have “/” endpoint in your rest controller or edit the load balancer setting later to use different path instead

Step 4: Test the application locally

First of all, lets build the project using-

mvn clean install

Once build is successful, let’s run the project locally.

You can right click on the class annotated with @SpringBootApplication and Run as Java Application.

spring boot in local host

Application is running fine locally and I can access it using localhost:8080

Step 5 : Prepare the final jar file

Our application is working perfectly in local environment. Let’s prepare the final jar file and keep it handy in a folder. We will need the jar file during the deployment.

Run maven build to package the application as a fat jar.

mvn clean install

Once your build is successful, you will find the jar file in target folder. Copy the jar file and keep it in your system at a convenient location.

Step 6 : Login to AWS Management Console and open Elastic Beanstalk

To deploy the jar file we just created in previous step into AWS Elastic Beanstalk, the very first step is to login to AWS Management Console and open Elastic Beanstalk service.

You can either go to Services -> Compute -> Elastic Beanstalk

or

Type elastic beanstalk in the search bar. Once you see Elastic Beanstalk option, click on that.

How to Deploy Spring Boot Application to AWS Elastic Beanstalk 1

Step 7: Create an Application

An application is the top-level container in Elastic Beanstalk that contains one or more application environments (for example dev, qa and prod etc).

So, let’s create an application.

Click on Create Application

How to Deploy Spring Boot Application to AWS Elastic Beanstalk 2

Provide an Application name. You can also provide tag if you want but I will skip that as of now to keep it simple.

How to Deploy Spring Boot Application to AWS Elastic Beanstalk Creation

Scroll down and provide below information.

Platform : Java
Platform branch: Corretto 8
Platform version: Best to choose recommended one 

This is how it looks likes after selecting these options for our spring-boot application.

How to Deploy Spring Boot Application to AWS Elastic Beanstalk code selection

Click on Upload your code option

Chhose your jar file from system

Click on Choose file and upload the jar file we created in Step 5

Once the jar is uploaded successfully, Click on Create application as shown in below screenshot.

Create Elastic Beanstalk application final

As soon as you click Create application, Elastic Beanstalk starts setting up your application environment. You get to see the log of what’s happening behind the scene on the dashboard like below.

Environement is being created

It will take few minutes for Elastic Beanstalk to provision resources to set up complete environment. Meanwhile you can try to understand log on what’s really happening(just for fun and learning of course). Once setup is done, you will see below screen.

Important Note:

Elastic Beanstalk needs following two service roles for the orchestration.

  1. aws-elasticbeanstalk-ec2-role with policy AWSElasticBeanstalkWebTier
  2. aws-elasticbeanstalk-service-role with policy AWSElasticBeanstalkEnhancedHealth

Usually, they get created automatically when creating your first Elastic Beanstalk environment with the Management Console or Elastic Beanstalk CLI. But sometimes you may face issue. I am just letting you know that if you face default role creation issue, just go ahead and create these two role with mentioned policy and you should be good to go.

ELB severe heath issue

Resource provisioning is completed. But, application health is Severe at the moment. You can click on the Causes, you will see 5XX errors.

You might think why this?

Well, Elastic beanstalk by defaults provides an EC2 instance that runs Nginx as reverse proxy and listens to port 80. Nginx by default forwards the request to application running on internal port 5000.

Therefore, we need to make sure that our Spring Boot application runs on port 5000.

Step 8: Change the port of application

There are quite a few ways of doing this like-

  1. Changer server.port to 5000 in application.properties file
  2. Use Elastic Beanstalk Environment variable to provide SERVER_PORT

Going with the first option will require to change application code , build and redeploy. So let’s go ahead by 2nd option. Let’s change environment variable to make our spring boot application listen on 5000.

On left hand side project menu, Click on Configuration

spring boot demo configuration

There are various category displayed here. Environment variables belongs to Software category as you can see below. So, let’s click edit Software category.

Edit Software Configuration

Click Edit and scroll down to Environment properties section

Add a property with name SERVER_PORT and value 5000.

Elastic Beanstalk Environment properties

Click Apply

Wait for sometime as you see your environment is getting updated. Behind the scene, application gets restarted and picks up latest environment variables. Once done, you will see application health updated as green. Yahooo 🙂

Let’s go ahead and test our application.

Step 9: Test Spring Boot Application Deployed on Elastic Beanstalk

Application endpoint

Click on the application endpoint(Underlined with red on above screenshot)

Congratulation, Spring Boot app deployed on elastic beanstalk

Congratulations !!!

You have successfully deployed your Spring Boot application to AWS Elastic Beanstalk.

Step 10: Update your Spring Boot app

Now, this was a hello world kind of example. You may want to go ahead and make changes in your app and redeploy your changes.

Right?

You are thinking how to do that?

Isn’t it?

Don’t worry, It’s very simple.

Go back to IDE and make some changes. Prepare the jar and keep handy.

Click on Upload and deploy and upload your jar file. Wait for some time for application to restarted with new jar to be updated. Once updated successfully, hit the endpoint to validate your changes.

Conclusion

In this tutorial to deploy Spring Boot application to AWS Elastic Beanstalk we-

  • Created and tested an Spring Boot application from scratch
  • Created an Elastic Beanstalk Application using our jar file
  • Updated the environment variable for our app to listen on port 5000
  • Tested our deployed Spring Boot application.

I hope you enjoyed the tutorial. Do let me know in comment in case you face any issue.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

I hope you enjoyed this tutorial. If you have a question, feel free to drop in comment. I will be more then happy to answer your query.

Meanwhile you can also –

Suggested Read:

Sharing is Caring:

Leave a Reply

Your email address will not be published. Required fields are marked *