Adding an Amazon RDS DB instance to your Ruby application environment
You can use an Amazon Relational Database Service (Amazon RDS) DB instance to store data gathered and modified by your application. The database can be attached to your environment and managed by Elastic Beanstalk, or created and managed externally.
If you are using Amazon RDS for the first time, add a DB instance to a test environment with the Elastic Beanstalk Management Console and verify that your application can connect to it.
To connect to a database, add the adapter to your application and configure a connection with the environment properties provided by Elastic Beanstalk. The configuration and connection code vary depending on the database engine and framework that you use.
Adding a DB instance to your environment
To add a DB instance to your environment
Open the Elastic Beanstalk console
, and in the Regions list, select your AWS Region. -
In the navigation pane, choose Environments, and then choose the name of your environment from the list.
Note If you have many environments, use the search bar to filter the environment list.
In the navigation pane, choose Configuration.
-
In the Database configuration category, choose Edit.
-
Choose a DB engine, and enter a user name and password.
-
Choose Apply at the bottom of the page.
Adding a DB instance takes about 10 minutes. When the environment update is complete, the DB instance's hostname and other connection information are available to your application through the following environment properties:
Property name | Description | Property value |
---|---|---|
|
The hostname of the DB instance. |
On the Connectivity & security tab on the Amazon RDS console: Endpoint. |
|
The port where the DB instance accepts connections. The default value varies among DB engines. |
On the Connectivity & security tab on the Amazon RDS console: Port. |
|
The database name, |
On the Configuration tab on the Amazon RDS console: DB Name. |
|
The username that you configured for your database. |
On the Configuration tab on the Amazon RDS console: Master username. |
|
The password that you configured for your database. |
Not available for reference in the Amazon RDS console. |
For more information about configuring an internal DB instance, see Adding a database to your Elastic Beanstalk environment.
Downloading an adapter
Add the database adapter to your project's gem file.
Example Gemfile – Rails with MySQL
source 'https://rubygems.org'
gem 'puma'
gem 'rails', '~> 6.1.4', '>= 6.1.4.1'
gem 'mysql2'
Common adapter gems for Ruby
-
MySQL –
mysql2
-
PostgreSQL –
pg
-
Oracle –
activerecord-oracle_enhanced-adapter
-
SQL Server –
activerecord-sqlserver-adapter
Connecting to a database
Elastic Beanstalk provides connection information for attached DB instances in environment properties. Use ENV['
to read the properties and configure a database connection.VARIABLE
']
Example config/database.yml – Ruby on rails database configuration (MySQL)
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'] %>