Spring Boot and cloud #Ep.1. Deploying Rest API with RDS to AWS Elastic Beanstalk

Spring Boot and cloud #Ep.1. Deploying  Rest API with RDS to AWS Elastic Beanstalk

Elastic Beanstalk is one of the most straightforward methods for setting up and running your web application on Amazon Web Services. Elastic Beanstalk takes care of the deployment specifics including load balancing, automated scaling, and web application health monitoring automatically. It is an excellent example of Paas (Platform As A Service), and today I'll be your guide on How to deploy a spring boot application and set up a relational database for it. The phrase "platform as a service" (PaaS) refers to the provision of computer platforms, which often contain an operating system, an environment for executing programming languages, a database, a web server, etc.

Now that we have a basic understanding of what the service is all about let's proceed to deploy our application

Login to your AWS Console, search for ElasticBeanstalk in the search box and click on it.

Screenshot from 2022-08-22 19-21-24.png

By the top right corner of the page click on create an application.

Screenshot from 2022-08-22 19-23-02.png

You will be presented with a page that looks like this page below. Type the name of the application , Select the platform to run the application to be Java and click on the "configure more options" to add other configurations.

Screenshot from 2022-08-23 02-52-15.png

When you click on the configure more option you will be taken to a new page, scroll down and look for the database card. Click the edit button to configure a relational database to it. Screenshot from 2022-08-23 02-54-17.png

Set up the database by setting the type of database Engine, for this tutorial I will use MySQL but can choose from the available option. Enter the username you want to use and the password, then scroll down to click save Screenshot from 2022-08-23 03-02-52.png

Finally, click on the "create app" so that Elastic Beanstalk will provision the server environment and deploy a sample java app. This can take up to 15 minutes after which you will be able to see the health of the application environment change from pending to OK if everything goes well.

Screenshot from 2022-08-23 09-08-12.png

When you click on the application environment you should see this screen indicating that the java environment has been set up.

Screenshot from 2022-08-23 08-56-53.png

You can click on the link at the top to view the deployed application

Screenshot from 2022-08-23 09-12-14.png

Screenshot from 2022-08-23 09-14-45.png

Note: you can follow along using this quizApp on Github. https://github.com/SlyCreator/Spring-Quiz-App

Now we have to upload our Springboot Application to deploy. Click on upload and deploy, choose the file and then click on Deploy.

Note that you are to upload the jar file that was built when you package your spring boot application(on your terminal run mvn clean package ), so check your target folder inside your root folder of the spring boot application(AppName->target) and locate the "your-app-name-0.0.1.SNAPSHOT.jar"

Screenshot from 2022-08-23 09-20-06.png

Screenshot from 2022-08-23 14-10-50.png

Elastic Beanstalk will upload the jar file and deploy it as the new version of our application. After a few minutes, you should be able to see the new version of our application that was deployed( If you use the Quiz Github repo you should see the health page.).

Screenshot from 2022-08-23 14-06-12.png

Now you can test your API using postman, simply replace your local address with the elastic Ip that was assigned to our application through beanstalk and you should be able to test your application.

Screenshot from 2022-08-23 14-15-44.png

Fix: If you are getting the Nginx 502 page after deploying your app, ensure you are using the port 5000 in your Application before packaging it or you can define the environment port in Elastic beanstalk. If that doesn't resolve the error you can follow this StackOverflow link.

https://stackoverflow.com/questions/54612962/502-bad-gateway-elastic-beanstalk-spring-boot

Please leave a comment if you have suggestions or any corrections.

Thanks for reading ❤️.

Did you find this article valuable?

Support Sylvester Amaechi by becoming a sponsor. Any amount is appreciated!