The first step is to let my Spring Boot application listen for port 8888 instead of 5000. To do this, simply change the following line in the ‘application.yml’ file:

...
server:
port: 8888
...

When we now deploy the application with the Elastic Beanstalk, it won’t work anymore and we will see the 502 Bad Gateway again.

To modify the ports of the Elastic Load Balancer (and the corresponding Security Groups), I add the folder ‘.ebextensions’ to my Spring Boot project. In this folder, I put configuration files that will be used by the Elastic Beanstalk to configure the resources it creates. Because I deploy my application as a jar file and use Maven to create it, I add the folder to the ‘src/main/resources’ folder so it ends up in the root of the jar file.

As you can see, I add the inbound port 8888 to the ‘default’ security group with the allowed source of the traffic being the Elastic Load Balancer. That way, port 8888 is only available from the Elastic Load Balancer. The next file will configure the Elastic Load Balancer so it forwards the incoming traffic on port 80 to 8888 on our EC2 instance: