Vaadin 14 on AWS Elastic BeanStalk

Java Apr 14, 2020

This post will guide you how to deploy a Vaadin Flow 14 Application to AWS Elastic BeanStalk. All in all it's very easy, but there are some pitfalls you should now.
First of all you should create your plattform in AWS Beanstalk (Tomcat 8.5 with Java 8 running on 64bit Amazon Linux).

Now you should build your vaadin application to a war file with your favorite buildtool, in my case maven.
My Vaadin Application uses a Connection Pool with a JNDI Resource. This is very trick to get this work because you can not change the context.xml from the tomcat installation, so you can not configure the JNDI-Resource. But there is a litte trick to get this work.

You simply have to add the context.xml in your war file in the META-INF folder.

filename.war/META-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resource name="jdbc/mysql" auth="Container" 
        type="javax.sql.DataSource" 
		driverClassName="org.mariadb.jdbc.Driver"
		url="jdbc:mariadb://database-name.xxxx.eu-central-1.rds.amazonaws.com:3306/dbname"
		factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
		username="user" password="p4ssw0rd"
        maxTotal="20" maxIdle="10" maxWaitMillis="10000"/>
</Context>

In Java you can get a active connection from the pool with:

Context context = new InitialContext();
DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/mysql");
.
.
.
ds.getConnection().close();
Don't forgett to close the connection manually to get the connection back in the pool!!!

With maven you can add the following plugin to add the context.xml file from src/main/resources/META-INF to filename.war/META-INF/context.xml

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <configuration>
    <webResources>
      <resource>
        <directory>
          ${project.basedir}/src/main/resources/META-INF/
        </directory>
        <targetPath>META-INF</targetPath>
	    <includes>
		  <include>context.xml</include>
		</includes>
      </resource>
	</webResources>
  </configuration>
</plugin>

Now you can upload you war file to AWS.

Tags

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.