Deploy Spring Boot WAR file on Tomcat in AWS | Elastic Beanstalk

In this tutorial, we will learn how to deploy the Spring boot WAR file on tomcat in AWS using the Elastic beanstalk service.

I have explained this tutorial in detail in my YouTube video so refer to my below YouTube video to implement this tutorial step by step.

We basically create a Spring boot project,  package it as a WAR file, and deploy it in tomcat server on AWS cloud using Elastic beanstalk service.

What is AWS Elastic Beanstalk?

Amazon Web Services (AWS) comprises over one hundred services, each of which exposes an area of functionality. While the variety of services offers flexibility for how you want to manage your AWS infrastructure, it can be challenging to figure out which services to use and how to provision them.

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.

The following diagram illustrates the workflow of Elastic Beanstalk.
Elastic Beanstalk flow

Development Process:

1. Create Spring Boot Application

2. Create Spring MVC Controller and Thymeleaf Page

3. Configure Spring boot as WAR

4. Package the WAR file using Maven

5. Create a new application in Elastic Beanstalk

6. Upload the WAR file to Elastic Beanstalk

#aws #springboot #javaguides

1. Create Spring Boot Application

1. Open Eclipse STS IDE
2. New -> Spring Starter Project
3. Specify project details
                 Project type: Maven
                 Packaging: WAR // important
                 Project name: springboot-web-app-tomcat
                 Select below dependencies: 
                 - spring-boot-starter-thymeleaf
                 - spring-boot-starter-web
                 - spring-boot-starter-tomcat
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.3</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>net.javaguides</groupId>
	<artifactId>springboot-web-app-tomcat</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>springboot-web-app-tomcat</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-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

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

	<build>
	<finalName>springboot-web-app-tomcat</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

2. Create Spring MVC Controller and Thymeleaf Page

Let's create a welcome.html Thymeleaf page under resources/templates folder and add the following content to it:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h1 th:text = "${message}"></h1>
</body>
</html>

Let's create a WelcomeController.java  Spring MVC controller to return above Thymeleaf page:

package net.javaguides.springboot;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class WelcomeController {
	
	@GetMapping("/welcome")
	public String welcome(Model model) {
		model.addAttribute("message", "Deploying Spring Boot WAR file on tomcat in AWS");
		return "welcome";
	}

}

3. Configure Spring boot as WAR

When you choose packaging as WAR while Spring boot project creation using spring initialize, the Spring boot automatically configure your spring boot project to make a WAR file. 

4. Package the WAR file using Maven

mvn clean install

5. 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-web-app-tomcat
    Platform: Tomcat
    Platform branch: keep default selected
    Platform version: keep default selected

How to change the post?

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 the Spring Boot application listens on 8080 in the application.properties file.

server.port=5000

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

6. Upload the WAR file to Elastic Beanstalk

- Click on Upload file
- Select WAR 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.

Comments