Deploy Spring Boot App On 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.

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

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

Table of Contents

Creating the Spring boot application

Create a simple spring boot application with the spring-boot-starter-web starter dependency. To learn how to create a spring boot application using STS IDE, please go through this article.

We can also 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 two REST endpoints. The first endpoint prints a “Hello” message and, the second endpoint receives the name as a path parameter and uses the passed path parameter to print the 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("/{name}")
    public String hello(@PathVariable("name") String name) {
        return "hello " + name;
    }

    @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.

spring boot aws elatic beanstalk example

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

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

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

spring boot aws elatic beanstalk example
spring boot aws elatic beanstalk example

Deploy Spring boot app on 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.

spring boot aws elatic beanstalk example

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

spring boot aws elatic beanstalk example

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

spring boot aws elatic beanstalk example

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

spring boot aws elatic beanstalk example

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.

spring boot aws elatic beanstalk example

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

spring boot aws elatic beanstalk example

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

spring boot aws elatic beanstalk example

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

spring boot aws elatic beanstalk example

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

spring boot aws elatic beanstalk example

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.

spring boot aws elatic beanstalk example

The application returns a customized greeting message on our second endpoint.

spring boot aws elatic beanstalk example

Deleting the application

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

spring boot aws elatic beanstalk example

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

spring boot aws elatic beanstalk example

Conclusion

In this article, we learned how to create a simple spring boot web application.

We also learned how to test the application locally and generate the application jar file.

Finally, we learned how to deploy the spring boot jar file to the AWS Elastic Beanstalk.

Deploy Spring Boot App On AWS Elastic Beanstalk
Scroll to top

Discover more from ASB Notebook

Subscribe now to keep reading and get access to the full archive.

Continue reading