Deploy Spring Boot Application On AWS Elastic Beanstalk
Tags: aws, devops, springboot
Published: 2023-03-04 06:04:22

[AWS Elastic Beanstalk](https://aws.amazon.com/elasticbeanstalk/) 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.

## Deployment Flow

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](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3.html) 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.**

![Initializing Elastic Beanstalk Environment to Deploy Spring Boot Application
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ux2p5ik7tksx1j13pye.png)

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.

![Selecting platform and deployment version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qkcuhjdpz90v645f9t4a.png)

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.

![Code commit and SSH access setup elastic beanstalk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8uy3qa4fk48iegji9940.png)

So now we should have elastic beanstalk config.yml created on project root with config as below.

![Project structure after creating EB configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eyfog6geokm90bjfij8n.png)

```
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.

![Elastic Beanstalk Environment Creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/glwhxhgd31dytlhqjrp8.png)

Here you should see EB CLI started to deploy your JAR built on AWS, and it should be started to upload with above command.

![Output on Deploying Spring Boot Application on Elastic Beanstalk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lxgy2mhb1dgxw3fb99to.png)

## 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.

![Environments on Elastic Beanstalk Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7azyq2ri722m1i4g43o.png)

Access the API using public URL and /api/v1/countries and it should return following JSON reponse.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxc08kdmoff0567bnxyw.png)

## 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.

![EC2 Instances created automatically on Elastic Beanstalk Environment Setup.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4g9xtio98bjsz7jz1ml4.png)

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.

![Auto Scaling Groups created automatically](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/46q9xiji4iysy1b43tlu.png)


![Load balancers provisioned on environment creation automatically](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nbd11nztlb1az7fnjcbx.png)

## Conclusions

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](https://github.com/javatodev/amazon-sqs-spring-boot-application) repository.

Happy coding.