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.
By the top right corner of the page click on create an application.
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.
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.
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
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.
When you click on the application environment you should see this screen indicating that the java environment has been set up.
You can click on the link at the top to view the deployed application
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"
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.).
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.
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 ❤️.