Deploy a Spring Boot Application with AWS

A spring boot application is a Java-based web application that is built using the Spring Boot framework. Spring Boot is a project designed to simplify the development and deployment of production-ready Java applications.

Creating a Spring Boot Project with Spring Initializr: Step-by-Step Guide

Note : If you already have a spring boot application ,you can skip these steps.

Step 1 : Go to spring initializr to quickly bootstrap a simple spring boot web application.

Spring Initializr

Please note that I have included only one dependency, which is Spring Web, as you can see from the screenshot above.After filling all details and selecting web dependency, click on Generate or ctrl + Enter .The zipped project will be downloaded in your system.

Step 2 : Unzip the downloaded project and import it in your IDE. I’ll use VS Code for it.

Environment

Step 3: Adding a RestController to test our project.

Go to src > main > com > example > demo

src > main > com > example > demo

Note: You may have different names after com . If different project and package names are specified .

Now create a new file named TestController.java beside DemoApplication.java file.

Demo application

We will create a basic controller with minimal code and endpoint to “/.”

Java

package com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController < @GetMapping ( "/" ) public String health() return "Hello Geeks And Welcome to GeekForGeeks . " ;

Note : The load balancer for Elastic Beanstalk by default utilizes path “/” for health checks. Your application will keep failing health checks and display Severe status in the dashboard if that path is not defined in your controller. Either include a “/” endpoint in your rest controller code, or later change the load balancer setting to utilize an alternate path.

Sample code

Step 4: Test the Application locally .Just click on Run Java Button on top right on the VS Code.Now , go to browser and open localhost:8080 . It will show like

Output

So, our application is running properly.

Step 5: Prepare the final Jar file

Open the terminal and type (maven) mvn package to build a jar file

Build the Package

After successful execution it will show like this

Build Success

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

How to deploy our Spring Boot Application on AWS?

There are multiple ways to deploy Spring Boot application on AWS. In this example, we will deploy a Spring Boot application on AWS Elastic Beanstalk, which is a P latform as a Service (PaaS) offering that simplifies the deployment and scaling of web applications. Elastic Beanstalk supports Java applications, making it a suitable choice for Spring Boot.

Step 1: Open AWS Console and Create and Elastic BeanStalk Application

An application is a top-level container in Elastic Beanstalk that contains one or more application environments .So, let’s create an application.Click on Create Application

Create Application

Provide Application name and Application Tags(Optional).

Environment tier

Step 2: Scroll Down and Provide the below information :

Platform type

Step 3: In Application Code , Select upload your Code :Give a Version Label, and select Local File and select the jar file saved earlier.

Application code

and click on NEXT .

Step 4: Configure Service Access.Select existing service role as aws-elasticbeanstalk-service-role

Service Access

Select your EC2 key pair and EC2 instance profile to securely log in to your EC2 instances.

EC2 key pair

Now Click on Skip to Review ,

Step 5: Adding Environment Variable

Make Sure to add SERVER_PORT and JAVA_HOME values as

SERVER_PORT 5000

and java_home variable as,

JAVA_HOME /ur/lib/jav/java

after setting the environment variables , it will look like

Environment properties

Now Click On Submit.

Step 6: As soon as you click Submit . Elastic Beanstalk starts setting up your application environment. It will take few minutes , After completion the dashboard will look like this

Environment Overview

Upon clicking on the link under domain ,we will be directed to our deployed app,

Output

Our Spring Boot Application is Deployed on AWS Elastic Beanstalk successfully.

FAQs On The Deploy a Spring Boot Application with AWS

1. What Is Spring Boot, And Why Should I Use It For My Web Application?

Spring Boot is a Java framework that makes it easier to build and deploy web applications. Use it to simplify development and save time on configuration tasks.

2. How Do I Deploy My Spring Boot Application On AWS Elastic Beanstalk?

To deploy on AWS Elastic Beanstalk, create an app, upload your code, configure settings, and Elastic Beanstalk handles the rest. Follow the provided steps in the article.

3. What Are The Benefits Of Using AWS Elastic Beanstalk For Hosting My Spring Boot App?

Elastic Beanstalk simplifies hosting by handling infrastructure, scaling, and more. It’s easy to use, and AWS services integrate well for added functionality and scalability.