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...

Using Amazon RDS with Ruby

With Amazon Relational Database Service (Amazon RDS), you can quickly and easily provision and maintain a MySQL, Oracle, or Microsoft SQL Server instance in the cloud. This topic explains how you can use Amazon RDS and Ruby with your AWS Elastic Beanstalk application. For more information about Amazon RDS, go to http://aws.amazon.com/rds/.

To use Amazon RDS from your AWS Elastic Beanstalk application, you need to do the following:

  1. Create an Amazon RDS DB instance.

  2. Establish a database connection in your code by using the connectivity information for your Amazon RDS DB instance.

  3. Deploy your application to AWS Elastic Beanstalk.

This topic walks you through the following:

  • Using a new Amazon RDS DB instance with your application

  • Using an existing Amazon RDS DB instance with your application

Using a New Amazon RDS DB Instance with Ruby

This topic walks you through creating a new Amazon RDS DB Instance and using it with your Ruby application.

To use a new Amazon RDS DB Instance and Ruby from your AWS Elastic Beanstalk application

  1. Create an Amazon RDS DB Instance. You can create an RDS DB Instance in one of the following ways:

    • Create an RDS DB instance when you create a new application version. For instructions using the AWS Elastic Beanstalk console, see Creating New Applications. For a sample walkthrough of a Rails application deployment with Amazon RDS using eb, see Deploying a Rails Application to AWS Elastic Beanstalk.

    • Create an RDS DB instance when you launch a new environment with an existing application version. For instructions using the AWS Elastic Beanstalk console, see Launching New Environments.

    • If you already deployed an application to AWS Elastic Beanstalk, you can create an RDS DB instance and attach it to an existing environment. For instructions using the AWS Elastic Beanstalk console, see Configuring Databases with AWS Elastic Beanstalk. If you use eb, use the eb init command to specify RDS configuration settings, and then use the eb update command to update your environment.

  2. Establish a database connection in your code using your Amazon RDS DB Instance's connectivity information. You can access your connectivity information using environment variables. The following shows how you would connect to the database on an RDS instance.

    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'] %>
  3. Deploy your updated application to your existing AWS Elastic Beanstalk environment. For information on how to deploy a new application version to an existing environment using the Elastic Beanstalk Console, see Step 4: Deploy New Version. If you are using eb, commit your changes and deploy your application. For an example, see Step 5: Update the Application.

Using an Existing Amazon RDS DB Instance with Ruby

You can update your Ruby application to use an Amazon RDS DB Instance that you have previously created. This topic walks you through how to update your Ruby application using an existing Amazon RDS DB Instance and deploy your application to AWS Elastic Beanstalk.

To use an existing Amazon RDS DB Instance and Ruby from your AWS Elastic Beanstalk application

  1. Create an AWS Elastic Beanstalk environment in one of the following ways:

    • Create a new application with a new environment. For instructions using the Elastic Beanstalk console, see Creating New Applications. For a sample walkthrough of a Rails application deployment with Amazon RDS using eb, see Deploying a Rails Application to AWS Elastic Beanstalk. You do not need to create an RDS DB Instance with this environment because you already have an existing RDS DB Instance.

    • Launch a new environment with an existing application version. For instructions using the Elastic Beanstalk console, see Launching New Environments. You do not need to create an RDS DB instance with this environment because you already have an existing RDS DB instance.

  2. Configure your Amazon RDS DB Security Group to allow access from the Amazon EC2 security group used by your AWS Elastic Beanstalk application. For instructions on how to find the name of your EC2 security group using the AWS Management Console, see Amazon EC2 Security Groups. For more information, go to the "Authorizing Network Access to an Amazon EC2 Security Group" section of Working with DB Security Groups in the Amazon Relational Database Service User Guide.

  3. Establish a database connection in your code using your Amazon RDS DB instance's connectivity information. The following examples show how you could connect to the database on an RDS instance at mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com using port 3306, with the user name "sa" and the password "mypassword".

    production:
      adapter: mysql2
      encoding: utf8
      database: exampledb
      username: root
      password: mypwd
      host: example.rds.amazonaws.com
      port: 3306  
  4. Deploy your updated application to your existing AWS Elastic Beanstalk environment. For information on how to deploy a new application version to an existing environment using the Elastic Beanstalk Console, see Step 4: Deploy New Version. If you are using eb, commit your changes and deploy your application. For an example, see Step 5: Update the Application.