AWS Elastic Beanstalk
Developer Guide (API Version 2010-12-01)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Deploying a Rails Application to AWS Elastic Beanstalk

This section walks you through deploying a sample application to AWS Elastic Beanstalk using eb (an updated command line interface) and Git, and then updating the application to use the Rails framework. This example uses a configuration file to customize and configure the Ruby container. For more information about the configuration file, see Customizing and Configuring AWS Elastic Beanstalk Environments. Ruby 1.8.7, 1.9.3, and 2.0.0 have been tested with AWS Elastic Beanstalk, and it uses the latest Rails version available.

Note

This example uses Amazon RDS, and you may be charged for its usage. For more information about pricing, go to Amazon Relational Database Service (RDS) Pricing. If you are a new customer, you can make use of the AWS Free Usage Tier. For details, go to AWS Free Usage Tier.

Step 1: Create Your Rails Application

Before you create your Rails application, make sure you have installed Ruby and Rails successfully. For more information, go to http://rubyonrails.org/download. In this step, you create the application. First, generate an empty Rails skeleton so that you can set it up against a Git repository.

rails new fooapp
cd fooapp

Step 2: Set Up Your Git Repository

Eb enables you to deploy applications quickly and more easily using Git. Eb is available as part of the CLI package. Before you can use the command line interface, you need to install the following prerequisite software to set up your Git repository.

To install the prerequisite software and initialize your Git repository

  1. Install the following software onto your local computer:

    1. Linux/Unix/OS X

    2. Windows

  2. Initialize your Git repository and commit your changes.

    git init && git add -A && git commit -m "Initial commit"

Step 2: Configure AWS Elastic Beanstalk

You use eb, a command line tool, to configure AWS Elastic Beanstalk. If you haven't already installed eb on your local computer, do that now at the AWS Sample Code & Libraries website. If you are running eb on a Linux operating system, you will need to install Python 2.7 or 3.0.

Before you use eb, set your PATH to the location of eb. The following table shows an example for Linux/UNIX and Windows.

On Linux and UNIXOn Windows
$ export PATH=$PATH:<path to unzipped EB CLI package>/eb/linux/python2.7/ C:\> set PATH=%PATH%;<path to unzipped EB CLI package>\eb\windows\

Use the init command, and AWS Elastic Beanstalk will prompt you to enter this information. If a default value is available, and you want to use it, press Enter.

To configure AWS Elastic Beanstalk

  1. From your directory where you created your local repository, type the following command.

    eb init
  2. When you are prompted for the access key ID, type your access key ID. To get your access key ID, see How Do I Get Security Credentials? in the AWS General Reference.

    Enter your AWS Access Key ID (current value is "AKIAIOSFODNN7EXAMPLE"): 
  3. When you are prompted for the secret access key, type your secret access key. To get your secret access key, see How Do I Get Security Credentials? in the AWS General Reference.

    Enter your AWS Secret Access Key (current value is "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"): 
  4. When you are prompted for the AWS Elastic Beanstalk region, type the number of the region. For information about this product's regions, go to Regions and Endpoints in the Amazon Web Services General Reference. For this example, we'll use US East (Virginia).

  5. When you are prompted for the AWS Elastic Beanstalk application name, type the name of the application. AWS Elastic Beanstalk auto-generates an application name based on the current directory name if an application name has not been previously configured. In this example, we use railsapp.

    Enter an AWS Elastic Beanstalk application name (auto-generated value is "windows"): railsapp

    Note

    If you have a space in your application name, make sure you do not use quotes.

  6. When you are prompted for the AWS Elastic Beanstalk environment name, type the name of the environment. AWS Elastic Beanstalk automatically creates an environment name based on your application name. If you want to accept the default, press Enter.

    Enter an AWS Elastic Beanstalk environment name (current value is "railsapp-env"): 

    Note

    If you have a space in your application name, make sure you do not have a space in your environment name.

  7. When you are prompted, choose an environment tier. For this example, we'll use 1.

    Available environment tiers are:
    1) WebServer::Standard::1.0
    2) Worker::SQS/HTTP::1.0
    
  8. When you are prompted for the solution stack, type the number of the solution stack you want. For this example, we'll use 32bit Amazon Linux 2013.09 running Ruby 1.9.3.

  9. When you are prompted, choose an environment type.

    Available environment types are: 
    1) LoadBalanced
    2) SingleInstance
    			
  10. When you are prompted to create an Amazon RDS DB instance, type y or n. For this example, we'll type y.

    Create an RDS DB Instance? [y/n]:
  11. When you are prompted to create the database from scratch or a snapshot, type your selection. For this example, we'll use No snapshot.

  12. When you are prompted to enter your RDS user master password, type your password containing from 8 to 16 printable ASCII characters (excluding /, \, and @).

    Enter an RDS DB master password: 
    Retype password to confirm: 
  13. When you are prompted to create a snapshot if you delete the Amazon RDS DB instance, type y or n. For this example, we'll type n. If you type n, then your RDS DB instance will be deleted and your data will be lost if you terminate your environment.

    You should see a confirmation that your AWS Credential file was successfully updated.

    By default, eb sets the following default values for Amazon RDS.

    • Database engine — MySQL

    • Default version: — 5.5

    • Database name: — ebdb

    • Allocated storage — 5GB

    • Instance class — db.t1.micro

    • Deletion policy — delete

    • Master username — ebroot

  14. When you are prompted to enter your instance profile name, you can choose to create a default instance profile or use an existing instance profile. Using an instance profile enables IAM users and AWS services to gain access to temporary security credentials to make AWS API calls. Using instance profiles prevents you from having to store long-term security credentials on the EC2 instance. For more information about instance profiles, see Granting Permissions to Users and Services Using IAM Roles. For this example, we'll use Create a default instance profile.

