The Blackninja Blog
Expand your Cloud Application Development and DevOps knowledge with detailed tutorials and case studies.
This post is going to show you how to configure a database instance using the Relational Database Service (RDS) and add it to your Elastic Beanstalk environment on Amazon Web Services.
RDS is a service that allows you host a relational database in the cloud without worrying about the infrastructure required to do so.
This guide assumes you have created an account with Amazon Web Services and you have an Elastic Beanstalk Environment setup already. It also assumes you’ve been following along the Deploying a Ruby on Rails Application Stack to AWS using Elastic Beanstalk and RDS.
If you allow Elastic Beanstalk to manage your database instance, you incur a significant risk that your Elastic Beanstalk environment lifecycle operations may inadventently destroy your database. Amazon recommends decoupling your database instance from your Elastic Beanstalk environment. Its a little more complicated to setup - but worth it.
TLDR: Do not use the Database section of your Elastic Beanstalk Configuration in Production
myapp_dev_2
10.0.1.0/24
for the IP rangemyapp_dev_3
10.0.2.0/24
for the IP rangeWe are going to use MySQL for this part of the guide. For two reasons, one, it supports a free tier. Second, because it has a great free database management app for the mac called Sequel Pro.
Congratulations! You just created a MySQL Database instance on RDS!
Wait until the DB instance status is Available
before moving on to the next steps.
By default, your new RDS Database Instance will not allow connections from your Elastic Beanstalk Environment. Technically, we need the EC2 instance created by the Elastic Beanstalk environment to be able to connect to the database. To do this, we will add an entry to our RDS security group.
An easy and informative way to test access from your Elastic Beanstalk Instance to your Database is to connect to the EC2 Instance using SSH and run a command using the MySQL client. This isn’t just helpful for setup, but can be very helpful to troubleshoot issues if someone changes the Inbound rules or database password.
eb ssh
mysql -h (paste your endpoint url here) -u (type your Master username) -p
mysql>
promptexit
to quit the mysql promptexit
again to close the SSH connectionNow that you have an RDS Database instance in the Available state, an Inbound rule setup in your Security group, and you’ve tested your connection from the EC2 instance to the database instance… you are ready to add the environment variables needed by your rails application to connect to the database!
Note: We could have used the eb setenv
command to set these also, but that would have been tedious for this many variables.
If you forgot your database name you can connect to your EC2 instance using SSH, then connect to your rds database instance using the mysql client command we used above and type show databases;
into the prompt.
Last step! You have configured your Elastic Beanstalk environment to use your RDS Database instance and you’ve setup environment variables that declare the connection details - now its time to update your Rails application to use this database in production (after a deployment).
If you’ve been following along and want to use Rails for your application database, you probably want to have a local installation for development.
brew install mysql
brew services start mysql
Note: the default user for this installation is root
and the password is blank. The installation will only accept connections from localhost.
gem 'mysql2'
to your Gemfile (you may only want to use this is production)bundle
in the Terminal to install this gem
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'] %>
eb deploy
Congratulations! Your Rails Application in Elastic Beanstalk is now using your new RDS Database instance.