AWS Beanstalk Java Environment Returns a 502!

AWS Beanstalk Java Environment Returns a 502!
December 9, 2015 Manuel Ortiz

Today’s Mozart Analytics BIT made me lose more than 2 hours and the solution was right in front of my face… as always!. If you have read our posts and blogs you may know by now that we are great fans of developing all our web apps and RESTful services using Grails and deploying them to our favorite cloud: Amazon Webservices (AWS). Grails is a Java-based framework so, in order to deploy our applications, the easiest way to go is by using the grails war command that outputs it inside a WAR file ready to be used in a Java Servlet container such as Tomcat. We use AWS Elastic Beanstalk as our application container and manager of choice. In fact, we are running all of our production services inside AWS Beanstalk containers.

Well, here is the thing! Recently, AWS added a new Java-based environment type that I had not seen before. So I assumed that it was a new name for the previously existing Tomcat environment type which is the one we always used before. I selected it from this dropdown:

AWS Beanstalk Environment Types

and continued with the process. Then, after the environment was created and the app was running green; when I navigated to the app, I got the dreaded 502 http code!

nginx 502

I didn’t know what was happening and started debugging my app in my dev environment. I did some changes to some parts that I thought that were causing this issue and redeployed (first lost hour). 502 again!

So I started googling and found out that sometimes when this error happens it is because the server is low on memory. So I selected a bigger instance and redeployed… 502! Then I found another forum post discussing that the new AWS Beanstalk Java environment uses an nginx reverse proxy and that it connects to the app using port 5000 by default. On the other hand, Grails standalone apps run using the standard 8080 port. So I continued with my research and found how to change the default Grails port to 5000 using an Environment variable. Then, when I started with the configuration changes it hit me! I thought “why would AWS do this and remove the Tomcat environment! it makes no sense at all!”. So I decided to recheck the Environment Type dropdown and there it was! The Tomcat configuration type, I have missed it all over (sometimes I think I am a little bit dislexic)! So here is today’s BIT:

When you get a 502 when navigating to your app deployed in AWS Beanstalk, make sure you are using the correct environment type before doing anything else!!!!

… hope this helped you save some time!!

Loves beach and beer (specially Medalla). Software Engineer and Co-founder of Mozart Analytics, and award winning technology startup Passionate about software engineering with strong knowledge and years of experience in cloud services and architecting for the cloud. Works as the CEO of in San Juan, Puerto Rico. Lives happily with his wife and dog.


Leave a reply

Your email address will not be published. Required fields are marked *