Create a Rest Based application in Spring Boot with Security and deploy that in AWS using Elastic Beanstalk

Raghwendra Sonu
5 min readOct 7, 2019

In this article, i will talk about how to deploy a Spring Boot application on AWS using Elastic beanstalk. AWS comes with a free tier that lets new users experiment with its services for free for 1 year. Every AWS resource that we’ll create in this article is eligible for AWS free tier. So you can follow this step by step guide worry free. So, the first thing will be developing the application.

The application

We have created a simple Spring Boot application with Spring Security. So, on launching the application, user needs to enter credentials and once done, username will be displayed on the welcome page. After this user can pass any name in URL and the same will be displayed on the page.

We need to update pom file and mail class to provide entry point to Tomcat server.

This is how my start-class property in POM file looks like:

<properties>
<start-class>com.QATechTesting.rest.webservices.restfulwebservices.StartWebApplication</start-class>
</properties>

This is how my StartWebApplication class looks like:

@SpringBootApplication
public class StartWebApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(StartWebApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(StartWebApplication.class);
}
}

The endpoint is as the following:

  • GET /{message}— prints the message passed in the URL.

Add server port variable in application.properties file to ensure the use of default port 5000 expected by Beanstalk.

#############application.properties############
server.port=5000

Downloading the application to deploy on AWS

You can download this application directly from Github and then you can modify the code as per your need.

https://github.com/raghwendra-sonu/spring-boot-hello-user-rest-api-project

Using Elastic Beanstalk for deployment

Elastic Beanstalk (EB) is a service to easily deploy an application with the usual needs, EC2 instances where the app will run, database, scaling, load balancing, etc. I won’t do a deep dive here about AWS. First of all, login to AWS, then select the Elastic Beanstalk service under the Compute category. Then create a new application:

After the application has been created, you will have the environments page in front of you. Elastic Beanstalk support multiple environments for a single application. Imagine like you want to have a development environment, a staging environment, a test environment and so on. With EB, you can do that.

Since, i have generated .war files, so, i will select Tomcat from available platform option.

Now, you need to navigate to the folder where .war file is generated and upload that.

Once, done you will see below screen and the uploaded code will be deployed in that environment.

If all goes well, you will see below screen with Health as OK and green tick mark.

At the right side top of the page, beside Actions drop-down, it will specify the URL to access the application we have deployed.

Before launching the URL, we need to make one last change:

Go to the Configuration -> Software -> Modify. Look for Environment properties and add the following value to the list:

SERVER_PORT : 5000

After changing it, click on the Apply button and wait for AWS to apply the changes in the environment. When the message Environment update completed successfully. appears, open the URL again.

We have used user id as “sonu” and password as “admin”. You can use the same to login to the application.

Once done, you can pass any String in the URL, and on UI it will display the same. e.g.

I have also tested this with Postman.

If you want to update code and redeploy, that can be done from here,

If you have problems, you can always check the logs. In the menu, click on Logs -> Request Logs ->Full Logs. This will generate a zip file with all your environment logs

  • Download and extract the file and check application logs.

I hope this was useful.

GIT for the source code:

https://github.com/raghwendra-sonu/spring-boot-hello-user-rest-api-project

Let me know if this was helpful. If you ever need my help, you can write in comments sections. Also, you can contact me through my LinkedIn Profile.

--

--

Raghwendra Sonu

Software Automation Testing expert with 9 years of work experience in diverse tools and technologies.