Deploying a simple Spring Boot with MySQL app to AWS using Elastic Beanstalk

There is no doubt that AWS is still a hot topic in 2019 so I felt that I should write something useful to get started with exploring it’s capabilities. In this tutorial I’ll show you how to deploy a simple Spring Boot application which uses a MySQL database to AWS using Elastic Beanstalk.

The application

We’ll create a very simplistic web service which exposes some HTTP endpoints to interact with the system. Imagine that we want to store books and retrieve the book information. Obviously there is a need for some storage which could be a simple H2 if Spring Boot is already on the table but as this is a tutorial about a let’s say “full stack” service, we are going to use an external database, namely MySQL. On, create a new project using Web, JPA, MySQL.

For database access I’ll use Spring Data JPA and Hibernate. The model is quite simple, just an id  and a name  for the books:

The repository is a Spring Data JPA repository, nothing special:

The 2 endpoints are the following:

  • GET /books  – lists all the books in the system
  • POST /books  – stores a book
    • The JSON structure which the endpoint will accept is:  { "name": "test-book" }

Okay, I don’t want to overexplain things here. One method for the GET endpoint which uses the BookView  POJO containing the id  and name  attributes and the other method is for the POST endpoint which practically just creates an entity out of the request body.

Now the configuration for the app. I’m using YAML format here because it’s just more convenient for me, src/main/resources/application.yml :

As you can see, I’ve set all the database related configuration to be configurable. I’m using the ${ENV_VAR:[default_value]}  notation where it’s possible to inject values from environment variables to the configuration and I’ve provided some defaults for easier testing locally… because of course we’ll start up the application before deploying to AWS. 🙂

The default credential for the database is root:root and the URL is localhost:3306. The default database the application is connecting to is aws_eb_db.

Bringing up the tool of 2018, Docker. If you have a standalone MySQL setup locally already, you can skip this, but let’s check how to fire up a brand new MySQL server in Docker for testing the application. I’m going to use MySQL 8.0.11 because that’s the newest MySQL server available in AWS at the time of writing this article.

After this, hopefully your container is running properly. Now the database has to be created before starting the application. We’ll use MySQL CLI for this purpose within the container:

This should bring up a bash terminal for you. Let’s login to the server:

Now the new database for our application:

Great, we are all set. Spin up the application using the Boot plugin (I’ve created a Gradle project here):

The app should start up correctly if everything is configured properly. Testing the endpoints is easy, if you are a CLI type of guy, use curl, if not, open your favorite REST client (e.g. Postman).

Cool, the application is ready. Package it using  ./gradlew clean build and the executable JAR can be found under the build/libs/ folder.

Quick AWS rampup

I won’t do a deep dive here about AWS, only an overall view. For a simple application with database access, 2 AWS services can be used for deploying the application. One of them is EC2 (Elastic Compute Cloud) which is straight up just a virtual machine. You can choose between different AMIs (Amazone Machine Image) which translates to – “which operating system image”. It can be an Amazon specific Linux, Ubuntu, Windows, etc. This is where the application package (JAR) will be put and started. Note that the services which we’ll be using in this tutorial are eligible for the free-tier on AWS.

The second service is Amazon RDS (Relational Database Service). As the name suggests, it is a relational database service. There are several database types available, we’ll use MySQL for the purpose of this tutorial but PostgreSQL, Oracle, etc. are also available. The database used by the application will reside here and the application running on the EC2 instance will access the database provided by the RDS service.

It’s not that hard to understand there is an overhead every time you want to create a test bench for a simple service with database access because you have to set up the EC2 instance, the database, the security groups, VPC, optionally load balancing and auto scaling. Amazon came up with a great idea to compose these things together into a new service called Amazon Elastic Beanstalk.

Elastic Beanstalk (EB) is a service to easily deploy an application with the usual needs, EC2 instances where the app will run, database, scaling, load balancing, etc. EB is really just a composition of the already existing Amazon services. Here’s a very simplified picture how the application will look like in an EB environment.

Using Elastic Beanstalk for deployment

First of all, login to AWS, then select the Elastic Beanstalk service under the Compute category. Then create a new application:

After the application has been created, you will have the environments page in front of you. Elastic Beanstalk support multiple environments for a single application. Imagine like you want to have a development environment, a staging environment, a test environment and so on. With EB, you can do that, although it’s not necessarily a good idea to run a production grade application because as any abstraction, EB also comes with a cost. It is very powerful but when you want to customize your application down to the last tiny bit, you’ll find yourself in problems with EB. For production, rather use native EC2 and RDS setup.

Create a new environment and select Web server environment. The only thing we need to configure is the Platform. Select Java there from the dropdown and upload the application JAR which we’ve created. Click on Configure more options next to the Save button.

Now the detailed configuration is available. Start with adding a new database. Click on Modify under the Database category. Select mysql 8.0.11, db.t2.micro instance is enough for now and it’s free. Provide a username and password with which you want to access the database.

Click on Create environment. It takes ~10-15 mins to create everything but after it’s done, the app won’t be accessible yet, more configuration is needed but first we need the running database to set the host (which is auto-generated) for the application.

After a successful environment creation, you should see a similar picture:

The next step is to set up the connection between the app and the RDS. Go to Configuration and check the database details, there is an endpoint by which you can access the database. Copy that link and go to the Software category and click on Modify, that’s where the environment variables can be added.

Add the following values:

  • MYSQL_HOST=[the endpoint of the database without the port]
  • MYSQL_DB=ebdb (this is a fix value for EB databases)
  • MYSQL_USER=[the username you provided]
  • MYSQL_PASSWORD=[the password you provided]

The application should have established the connection to the database. Let’s expose port 8080 on the environment to access the API. The reason why I didn’t use port 80 because the web environment in EB puts an NGINX to the running EC2 instance which already takes port 80. Of course it’s possible to remove that but that would just complicate things.

Open up the EC2 service and select Security Groups (SG).

Select the SG in the list which is bound to the EC2 instance (by default it has a description of SecurityGroup for ElasticBeanstalk environment). In the bottom, select the Inbound tab so we can customize which ports are exposed on the instance. Click on Edit and add a new rule. Make it a Custom TCP Rule and set port 8080. For Source, you can set which translates to anyone can access port 8080 or you can select the My IP from the list so only you can access it.

That was the configuration. Go back to Elastic Beanstalk and select your application and environment. On top of the page, you should have a URL, something like this:

This is the URL where you can access the application so bring up your REST client or terminal and test the app, don’t forget that you need to specify port 8080 in the URL. I’m going to use cURL here.

Getting the list of books works correctly. Let’s try adding one:

Fetching the books again will result in the following response:

That’s it. Easy, right?


Elastic Beanstalk is a great tool to quickly deploy applications into the cloud, only minor configuration is needed for a Spring Boot application with database access. On top of the setup I’ve showed, you can customize the application even more with auto scaling, monitoring, rolling updates, and so much more. Also note that I’ve used the Web UI for doing the deployment but all of this is available through the CLI of EB so you can simply automate this process.

The application code can be found on GitHub. If you liked the article, give it a thumbs up and share it. If you are interested in more, make sure you follow me on Twitter.

Leave a Reply

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