Open In App

Deploy Java Microservices on AWS Elastic Beanstalk

Improve
Improve
Like Article
Like
Save
Share
Report

AWS Elastic Beanstalk is a fully managed service that simplifies the deployment, operation, and scaling of web applications and services in the AWS Cloud. AWS Elastic Beanstalk is an ideal solution for deploying Spring Boot applications because it provides a straightforward and cost-effective approach to execute and grow Java web apps without worrying about the underlying infrastructure.

In this article, we’ll look at how to build a simple spring boot web application and deploy it to AWS Elastic Beanstalk.

Creating the Spring Boot Application

Step 1: Create a basic spring boot application using the spring-boot-starter-web starter dependency. You can use the Spring Initializer to create the Spring Boot application template and download the application.

 

Step 2: Make a new package called com.example.demo.controller and create a HelloWorldController controller class. We’ve constructed a REST API that outputs 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;
@RestController
public class HelloWorldController {
@GetMapping("/")
public String helloWorld() {
return "hello ";
}
}

Step 3: Add the configuration property shown below to the application.properties file of the spring boot application located in the src/main/resources folder. AWS Elastic Beanstalk anticipates that our app will listen on port 5000. This variable must be set in order for our spring boot application to listen on port 5000.

server.port=5000

Step 4: The spring boot application’s final project structure is shown below.

 

Step 5: We can run the application locally by Right-clicking on the project > Run as > Spring boot app in our IDE. The application runs locally, and we can access our REST API at http://localhost:5000/.
 

 

Deploy Spring Boot App to AWS Elastic Beanstalk

Step 1: We must create the spring boot jar file, which includes the integrated Tomcat server.  We can start the Maven build in STS or Eclipse by Right-clicking on the project and selecting Run as > Maven install.  The maven build tool will generate our application as a jar file in the project’s target/ subdirectory.

 

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

 

Step 3: Click on the Create Application button to create a new application.
 

 

Step 4: Fill up the application name field with a name. In this case, we will call our application my-spring-app.

 

Step 5: Then, under the platform section, select the platform details. We chose Java as the platform since we want to launch a Java application.
Select the Upload your code option beneath the Application code area as well.

 

Step 6: Finally, choose the spring boot application jar file created by the maven build, as shown below.

 

Step 7: When you click the Create application button, the application begins to be deployed into the AWS elastic beanstalk.

 

Step 8: When the application is successfully deployed, an entry appears under Environments, as seen below.

 

Step 9: We can also observe that the application’s health is OK and that the Recent events are displayed on the screen.

 

Step 10: Our application is handled via AWS elastic beanstalk. The application URL produced by the AWS Beanstalk allows us to visit our application. As indicated in the above image, the URL can be discovered on the application instance screen. To access the spring boot application, open the application URL.

 

 



Last Updated : 01 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads