Earlier in this blog series, we showed you how to deploy a Ruby on Rails application to Amazon Web Services using AWS OpsWorks. In that example, we used an OpsWorks-managed MySQL database run on an Amazon EC2 instance. One common piece of feedback on that post was a desire to see how you can set up your stack with Amazon RDS. Today, we are going to show you how.
We are going to assume you’re familiar with our earlier post on how to deploy with OpsWorks in general. For this tutorial you can take one of the following approaches:
- Take your existing stack from following along with that post, delete your database and MySQL layers, then follow this tutorial.
- Clone your existing stack (in case you don’t want to lose your work), create a new app server instance, delete the new stack’s MySQL layer, then follow along.
- Start from a brand new stack, and go between both tutorials, replacing the DB steps from the previous tutorial with the DB steps here. If you’re going to go that route, we would recommend reading this tutorial first to understand the differences in approach.
Whichever approach you choose, you can be up and running in just a few minutes!
Create an RDS Instance
AWS OpsWorks will not create Amazon RDS instances on your behalf. You will need to create an instance and link it to OpsWorks.
- Open up the RDS console, navigate to Instances, and click Launch DB Instances.
- To be able to use our Rails example code unaltered, choose the MySQL engine (you can choose another engine if you like, see the next section for details).
- On the next screen, you can choose either Multi-AZ deployments or single-AZ, but if you want to stay within the RDS Free Usage Tier, then you should not choose a multi-AZ deployment.
- Make sure that you keep track of your
Master Password, and the
Database Namefrom the rest of the form.
- Within Configure Advanced Settings, make sure you set your VPC Security Groups to include
AWS-OpsWorks-Rails-App-Server. You can set Publicly Accessible to "No" as well.
- Once you’ve completed the forms, click Launch DB Instance. It may take a few minutes for your instance to launch, so now is not a bad time for tea or coffee.
Don’t Want MySQL?
You do not need to use MySQL as your database backend, that is just what we have chosen for this example. Want to use a different database backend? Just do the following:
- Replace the
mysql2gem with the adapter gem appropriate to your DB engine selection.
- Make sure you also use your adapter selection in your custom deployment JSON, found in your stack settings if you used our exact tutorial steps.
- Of course, select that database engine when creating your RDS instance.
Create an RDS Layer
Now that you have an RDS Instance, you need to register it with OpsWorks.
- Navigate to the OpsWorks console, to the Layers section.
- Click + Layer, and select the RDS tab.
- Your RDS instance should be on this screen to select. Select it, then enter your User and Password from the RDS instance creation step.
- Click Register with Stack.
This registers your database with OpsWorks, and next you need to connect your app.
Attach Your RDS Instance to Your App
- From the Apps section, click on Edit for TodoApp (or whatever app you are developing for).
- Under the Data Sources section, select RDS.
- It should auto-select your instance; select it if not. Then, fill in your Database name from the RDS instance creation process.
- Click Save to save your changes.
Note that this should work for any of the topic branches we’ve made for TodoApp, as your choice of database host is transparent to your app so long as you have the correct database adapter gem in your
Deploy the App
To use your new database, simply run an app deployment, making sure you select the Migrate database option so that Rails can set up your database.
Once that’s done, navigate to your app server host and play around with your app. You’re now running on RDS!