Deploying a Scalable Application with AWS Elastic Beanstalk and New Relic
The Performance Zone is presented by AppDynamics. Scalability and better performance are constant concerns for the developer and operations manager. Try AppDynamics' fully-featured performance tool for Java, .NET, PHP, & Node.js.
There are a number of options for deploying your web applications. However, deploying scalable web apps can bring some unique complexities. You may be solving some of these complexities yourself by putting together an infrastructure on AWS. But with Elastic Beanstalk you can get a complete scalable solution without configuring all of the components yourself.
A scalable web application is able to bring up additional resources automatically, rather than requiring intervention from a human. This type of automated orchestration requires a fair bit of infrastructure — something that can take valuable time configuring. A robust scalable infrastructure should include application servers, database servers, load balancers, log aggregation, firewall, monitoring, and alerting. Without automation, nearly every component of your infrastructure would need updating to make it aware of the new resources. With Elastic Beanstalk — which includes all of the components of a robust infrastructure — the creation and tear-down of resources is handled automatically. In other words, you can have your cake and eat it too.
Elastic Beanstalk is not a separate technology stack from AWS. In fact, it’s bringing together the traditional components from EC2, S3, RDS and CloudWatch into a fully scalable infrastructure. So if your application is running on AWS already, it’s very likely you can take advantage of Elastic Beanstalk’s orchestration and scaling system right now. Elastic Beanstalk supports applications running on Java, Python, PHP and Ruby running on Linux, or IIS running on Windows Server. It also supports databases in MySQL, Oracle and Windows SQL Server. Because your application is running on EC2 instances, you can have additional packages installed to support your technology stack like memcached and Redis. Adding New Relic to your application is simple as well. And since we’ll just be using the standard AWS components, there aren’t any special changes you’ll need to make in order to get up and running.
In this example, we’ll be setting up Michael Hartl’s Rails Tutorial sample application running on Phusion Passenger. Before you get started, you’ll need the Elastic Beanstalk CLI Tools setup and properly configured.
Configure Your Application Stack
First, initialize the application stack. In this step, select where you’ll be hosting the application.
$ eb init
After verifying your AWS credentials, select which region to deploy your application. You can deploy instances to multiple availability zones within that region to provide for high quality redundancy. We’ll choose US West (Oregon), Option 2, for this example:
Select an AWS Elastic Beanstalk service region. Available service regions are: 1) US East (Virginia) 2) US West (Oregon) 3) US West (North California) 4) EU West (Ireland) 5) Asia Pacific (Singapore) 6) Asia Pacific (Tokyo) 7) Asia Pacific (Sydney) Select: (1 to 7): 2
Next set the application name, defaulted to the directory name you initialized the Beanstalk in. Just hit return to accept the default:
Enter an AWS Elastic Beanstalk application name (auto-generated value is "railstutorial"):
You can name the environment for the application, such as development, staging or production. We’ll be deploying a staging environment for this tutorial:
Enter an AWS Elastic Beanstalk environment name (auto-generated value is "railstutorial-env"): railstutorial-staging
Now it’s time to select the application stack. This is a Rails 3.2 application that runs on Ruby 1.9.3. We’ll be using 64Bit Amazon Linux instances for our application, Option 14. Below you can see the available stack options for your application:
Select a solution stack. Available solution stacks are: 1) 32bit Amazon Linux running PHP 5.3 2) 64bit Amazon Linux running PHP 5.3 3) 64bit Windows Server 2008 R2 running IIS 7.5 4) 64bit Windows Server 2012 running IIS 8 5) 32bit Amazon Linux running Tomcat 7 6) 64bit Amazon Linux running Tomcat 7 7) 32bit Amazon Linux running Tomcat 6 8) 64bit Amazon Linux running Tomcat 6 9) 32bit Amazon Linux running Python 10) 64bit Amazon Linux running Python 11) 32bit Amazon Linux running Ruby 1.8.7 12) 64bit Amazon Linux running Ruby 1.8.7 13) 32bit Amazon Linux running Ruby 1.9.3 14) 64bit Amazon Linux running Ruby 1.9.3 Select: (1 to 14): 14
Our application requires a relational database, so we’ll need to include RDS as part of our stack. We’re starting from a fresh install, so we don’t have a snapshot to restore from:
Create an RDS DB Instance? [y/n]: y Create an RDS BD Instance from (current value is "No snapshot"): 1) No snapshot 2) Other snapshot Select: (1 to 2): 1 Enter an RDS DB master password: Retype password to confirm:
Normally, you’d backup your data before you terminate your entire stack. But in this example, we don’t need to backup the data so we’ll select “no”.
If you terminate your environment, your RDS DB Instance will be deleted and you will lose your data. Create snapshot? [y/n]: n
Start Your Application Stack
That’s all it takes to configure Elastic Beanstalk for an application. If you ever need to update your choices, simply use
eb init to go through them again. Now we just need to start the stack and push our code.
$ eb start Starting application "railstutorial". Waiting for environment "railstutorial-staging" to launch. 2012-12-02 12:25:08 INFO createEnvironment is starting. ... Application is available at "railstutorial-staging-gcsi2pfevm.elasticbeanstalk.com".
This will take some time to complete and once the command
finishes your application stack will still be booting. You can check the
status of your stack, looking for
$ eb status --verbose Retrieving status of environment "railstutorial-staging". URL : railstutorial-staging-gcsi2pfevm.elasticbeanstalk.com Status : Updating Health : Grey Environment Name: railstutorial-staging Environment ID: e-twb3pccvxz Solution Stack: 64bit Amazon Linux running Ruby 1.9.3 Version Label: git-c5b54c38f70ed4afcbd3624746fb0e6b9fffde3d-1354481313527 Date Created: 2012-12-02 12:25:09 Date Updated: 2012-12-02 12:48:35 Description: None RDS Database: AWSEBRDSDatabase | aapx7gzecu7cns.chojteloju75.us-west-2.rds.amazonaws.com:3306 Database Engine: mysql 5.5.27 Allocated Storage: 5 Instance Class: db.t1.micro Multi AZ: False Master Username: ebroot Creation Time: 2012-12-02 12:30:13 DB Instance Status: available
Once your application’s health changes to green, you have everything you need in place to deploy your code. You can visit the application’s URL now to find the sample AWS application in place.
Configure Database to Use RDS
Your app is nearly ready for deployment, first you’ll need to change your database configuration to use the RDS instance. The hard work is taken care of for us, where most of the database config is stored in environment variables. So for our Rails app, you’ll just need to have your
config/database.yml file include the following:
production: adapter: mysql2 encoding: utf8 database: <= ENV['RDS_DB_NAME'] > username: <= ENV['RDS_USERNAME'] > password: <= ENV['RDS_PASSWORD'] > host: <= ENV['RDS_HOSTNAME'] > port: <= ENV['RDS_PORT'] >
RDS currently supports MySQL, Oracle and MS SQL Server. In our app we’re using MySQL, so we’ll need to include the
mysql2 gem in our Gemfile and do
Deploy The Application Code
To deploy the application, we just need to git push to our stack:
$ git aws.push
For Rails apps, Elastic Beanstalk tools automatically run
migrations and compile our assets with the asset pipeline. The
application will need to restart after the push, so you can use
again to monitor the restart until the health returns to green. Once
you’re green again, you should be able to visit your app’s URL and see
your code running.
Configure Elastic Beanstalk for HTTPS
Our application uses HTTPS to secure our users from snoopers. It’s highly recommended that all portions of your site be served over HTTPS (not just the registration/login portions). To setup HTTPS on Elastic Beanstalk requires two steps. First, create and upload an SSL certificate, then configure Elastic Beanstalk to use your certificate.
For a production application, you should use a purchased certificate from a reputable Certificate Authority. For our sample application, we use a self-signed certificate (your browser will show you a certificate warning when using self-signed certificates and so they should never be used in production).
We’ll use OpenSSL to generate the certificate. First, we need a private key:
$ openssl genrsa 1024 > privatekey.pem
Next we use our private key to generate a Certificate Signing Request:
$ openssl req -new -key privatekey.pem -out csr.pem
You’ll be prompted to complete the certificate details. Answer each of the questions to complete the request. Then we generate the self-signed certificate:
$ openssl x509 -req -days 365 -in csr.pem -signkey privatekey.pem -out server.crt
You now have a self-signed certificate and private key which we’ll upload to AWS identity management service. You need the IAM CLI Tools installed and configured before you can continue. Use the IAM CLI to upload your certificate and private key:
$ iam-servercertupload -b server.crt -k privatekey.pem -s railstutorial
Although the command won’t return anything to your prompt, everything should be uploaded. Now you need to get the unique identifier for your certificate:
$ iam-servercertlistbypath arn:aws:iam::YOURACCOUNTNUM:server-certificate/railsTutorial
To configure Elastic Beanstalk to use HTTPS, you need to
Edit Configuration under the
Go to the
Load Balancer tab and set
HTTPS Listener Port to 443. Put the unique identifier from your certificate in the
SSL Certificate ID field.
Apply Changes and once your application stack restarts you’ll be able to visit your application over HTTPS.
Add New Relic to Your Application
Adding New Relic to an application is simply a matter of installing the agent and adding the configuration file to your codebase. For Rails, we’ll just add the
newrelic_rpm gem and the configuration in
You can get New Relic Standard for free if you are using AWS. Just signup for an account and follow the instructions for downloading the agent and config file. Within minutes of using your application you’ll see performance metrics in your New Relic dashboard.