Going to production with Spring & Beanstalk
- Getting an Amazon Elastic Beanstalk account
- Configuring Vaadin for Production
- Configuring a Production Database
- Preparing a Production WAR
- Creating an EB Web Application
- Creating a Database for Production
- Enabling Production Mode
- Post Production
In the following we’ll take a look at how to deploy the Spring version to AWS Elastic Beanstalk (EB).
Beanstalk is part of Amazon Web Services (AWS) and hides a lot of the complexity with provisioning, load balancing, scaling etc, since you do not have to deal with each of these individually. However, since it’s part of AWS, you can still take full control of the individual services, if you have specific needs, or as your application grows.
Getting an Amazon Elastic Beanstalk account
In order to use Amazon Elastic Beanstalk, you need to have a AWS account. Head over to https://aws.amazon.com/elasticbeanstalk to read more about the service, and create an account.
Tip
| You will be asked to provide credit-card details before making use of AWS services, but there is a free tier that allows you to try the service for free. You can also set up a billing alarm to ensure you don’t accidentally exceed the free tier limits (or any other spending budget you might have.) |
We’ll do the following steps
-
Configure the application for production
-
Build a production WAR
-
Create an EB application, upload the WAR
-
Create a stand-alone production database in EB
-
Configure the application to use the production database
Configuring Vaadin for Production
When going to production, you want to make sure your Vaadin application is in production mode by setting the following parameter in application.properties
:
vaadin.servlet.productionMode=true
Configuring a Production Database
To be able to use another database, you first need to add the appropriate JDBC driver as a dependency in pom.xml
, e.g for MySQL:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
Changing the database is covered in more detail in Changing database
When running in EB, the environment provides some variables we can use to configure our database connection in application.properties
:
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://${RDS_HOSTNAME}:${RDS_PORT}/${RDS_DB_NAME}
spring.datasource.username=${RDS_USERNAME}
spring.datasource.password=${RDS_PASSWORD}
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
Note
|
There is a separate application.production.properties with these properties pre-configured. We’ll configure EB to use that instead of the regular application.properties - that way we can have separate production and development properties, and use the embedded database when developing.
|
Preparing a Production WAR
Run mvn install
to build and package your application into a .war
-file (it will be placed in the target
folder).
Creating an EB Web Application
To create a new Elastic Beanstalk application, either click "Build a web app" in the AWS main page, know as the "console". (You can also find Elastic Beanstalk in the menu and "Create New Application" - the steps will be similar, but not identical.)
Next choose a name for your application, select "tomcat", and "upload your code".
At this point, we can click "Create application", and wait for the environment to start (it can take a while).
Once started, we can go to the URL (available in at the top of the page) and verify our application is working.
However, at this point the application is still using the internal database.
Creating a Database for Production
From the "Configuration" screen we can configure a database in the "Data Tier" -section. We’ll "create a new RDS database", choose MySQL, set a master username and password, and go ahead an create the database. This operation can take a while to complete.
Enabling Production Mode
Once the database is up-and-running, we’ll configure our application to use application.production.properties
, which will enable productionMode and take the production database into use. Go to "Software Configuration" on the "Configuration" screen. You want to set the following in "Environment Properties":
spring.config.name=application.production
Note
|
The spring.config.name -property should not have the .properties extension.
|
When you apply the change, your environment will be restarted with the new configuration.
Post Production
Once you have your application up-and-running with a production database, you might wonder how you can see the contents of your database. You can do this with the regular tools you use to connect to MySQL, but by default access is restricted; you need to modify your security realm. AWS provides a handy "My IP" shorthand; after applying, you should be able to connect to the database (note: Elastic Beanstalk uses the database name "ebdb" by default.)
You may also want to review server logs, especially if you run into problems. You can request and download (view) logs easily from the console:
There is a lot more you can do with AWS Elastic Beanstalk and Amazon Web Services in general, and fortunately the official documentation is very good.
Here are some further topics you might want to review: