Enterprise Java

Deploy Spring Boot Application on Amazon Elastic Beanstalk

In this blog, we are going to see how to deploy a Spring boot application on Amazon ElasticBeanstalk.

Amazon ElasticBeanstalk has a preconfigured Java environment, which could be used to deploy a Spring Boot JAR which has the servlet container inside it.

For our example, we will use maven as the build tool here.

Step 1 : The first step would be package the application as JAR to be deployed.
– In pom , we have to make sure that the “packaging” attribute is set to “jar”

<groupId>com.anirudh</groupId>
    <artifactId>practice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Practice</name>

– Now, run the maven command to package the application and produce a JAR.

Anirudhs-MacBook-Pro:~ anirudh$ mvn clean package

This will package a JAR into the target folder, something like this :

practice-1.0-SNAPSHOT

Now, we need to create an environment in ElasticBeanstalk.

Step 2:

Sign into your AWS account and go to Elastic Beanstalk. Create a new environment, give name for your application, select appropriate instance type. If you are just testing the this, make sure you select t2.micro instance, as it is under Free Tier.

Once the Java environment is created, you need to upload the “JAR” created to the Elastic Beanstalk, if you are using Web Interface you can directly select the file and upload, else if you are using CLI, you can push it to ElasticBeanstalk using its CLI module. Once you upload it will take few minutes to deploy your application.

The application would not be available right now as it uses Nginx reverse proxy, which by default talks to port 5000. So if you try and hit your application now, you will get a 502 error. To fix that you need to define the property “PORT”

Step 3: Define the “PORT”

For the application to work properly you need to add a tag with the name : “PORT” and value most of the cases, if you are using all defaults, it would be 8080.

Step 4: Open the port 8080

Now we need to open the port 8080 for the inbound traffic for the EC2 instance. Go to the EC2 instance and find the security group which is been used for the instance. Edit the Secuirty Groups, add inbound Rules, and add the following rule :

screen-shot-2016-08-21-at-11-43-34-pm

Now your port 8080 is open to accept inbound connections. Now if you click on the link of your application, you should be able to access your application, if not try adding port 8080 to the URL (in case you are not using ELB)

Optional Step 5: Add Database

If your application is using a DB, add an RDS instance to your ElasticBeanstalk application and launch it.
Note down the access URL, username, password, etc. it would be something like

Endpoint: aaxxxxxxj.xxxxxxxmcn.ap-south-1.rds.amazonaws.com:3306

Go to your application.properties file and put all these 4 parameters : ( this example uses MySQL)

spring.datasource.url = jdbc:mysql://aaxxxxxxj.xxxxxxxmcn.ap-south-1.rds.amazonaws.com:3306/practice
spring.datasource.username = xxxxx
spring.datasource.password = xxxxxx
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

Now, again package your JAR, and upload it, if you have set “spring.jpa.hibernate.ddl-auto” to create/update. You would be able to create the DB tables, provided you have a schema already made. You can use schema.sql to create the schema in Spring boot.

Hope, this helps getting a spring boot application, deployed to Amazon Elastic bean stalk.

Anirudh Bhatnagar

Anirudh is a Java programmer with extensive experience in building Java/J2EE applications. He has always been fascinated by the new technologies and emerging trends in software development. He has been involved in propagating these changes and new technologies in his projects. He is an avid blogger and agile enthusiast who believes in writing clean and well tested code.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button