AWS Elastic Beanstalk is a fully managed service that makes it easy to deploy, run, and scale web applications and services in the AWS Cloud. When deploying Spring Boot applications, AWS Elastic Beanstalk is an excellent choice as it provides a simple and cost-effective way to run and scale Java web applications without worrying about the underlying infrastructure.
The Elastic Beanstalk service is fully integrated with the Spring Boot framework, which allows you to deploy Spring Boot applications with minimal configuration quickly.
Spring Boot Application We Are Going To Deploy
Here, I’ve developed minimal REST API with Spring Boot, Java 17, JPA, and H2 In-memory database. You can clone it from this repository.
This API has a single endpoint where it reads the countries list from the H2 database and is exposed with the following URL or feel free to use any spring boot application you have access to.
Let’s start the deployment of our Spring Boot application on AWS elastic beanstalk and expose that through a public URL.
Using Elastic Beanstalk CLI
In this tutorial, I’m going to use Elastic Beanstalk CLI which allows us to simply access the elastic beanstalk product on AWS and create any kind of environment we like.
Also, there is another way we can use the AWS dashboard to achieve the same target.
You can refer to this official documentation in order to set up EB CLI on your development machine.
Setting Up Application On Elastic Beanstalk
First, we should create a new application and deployment environment on the elastic beanstalk. We can achieve that by executing the following command on the project root.
$ eb init
then choose the preferred AWS region and application name to create the deployment environment.
Here I’ve used us-west-2: US West (Oregon) for demonstration purposes.
After completing this step you should ask to choose the platform, Here it should be Java, and choose the java version which matches your application, For me, it is Java – Corretto 17 running on 64bit Amazon Linux 2.
Finally, there will be asked to set up CodeCommit and set up SSH access to the EC2 instances that this setup will create. But for the moment I’m not going to set up those.
So now we should have elastic beanstalk config.yml created on project root with config as below.
branch-defaults: main: environment: null global: application_name: spring-boot-elastic-beanstalk branch: null default_ec2_keyname: null default_platform: Corretto 17 running on 64bit Amazon Linux 2 default_region: us-west-2 include_git_submodules: true instance_profile: null platform_name: null platform_version: null profile: null repository: null sc: git workspace_type: Application
Configure JAR File For Elastic Beanstalk Deployment
By default, EB CLI uses git history and creates zip file to with the latest application code when deploying. But that will not work with the spring boot application since it should be deployed using JAR file build on application build folders.
We need to show that specific JAR build in order to deploy on the elastic beanstalk. We can achieve that by adding the following into the .elasticbeanstalk/config.yml.
Here I’m using Gradle for application build, it creates the application JAR build on /build/libs/ folder on project root.
deploy: artifact: build/libs/spring-boot-elastic-beanstalk-0.0.1-SNAPSHOT.jar
Setting Up Deployment Environment and Process Initial Deployment
Now, we can deploy our JAR build on the EB environment, with the following command,
$ eb create
choose environment name, DNS name prefix which will used to expose our API with domain name and load balancer type.
Here I’ve used an application load balancer and skipped creating a spot instance fleet.
Here you should see EB CLI started to deploy your JAR built on AWS, and it should be started to upload with above command.
Setting Application Ports To Expose With Public URL
Since we are using the default port with the spring boot application, It runs on port 8080. But elastic beanstalk uses nginx internally and it routes public domain entry into port 5000. Hence we have to set up our application to run with port 5000 with the following command.
$ eb setenv SERVER_PORT=5000
Now our deployment should be done, and we could access our API through the public URL published on the elastic beanstalk dashboard.
Access the API using public URL and /api/v1/countries and it should return following JSON reponse.
Benefits Of Using Elastic Beanstalk
One of the key benefits of using AWS Elastic Beanstalk for Spring Boot applications is that it abstracts away the underlying infrastructure so you don’t have to worry about provisioning and managing servers.
The service automatically provisions and scales the infrastructure to match the number of requests and the amount of traffic your application receives, taking care of all the underlying infrastructure for you. This allows you to focus on developing and deploying your application without worrying about the operational aspect of running it. Additionally, with the simple and cost-effective pricing model, it’s a great option for deploying and running Spring Boot applications on the cloud.
In this article, we have discussed how we can deploy a spring boot application on AWS elastic beanstalk in a practical scenario.
The implementation of all these examples and code snippets can be found in our GitHub repository.
Top comments (1)
Very good article! I did one similar using Quarkus.