Grails on AWS Elastic Beanstalk

Posted by {"display_name"=>"greg", "login"=>"greg", "email"=>"greg@udon.org", "url"=>""} on July 24, 2013 · 8 mins read

Here are the steps I took to deploy a Grails app on AWS Elastic Beanstalk for the first time using the AWS Free Tier using Grails 2.2.3. I'm using Bobby Warner's post from October 2011 as a guide to setting up the AWS services. Some field locations and the order of the setup has changed since Bobby did this in 2011.

Create Free AWS Account

Follow this link and sign-up for the free tier which gives you 12 months of free use from the time you sign-up. Details on the terms of the offer can be found here.

Create a Key Pair for SSH Access

Before launching into creating our AWS instance, it will save several steps to create a Key Pair that we'll use for SSH access.

- Go to the EC2 Console
- Look in the upper right hand corner of the window and make sure you are in the desired region
- Click on Key Pair
- Click on the Create Key Pair button - Enter a Key Pair Name to be used for the SSH access We'll use the Key Pair Name in a few minutes when we create the new instance.

Setup Elastic Beanstalk & MySQL

Let's create the AWS Elastic Beanstalk service to host our Grails application:

- Go to the Elastic Beanstalk console
- Look in the upper right hand corner of the window and make sure you are in the desired region
- Select the Create a New Application button
- Enter the Application name and click Create
- Select Tomcat as the platform and Single instance for the environment type then click the Continue button
- You can choose the Sample application for now because we'll need to modify our Grails application before uploading a war.
- Choose and environment name and URL and click Continue
- Under Additional Resources, click the Create an RDS DB if you want to create a MySQL instance
- Under Configuration Details, I chose the m1.small instance because the t1.micro does not have enough memory to support a Grails application. This page has a list of the resources provided with each instance type.
- Select the key pair you created in the earlier step to get ssh access to your server and click Continue
- For the RDS Configuration, I chose the instance class db.t1.micro and the minimum 5GB of storage.
- You'll be presented with a screen to review the configuration and click Create to start the process You'll be left at the Elastic Beanstalk dashboard for your newly created instance and will receive status updates as the instance components are created. It took 15 minutes to complete the setup but I was able to perform the steps below while AWS was setting things up.

Instance Java Memory Settings

We'll need to modify the Java memory settings to support our Grails app.

- Go to the Elastic Beanstalk consoled
- Select your application and then click on Configuration in the navigation pane
- Click the Settings icon in the Software Configuration section and you'll see the dialog box for the Java container memory settings
- I set the Initial JVM heap to 1024m; Maximum JVM heap to 1024m; JVM PermGen space to 768m then click Save Go back to the Elastic Beanstalk console for your app and choose ActionsRestart App Server(s) to restart your app.

Modify Grails App for AWS

Bobby Warner made three changes so that his Grails app would run on AWS in production mode - (1) Production datasource; (2) Server URL; (3) MySQL dependency. I'm going to skip his use of a separate JDBC connection string and embed it in the DataSource.groovy to make this simpler

(1) Production Datasource

Change the production entry in your grails-app/conf/DataSource.groovy as follows:

production {
 dataSource {
  username = "myuser"
  password = "mypass"
  pooled = true
  dbCreate = "update"
  driverClassName = "com.mysql.jdbc.Driver"
  url = "jdbc:mysql://ENDPOINT/DBNAME?user=USER&password=PASS"
  dialect = org.hibernate.dialect.MySQL5InnoDBDialect
  properties {
   validationQuery = "SELECT 1"
   testOnBorrow = true
   testOnReturn = true
   testWhileIdle = true
   timeBetweenEvictionRunsMillis = 1000 * 60 * 30
   numTestsPerEvictionRun = 3
   minEvictableIdleTimeMillis = 1000 * 60 * 30
   }
 }
}

As Bobby noted, I changed the values in the JDBC url as follows:

ENDPOINT: Endpoint value from the RDS detail page
DBNAME: Name from the RDS details page
USER: username from the RDS details page
PASS: password, that you entered during RDS creation

 (2) Server URL

In the grails-app/conf/Config.groovy file set the server URL to the URL shown on your Elastic Beanstalk dashboard. We can change this to use your own URL and use AWS Route 53 but I'll look at that in a separate post. For now, it should look something like this:

production {
 grails.serverURL = "http://appname.elasticbeanstalk.com/"
}

 (3) MySQL Dependency

If you don't already have the MySQL dependency setup in your Grails app, set the dependency in your grails-app/conf/BuildConfig.groovy like this:

dependencies {
 runtime 'mysql:mysql-connector-java:5.1.18'
}

Build & Upload Your Grails App

Create the production WAR file for your app:

grails prod war

Once the WAR is built, upload the file:

- Go to the Elastic Beanstalk console
- Select the application you created earlier
- Click the Upload and Deploy button to upload your WAR file The app will be started and you can try browsing to your URL

Troubleshooting without SSH

You can view your server logs including the tomcat logfile where your Grails app messages are typically written.

- From your apps the Elastic Beanstalk home screen select Logs on the navigation pane
- Click Snapshot Logs and AWS will create a snapshot of all the key logfiles on your server
- Click View log file to view the snapshot of your logfiles
- Search for the catalina.out file to find your Grails app error log

SSH & Troubleshooting

Before you can connect to your server with ssh, you'll also need to know the public DNS name for your EC2 instance.
- Go to the EC2 console
- Click on the name associated with the app you just created
- Scroll down to the bottom of the page and you'll find the Public DNS field. It will be something like ec2-54-212-82-123.us-west-2.compute.amazonaws.com.

Now that you know the name of your server, you can connect to it with ssh using that name and the key pair you downloaded earlier. I'm using the Linux ssh command but you could also use putty. Here's the Linux command to login:

ssh ec2-user@ec2-54-212-82-123.us-west-2.compute.amazonaws.com -i ~/myssh.pem

Other References

In addition to Bobby Warner's post I found the post by Christophe Coenraets on EC2 with Tomcat & MySQL and Liferay on Amazon helpful.