Deploy a Spring Boot Application on AWS | Elastic Beanstalk

In this tutorial, we will learn step by step how to create and deploy a spring boot application on AWS using Elastic beanstalk.

The YouTube video version of this tutorial available at Deploy a Spring Boot Application on AWS | Elastic Beanstalk

With Elastic Beanstalk, you can quickly deploy and manage applications in the AWS Cloud without having to learn about the infrastructure that runs those applications. Elastic Beanstalk reduces management complexity without restricting choice or control. You simply upload your application, and Elastic Beanstalk automatically handles the details of capacity provisioning, load balancing, scaling, and application health monitoring.

To understand more about this tutorial, I have created a YouTube video on the same topic so I highly recommend you to watch the YouTube video. (YouTube video present at end of this tutorial)

Development process:

1. Create Spring Boot Application
2. Build a simple REST API
3. Package the JAR file using Maven
4. Create a new application in Elastic Beanstalk
5. Upload the JAR file to Elastic Beanstalk

Let's start implementing this tutorial step by step.

1. Create Spring Boot Application

1. Open Eclipse STS IDE
2. New -> Spring Starter Project
3. Specify project details
                 Project name: springboot-aws-example
                 Select dependency: Web
Here is pom.xml file for your reference:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.2</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>net.javaguides</groupId>
	<artifactId>springboot-aws-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-aws-example</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<finalName>springboot-aws-example</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

2. Build a simple REST API

Let's create WelcomeController to build a simple REST API:
package net.javaguides.springboot;

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

@RestController
public class WelcomeController {
	
	@GetMapping("/welcome")
	public String welcome() {
		return "Deploying Spring boot rest api application on AWS using Elastic beanstalk";
	}

}

3. Package the JAR file using Maven

Use below maven command to package this spring boot project as a JAR file:
mvn clean install

4. Create a new application in Elastic Beanstalk

1. Sign in to the AWS Management Console
2. Go to Elastic beanstalk service
3. Create a new application in Elastic beanstalk
    Application name: springboot-aws-example
    Platform: Java
    Platform branch: keep default selected
    Platform version: keep default selected

How to configure port?

By default, Spring Boot applications will listen on port 8080. Elastic Beanstalk assumes that the application will listen on port 5000. There are two ways to fix this discrepancy: change the port Elastic Beanstalk is configured to use, or change the port the Spring Boot application listens on. 

For this post, we will change the port in the Elastic beanstalk service configuration ( This is very easy to specify the SERVER_PORT environment variable in the Elastic Beanstalk environment and set the value to 5000).

Watch below YouTube video below to understand more about this step.

5. Upload the JAR file to Elastic Beanstalk

- Click on Upload file
- Select JAR file from local File system
- Click on Create Application

Watch below YouTube video below to understand more about this step.

YouTube Video

This tutorial explained step by step in below YouTube video:

#AWS #javaguides #springboot

Comments