Deploy Spring Boot App To AWS Elastic Beanstalk

Thameem Ansari
Javarevisited
Published in
5 min readMar 7, 2022
Spring Boot App To AWS Elastic Beanstalk

Introduction

In this article, we learn how to create a simple spring boot web application and deploy it on the AWS Elastic Beanstalk.

Deploying a Spring boot application on AWS Elastic Beanstalk is a simple process.

We can use the AWS Elastic Beanstalk service to deploy web applications and services and scale them.

Prerequisites: We need to have an AWS account to use AWS services.

Creating the Spring boot application

Create a simple spring boot application with the spring-boot-starter-web starter dependency. We can use the Spring Initializer to create the spring boot application template and download the application.

Adding a Rest controller

Create a new package com.example.demo.controller and create a HelloWorldController controller class.

We have created a REST endpoint which prints a “Hello” message

package com.example.demo.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloWorldController {@GetMapping("/")public String helloWorld() {return "hello ";}}

Update the server port

Also, add the below configuration property to the application.properties file of the spring boot application under the src/main/resources folder.

server.port=5000

AWS Elastic Beanstalk expects our app to listen on port 5000.

We need to set this property to configure our spring boot application to listen on port number 5000.

Below is the final project structure of the spring boot application.

Project Structure

Now the application is ready. We can test it locally by running it.

Testing locally

We can start the application locally by Right-click on the project > Run as > Spring boot app using our IDE.

The application starts locally and, we can access our REST APIs at http://localhost:5000/

Local testing

Deploy Spring boot app to AWS Elastic Beanstalk

We need to build the spring boot jar file that contains the embedded tomcat server bundled into it.

Using STS or Eclipse IDE, we can run the maven build by Right-click on project > Run as > Maven install.

The maven build tool will build our application as a jar file inside the target/ folder of the project.

To deploy our application, we have to navigate on the Elastic Beanstalk service, as shown below.

Navigate AWS Services

Then, on the Amazon elastic Beanstalk home page, click on the Create Application button to create a new application.

Create an application

Enter a name on the application name field. Here, we will name our application as my-spring-app.

Create application name

Then select the platform details under the platform section. We have selected the platform to Java as we are going to deploy a java application.

Also under the Application code section, select the Upload your code option.

Upload the code

Finally, select the spring boot application jar file generated from the maven build, as shown below.

Choose and upload the application JAR

Click on the Create application button, and the application starts to get deployed into the AWS elastic beanstalk.

Create the application

Once the application deployment is successful, we can see an entry under Environments, as shown below.

Application listed with environment

We can also see that the application health is Ok, and the Recent events on the screen.

Application health check

Testing the application

The AWS elastic beanstalk handles our application. We can access our application from the application URL generated by the AWS beanstalk.

The URL can be found on the application instance screen, as shown in the previous image.

Open the application URL to access the spring boot application.

Test the endpoint

Deleting the application

We can delete the application instance by simply selecting the Delete application option under the Actions menu.

Delete the application

It may take some time for AWS to complete the delete operation.

Deleted successfully

--

--

Thameem Ansari
Javarevisited

Technology Expert| Coder| Sharing Experience| Spring | Java | Docker | K8s| DevOps| https://reachansari.com