Deploy a Grails application on AWS Elastic Beanstalk

publish on

Complete guide on how to configure and deploy a Grails application on Amazon Web Services Elastic Beanstalk.

Elastic Beanstalk

Elastic Beanstalk is AWS platform-as-a-service solution. It’s based on Amazon Elastic Cloud Compute (Amazon EC2), Amazon Simple Storage Service (Amazon S3), Amazon Simple Notification Service (Amazon SNS), Elastic Load Balancing, and Auto Scaling.

The advantage over other platform-as-a-service solution is that you can still take control over your instance through SSH if you have a specific need for your project. Also this service is free to use, you only pay for AWS usage (could be free the first year if you stay under the free tier usage).

The stack used to deploy a Grails app is a classic and proven Java stack composed of an Apache and Tomcat on an Amazon Linux (32 or 64).

Begin with AWS

Create an account or login to your Amazon Console https://console.aws.amazon.com/.

Navigate to Elastic Beanstalk console:


If it’s the first time you have launched the Elastic beanstalk section, you’ll see a wizard to help you starting out. Before going further, be carefull to choose a region where your Amazon EC2 will reside:

  • US East (N. Virginia)
  • US West (Oregon)
  • US West (N. California)
  • EU (Ireland)
  • Asia Pacific (Singapore)
  • Asia Pacific (Tokyo)
  • Asia Pacific (Sydney)
  • South America (São Paulo)


To deploy a Grails app choose:

  1. Tomcat
  2. 7 on 32bit Amazon
  3. Single Instance

Click on Get started, you will be redirected to the main page of Elastic Beanstalk:


Configure your Amazon EC2 instance:

Now choose the amount of ram you’ll need for your app. If you want to stay in the free tier stay under 613 mb of ram (http://aws.amazon.com/free/). A Grails app (depending of its size and sever load) can run pretty well using 512mb of RAM so you can set those params:

  1. Initial JVM heap size: 512m
  2. Maximum JVM heap size: 512m
  3. Maximum JVM permanent generation size: 128m

As seen below:


Now that your instance is ready, we'll move to the next step, setting up an Amazon RDS instance to hold a MySQL Database.

Setting up an Amazon RDS instance

In the Configuration screen, navigate to the Data layer section and click on create a new RDS instance.

Then fill up the form:

  1. Snapshot : none
  2. DB Engine : mysql
  3. Instance Class: db.t1.micro (if you want to stay in the free tier)
  4. Allocated Storage: 5 to 20 Gb (if you want to stay in the free tier)
  5. Deletion Policy: create Snapshot (each time you will shut down the RDS instance the data will be backup in your Amazon S3 bucket. This is useful otherwise all data in the DB will be lost).
  6. Availability: Single Availability


Write down the following information for future use:

  1. Username
  2. Password
  3. Endpoint name
  4. Port number

Now that the RDS is up and running, the EC2 instance where will be living your Grails app needs to have the right to access this RDS instance. Elastic beanstalk has added a new DB security group automatically. If you want to see the newly added record go back to the main console screen and click on RDS Managed Relational Database Service:


Then navigate to Security Groups:

You should see that Elastic Beanstalk has added a new security group with a description “Enable database access to Beanstalk application”.


Prepare Grails apps for deployment

Now it’s time to prepare our Grails app to the promotion, edit the file Config.groovy, find the production grails.serverURL variable under environments:

environments {
    production {
        grails.logging.jul.usebridge = false
        grails.serverURL = "http://{environment_ name}.elasticbeanstalk.com"
    }
}

Replace {environment_name} by your actual environment URL that you could find in the Elastic Beanstalk console. It’s not the sexiest url in the world but will do to test everything. When you'll be ready to go live, you can read the how to map a domain name to an AWS Elastic Beanstalk (EC2) instance.


The next step is to configure the access to the RDS instance aka MySQL Database we just setup previously.

Open the DataSource.groovy file and replace the production clojure (under environments) with this one :

	production {
		dataSource {
		 username = "{user}"
		 password = "{password}"
		 pooled = true
		 dbCreate = "update"
		 driverClassName = "com.mysql.jdbc.Driver"
		 url = "jdbc:mysql://{endpoint}:{port_number}/ebdb?user={user}&password={password}"
		 dialect = org.hibernate.dialect.MySQL5InnoDBDialect
		 properties {
		  validationQuery = "SELECT 1"
		  testOnBorrow = true
		  testOnReturn = true
		  testWhileIdle = true
		  timeBetweenEvictionRunsMillis = 1800000
		  numTestsPerEvictionRun = 3
		  minEvictableIdleTimeMillis = 1800000
		  }
		}
	   }

Replace:

  1. {user} your RDS instance user name defined earlier.
  2. {password} your RDS instance password
  3. {endpoint} your RDS endpoint
  4. {port} your RDS endpoint port number

Now that the Grails app is configured, we can package it using a the following Grails command:

{project_root}\ grails prod war

Deploy on AWS

The final step is to deploy the war with the Grails app inside. To do so launch the Elastic Beanstalk console and select your environment name on the next page:


And click on Upload and deploy:


Choose the war file you just generated in:

{project_root}\target\project_name-0.1.war

Choose a label, example “first release” (it’s important to have a well-chosen label because each war will be stored in a S3 bucket after deployment and you will be able to re-deploy an old version in case something goes wrong when deploying a new version.


Click on deploy.


wait a couple of minutes till you see a green check mark.


Then, finally you can test your app using your EC2  url.


If you get an error you’ll need to check out the log to see what went wrong. To do so navigate to the Logs section of Elastic beanstalk and click on Snapshot logs. Tomcat logs will be available after a few minutes for you to investigate.


On a related subject, you might be interrested in learning how to map a sub-domain to an Amazon S3 bucket in order to hold your website static data.

I hope you found this guide useful. If ever you encounter any issues or need any clarifications on the deployment process feel free to leave a comment below.

comments powered by Disqus