After configuring AWS Elastic Beanstalk, you are ready to deploy a sample application.

If you want to update your AWS Elastic Beanstalk configuration, you can use the init command again. When prompted, you can update your configuration options. If you want to keep any previous settings, press the Enter key. If you want to update your Amazon RDS DB configuration settings, you can update your optionsettings file in the .elasticbeanstalk directory, and then use the eb update command to update your AWS Elastic Beanstalk environment.

Step 4: Create an Application

Next, you need to create and deploy a sample application. For this step, you use a sample application that is already prepared. AWS Elastic Beanstalk uses the configuration information you specified in the previous step to do the following:

  • Creates an application using the application name you specified.

  • Launches an environment using the environment name you specified that provisions the AWS resources to host the application.

  • Deploys the application into the newly created environment.

Use the start command to create and deploy a sample application.

To create the application

  • From your directory where you created your local repository, type the following command.

    eb start

This process may take several minutes to complete. AWS Elastic Beanstalk will provide status updates during the process. If at any time you want to stop polling for status updates, press Ctrl+C. Once the environment status is Green, AWS Elastic Beanstalk will output a URL for the application.

Step 5: View the Application

In the previous step, you created an application and deployed it to AWS Elastic Beanstalk. After the environment is ready and its status is green, AWS Elastic Beanstalk provides a URL to view the application. In this step, you can check the status of the environment to make sure it is set to Green and then copy and paste the URL to view the application.

Use the status command to check the environment status, and then use the URL to view the application. Use --verbose to get detailed status information.

To view an application

  1. From your directory where you created your local repository, type the following command.

    eb status --verbose

    AWS Elastic Beanstalk displays the environment status. If the environment is set to Green, then AWS Elastic Beanstalk displays the URL for the application. If you attached an RDS DB Instance to your environment, your RDS DB information is displayed.

  2. Copy and paste the URL into your web browser to view your application.

Step 6: Update the Application

After you have deployed a sample application, you can update it with your own application. In this step, you'll update the sample application to use an Amazon RDS database.

To update the sample application to use Rails

  1. When you run the command eb init, AWS Elastic Beanstalk creates a .elasticbeanstalk directory in your project directory. This directory contains the private environment configuration data provided above, as well as other customized settings. Let's add this directory to .gitignore and then commit this change to .gitignore.

    git add .gitignore && git commit -m "Ignore .elasticbeanstalk from Git"

    Note

    For information about Git commands, go to Git - Fast Version Control System.

  2. Deploy to AWS Elastic Beanstalk.

    git aws.push

    You can also configure Git to push from a specific branch to a specific environment. For more information, see Deploying a Git Branch to a Specific Environment.

  3. Use the eb status --verbose command to check your environment status. When your environment is green and ready, refresh your web browser to view your updated application. You should see a "Welcome aboard You’re riding Ruby on Rails!" page.

Next, let's update the sample application to use Amazon RDS.

To update the sample application to use Amazon RDS

  1. On your local computer, update config/database.yml to use Amazon RDS. AWS Elastic Beanstalk has built-in support for Amazon RDS, so when you launch a Ruby container, you can associate an Amazon RDS DB Instance. The values of the Amazon RDS parameters are available through environment variables.

    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'] %>
  2. Add mysql2 to the Gemfile.

    gem 'mysql2'
  3. Add your files to your local Git repository, and then commit your change.

    git add .
    git commit -m "update database"
  4. Deploy to AWS Elastic Beanstalk. Make sure the database.yml file is not added to .gitignore.

    git aws.push

    You can also configure Git to push from a specific branch to a specific environment. For more information, see Deploying a Git Branch to a Specific Environment.

  5. Use the eb status --verbose command to check your environment status. When your environment is green and ready, refresh your web browser to view your updated application.

You can access the logs for your EC2 instances running your application. For instructions on accessing your logs, see Working with Logs.

Step 7: Clean Up

If you no longer want to run your application, you can clean up by terminating your environment and deleting your application.

Use the stop command to terminate your environment and the delete command to delete your application.

To delete the application

  1. From your directory where you created your local repository, type the following command.

    eb stop

    This process may take a few minutes. AWS Elastic Beanstalk will display a message once the environment has been successfully terminated.

    Note

    If you attached an RDS DB instance to your environment, your RDS DB will be deleted, and you will lose your data. To save your data, create a snapshot before you delete the application. For instructions on how to create a snapshot, go to Creating a DB Snapshot in the Amazon Relational Database Service User Guide.

  2. From your directory where you installed the command line interface, type the following command.

    eb delete

    AWS Elastic Beanstalk will display a message once it has successfully deleted the application.