In this tutorial, we’ll show how to deploy an application from our Bootstrap a Simple Application using Spring Boot tutorial to AWS Elastic Beanstalk.
As part of this we’ll:
- Install and configure AWS CLI tools
- Create a Beanstalk project and MySQL deployment
- Configure the application for MySQL in AWS RDS
- Deploy, test, and scale the application
2. AWS Elastic Beanstalk Configuration
As a pre-requisite, we should have registered ourselves on AWS and created a Java 8 environment on Elastic Beanstalk. We also need to install the AWS CLI which will allow us to connect to our environment.
So, given that, we need to log in and initialize our application:
Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : EU (Ireland)
5) eu-central-1 : EU (Frankfurt)
6) ap-south-1 : Asia Pacific (Mumbai)
7) ap-southeast-1 : Asia Pacific (Singapore)
8) ap-southeast-2 : Asia Pacific (Sydney)
9) ap-northeast-1 : Asia Pacific (Tokyo)
10) ap-northeast-2 : Asia Pacific (Seoul)
11) sa-east-1 : South America (Sao Paulo)
12) cn-north-1 : China (Beijing)
13) cn-northwest-1 : China (Ningxia)
14) us-east-2 : US East (Ohio)
15) ca-central-1 : Canada (Central)
16) eu-west-2 : EU (London)
17) eu-west-3 : EU (Paris)
18) eu-north-1 : EU (Stockholm)
(default is 3):
As shown above, we are prompted to select a region.
Finally, we can select the application:
Select an application to use
2) [ Create new Application ]
(default is 2):
At this time, the CLI will create a file named .elasticbeanstalk/config.yml. This file will retain the defaults for the project.
Now, we can create the database on the AWS Web Console or with the CLI using:
eb create --single --database
We’ll need to follow the instructions to provide a username and password.
With our database created, let’s configure now the RDS credentials for our application. We’ll do so in a Spring profile named beanstalk by creating src/main/resources/application-beanstalk.properties in our application:
Spring will search for the property named rds.hostname as an environmental variable called RDS_HOSTNAME. The same logic will apply to the rest.
Now, we’ll add a Beanstalk–specific Maven profile to pom.xml:
Next, we’ll specify the artifact into the Elastic Beanstalk configuration file .elasticbeanstalk/config.yml:
And finally, we’ll include two environmental variables into Elastic Beanstalk. The first one will specify the active Spring profiles, and the second one will ensure the use of the default port 5000 expected by Beanstalk:
eb setenv SPRING_PROFILES_ACTIVE=beanstalk,mysql
eb setenv SERVER_PORT=5000
5. Deployment and Testing
Now we are ready to build and deploy:
mvn clean package spring-boot:repackage
Next, we’ll check the status and determine the DNS name of the deployed application:
And our output should be something like:
Environment details for: BaeldungDemo-env
Application name: baeldung-demo
Deployed Version: app-181216_154233
Environment ID: e-42mypzuc2x
Platform: arn:aws:elasticbeanstalk:us-east-2::platform/Java 8 running on 64bit Amazon Linux/2.7.7
Updated: 2018-12-16 13:43:22.294000+00:00
We can now test the application – notice the use of the CNAME field as DNS to complete the URL.
Let’s add a book to our library now:
http POST http://baeldungdemo-env.uv3tr7qfy9.us-east-2.elasticbeanstalk.com/api/books title="The Player of Games" author="Iain M. Banks"
And, if all is well, we should get something like:
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Date: Wed, 19 Dec 2018 15:36:31 GMT
X-XSS-Protection: 1; mode=block
"author": "Iain M. Banks",
"title": "The Player of Games"
6. Scaling the Application
Lastly, we scale the deployment to run two instances:
eb scale 2
Beanstalk will now run 2 instances of the application and load balance traffic across both instances.
Automatic scaling for production is a bit more involved, so we’ll leave that for another day.
In this tutorial, we:
- Installed and configured the AWS Beanstalk CLI and configured an online environment
- Deployed a MySQL service and configured the database connection properties
- Built and deployed our configured Spring Boot application, and
- Tested and scaled the application
For more details, check out the Beanstalk Java documentation.
As always, the complete source code of our examples is here, over on GitHub.