How to Deploy a Spring Boot App to AWS Elastic Beanstalk
A step-by-step guide for creating a AWS account and how to deploy a Spring Boot application to AWS Elastic Beanstalk. Enjoy!
Join the DZone community and get the full member experience.Join For Free
In this post, we are going to explore how we can deploy a simple Spring Boot application to AWS Elastic Beanstalk. We will explain how to setup an AWS account and provide a step-by-step guide how to deploy to AWS.
AWS provides numerous services and in the beginning it is difficult to find out where to get started. An easy way to start experimenting with AWS, is to make use of AWS Elastic Beanstalk. Elastic Beanstalk can be seen as an abstraction layer above core AWS services (like Amazon EC2, Amazon Elastic Container Service (Amazon ECS), Auto Scaling, and Elastic Load Balancing). It provisions and operates the infrastructure and manages the application stack for you, in order for you to focus on writing code.
In the next sections, we will show how to setup an AWS account, create a simple Spring Boot application and provide the steps in order to deploy a Spring Boot application to AWS Elastic Beanstalk.
2. Create a Free Tier AWS Account
Before we can deploy or use any of the AWS services, we will need to create an AWS account. Fortunately, we can create a Free Tier account which will allow us to experiment with several AWS services. We click the Create a Free Account button and fill in our email address, a password and an AWS account name.
Next, we fill in our contact information and choose for the Personal account type.
After clicking the Create Account and Continue button, we fill in our credit card details for the identity check and for charging when we use paid services. Do not be afraid, you will not get charged for anything and we will show later on how to set up notifications when a certain budget has been exceeded. This will give you some extra guarantee in order to limit any costs.
We need to verify our phone number and choose to do so by means of a text message.
After clicking the Send SMS button, a verification code is received which needs to be entered.
After clicking the Verify Code button, our identify has been verified successfully.
In the next section, we need to choose a support plan. We choose the Basic Plan because we just want to experiment with AWS services.
In less than a minute, we receive an email indicating our account being activated. After this point, we can sign in as Root user with the email address and password we have used for creating the account.
After logging in, the Management console is shown.
3. Create IAM User
Using the Root user for your daily access and use of AWS services is not a very good idea. It is therefore advised to follow some best practices. Most of these items are shown when you navigate to the IAM Management Console Dashboard. The Security Status shows which actions need to be performed.
Before we start resolving all of the security issues, we first are going to enable billing information for IAM users. This way, it will be possible to grant IAM users access to the billing information. If you do not do so, you will always need to use the Root user for this and this is what we are trying to avoid.
3.1 Enable Billing Information
Go to your account and choose My Account. Scroll down to the section IAM User and Role Access to Billing Information, click Edit and Activate IAM Access.
3.2 Activate MFA on Your Root Account
Back to the Security Status. First thing to resolve, is to activate Multi Factor Authentication on your Root account. We will do so by means of an Authenticator App on our mobile device.
We choose Google Authenticator as Authenticator App for our Android device, but a complete list can be found here.
After scanning a barcode on the Amazon website and entering twice a MFA code, the authentication is set up correctly.
3.3 Create Admin Account
In this section, we are going to set up an Admin account. This way, we do not need to log in anymore with our Root user. Go to My Account and choose My Security Credentials. In the section Access Management, we choose Users and Add a User. Choose a User name and choose AWS Management Console access as Access type.
Because we do not have a Group created, we need to create an administrators group with AdministrationAccess in the next step.
After this, a step is shown where we can add Tags, but we just skipped this step. Next, we are presented a Review page where we can review the settings of the user. In a final step, instructions are shown in order to instruct the newly created user.
Log out as Root user, navigate to the AWS management console URL as being shown in the instruction page (this will automatically fill in your AWS account Id) and log in as Admin user. At first login, you will need to provide a new password.
3.4 Apply IAM Password Policy
Last thing to do is to Apply an IAM Password policy. After this step, your IAM dashboard will look as follows:
You can now create other groups and other users if you want to.
3.5 Set a Budget
As mentioned earlier, we will set up a notification when a certain amount of money is charged. This can give you some extra comfort when you just want to experiment with AWS services and do not want to be confronted with a high bill. Go to My Account and choose My Billing Dashboard and Budgets. Click the Create a Budget button.
Choose Cost Budget as budget type and click the Set your budget button.
Leave the defaults in this page and only change the budget amount to 1$. Do not forget to set a name also, it is mandatory. The button Configure Alerts will not be enabled before you do so and no indication is given why.
In the Configure Alerts screen, you can set a notification alert. Fill in the email contacts and click Add email contact to add the email addres. When finished, click the Confirm Budget button
After reviewing everything at the Review page, the budget is created. You can create many other budgets if you want to, but for now this will be sufficient.
4. Create a Spring Boot App
Now that we have setup our AWS account, we need to create a simple Spring Boot App. We will create a Spring Web MVC application with a
HelloController which just returns a hello message including the IP address.
We go to Spring Initializr, select the Spring Web dependency, use Spring Boot 2.3.4, Java 11 and generate the project. The
HelloController looks as follows:
Run the application:
And invoke the URL:
The sources of this project can be found at GitHub.
5. Install and Configure EB CLI
Before we can start with deploying our application, we need to install and configure the Elastic Beanstalk CLI tool which will allow us to deploy.
5.1 Install EB CLI
The instructions for installing the EB CLI can be found at GitHub. We describe the instructions for Ubuntu 20.04.
First, we clone the git repository:
Next, run the installer from the directory where you executed the
git clone command:
The build fails. However, paragraph 2.3 Troubleshooting of the installation instructions gives us the answer. Most problems during installation are due to missing libraries. So we execute the following:
Then we run the installer again:
This did the trick, the EB CLI is successfully installed!
Last thing to do is to ensure that the
eb command is available in your path.
5.2 Configure the EB CLI
Next step is to configure the EB CLI. Navigate to your project directory and run the
eb initialisation command:
We choose 17 because that is the area we are residing.
In the next step, we need a Security Access key. Go to the AWS Management Console, click My Account – My Security Credentials. We need to create an access key.
Click the Create access key button. In a popup window your Access key ID and Secret access key are shown. This information will be shown only once, so make sure you store it somewhere in a safe place.
Now fill in the Access Key and Secret and continue:
Choose an application name,we keep the default and choose Java as platform because we created a Spring Boot jar file.
Next, we need to select a platform branch. We are using Java 11 so option 1 is the one we need. Amazon Corretto 11 is a no-cost, multiplatform, production-ready distribution of OpenJDK 11.
Last two questions are answered with no. CodeCommit will store your code in AWS CodeCommit, this will speed up deployments, but for our small example, this is not necessary. The SSH keys are needed when you want to have acces via SSH.
In our repository, a
config.yml file is added to directory
.elasticbeanstalk with the following contents:
The Elastic Beanstalk files are also automatically added to the
The configuration of EB CLI is ready!
6. Deploy to AWS
A few things are left to do before we can deploy the Spring Boot App to Elastic Beanstalk. The Elastic Beanstalk environments run an nginx instance on port 80 to proxy the actual application, running on port 5000. Therefore, we need to set the server port to port 5000 in the
Now build the application which will create the jar file
Next, we need to add the following to the
Finally, we are going to create our AWS environment. Do so with the
-s option, otherwise a loadbalancer is created which will cost extra. The
-s option will create a single instance. Wait a few minutes and the environment will be available.
Lets check whether we can access our application (the URL is taken from the last but one log line):
With the command
eb console the console of your environment can be viewed. Just try it out and navigate the pages. Quite some information about your running application is available.
What if we want to change something and want to redeploy our application? This is quite simple. Let’s make a small change to the hello message and add the word again to the text to be displayed:
Rebuild the application with
mvn clean install and execute the
eb deploy command in order to deploy the application:
curl command again and the updated message is returned:
7. Terminate the environment
At the end, it is wise to remove the environment. This can be done with the
eb terminate command following the environment name. After a few minutes, the environment is gone.
It takes some time in order to setup your AWS account properly but this is something you need to do only once. After this, creating an environment for deploying your Spring Boot application is pretty easy and (re)deployments can be executed by means of single command.
Published at DZone with permission of Gunter Rotsaert, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.