Friday, February 18, 2022

Starting AWS Elastic Beanstalk : Deploy Spring Boot (REST)

Just as I mentioned in my earlier article - I am a Hard-Core Java/Java EE/Microservices Architect with an overall experience of ~17y. It also includes a book published on Microservices Architecture, with 3 more in the pipeline. Consider to buy my Book on Microservices Architecture : The Decision Maker from Notion Press, Flipkart or Amazon.

For the last about ~4y, I have been working on projects that used the Amazon Web Services (AWS) in some form or the other. But always my involvement was as a Decision Maker on AWS (Discussions/Decisions with DevOps Team on things like ECR, ECS, EKS, K8s on EC2, ...). For the last about 2 weeks, I have been working in this amazing world of AWS Step Functions, AWS API Gateway and AWS Elastic Beanstalk. This time as a Hands-On PoC Developer, not just as a Decision Maker. Absolutely amazed by the power that AWS brings to every aspect of Enterprise Software Development. Elegant Problem Solving with very few errors, bugs, surprises and a large community providing support and sharing their experience.


In this article, we will understand how to move your Spring Boot/REST service and deploy it publicly on the AWS Elastic Beanstalk. In case you have very specific requirements to deploy Java/Java EE on Tomcat, as opposed to a more enterprise virtual server - It is better to go for AWS Elastic Beanstalk. Internally, Elastic Beanstalk may actually be using/provisioning an EC2 instance.

From the Official AWS Documentation.

"AWS Elastic Beanstalk is an easy-to-use service for deploying and scaling web applications and services developed with Java, .NET, PHP, Node.js, Python, Ruby, Go, and Docker on familiar servers such as Apache, Nginx, Passenger, and IIS.

You can simply upload your code and Elastic Beanstalk automatically handles the deployment, from capacity provisioning, load balancing, auto-scaling to application health monitoring. At the same time, you retain full control over the AWS resources powering your application and can access the underlying resources at any time.

There is no additional charge for Elastic Beanstalk - You pay only for the AWS resources needed to store and run your applications."

Step-By-Step AWS Elastic Beanstalk (Spring Boot/REST)

01. Create your Spring Boot Service using your Favorite IDE like IntelliJ IDEA or Eclipse. Attached Here is the Eclipse Project for the Example Used Here - Buddhahood (Scope is to Generate Random Quotes from The Buddha - Right Now a Single Quote is being Returned as a String).

02. Will be good if you can test out your Spring Boot application locally to check if it compiles and runs fine. It is recommended that you test it using port 5000. Usually 8xxx port series is used for web applications, but 5000 is what is expected by Elastic Beanstalk.  Even though, this may be required to be done again, as the server port  

03. Keep your Binary (.jar) Ready for Deployment on AWS Elastic Beanstalk. Try to give a friendly or meaningful name, even though it is not necessary.

04. So, Login into your AWS Console and Access 'ElasticBeanStalk'.



05. The Next Screen will be the Listing of All Environment that shows all Current Deployed Instances on Elastic BeanStalk.



06. Click on the [Create a New Environment] - Select Web Server Environment on the Environment Tier.

 


07. In the next screen, give the name of your choice for the Application. Also, provide a related Environment Name. You may optionally provide a [Description]. 


08. Select Managed Platform - You may choose either Java 8 or Java 11 as per your comfort. Keep it in sync with your Spring Boot/REST Application. 



09. Now, upload your Java/Spring Boot/REST application binary using 'Upload your Code'.  Optionally, you may edit the version label. Next, Click on Create Environment. 



10. You can now observe that AWS Elastic BeanStalk is creating and configuring your new Instance. 



11. Observe the deployed service along with a public url that is available now to access this service. Yay!



12. With Elastic Beanstalk the allowed port for the access of services is 5000. We are yet to configure this. So, If I try to access the deployed service now, We will get at 502/5xx Bad Gateway Error.  


13. So, Let us go ahead and configure the Port 5000. Cannot wait to get the Application Up and Running. Can we? On the menu at the left, Click on configuration under 'buddhahood-env' ('<applicationname-env>')


14. In this screen, Click on [Edit] in the [Software] category.

.

15. Next, type in the environment property name and the value. In this case, SERVER_PORT and 5000, respectively. 



16. Click on [Apply] changes. We'll be back to the service deployment screen.



17. Observe that the service is now deployed. The first step in you becoming the AWS Ecosystem and AWS Elastic BeanStalk guru. :-)



18. Finally, the Spring Boot/REST service is deployed in AWS Elastic BeanStalk. Now, there are no errors and we are able to get the output from our service. In this case, a wise saying of The BuddhaFor sometime, this public link may work: http://skpbuddhahood-env.eba-cgwdy5jp.us-east-1.elasticbeanstalk.com/buddhahood/quotesElse, you may try at https://rebrand.ly/skp-buddha-hood  

The output shown below is from the example explained step-by-step in this example. The Spring Boot/REST Code that was used in this example, can be downloaded from this Google Drive Link


Even after working on and exploiting the extraordinary capabilities of AWS - I would like to say that Raw/Native Java/Java EE/Microservices will be very my heart is - Just coz of the flexibility that enterprises will want to retain to move across clouds. Also, simply because of the Passion of Java/Java EE, Programming, Computer Science and Software Architecture. :-) ;-)  All said, AWS is AWeSome

1 comment:

Ashutosh Kumar said...

Very Nice artical with Proper explaination for Beginers.