How to setup RDS using AWS CLI
Assuming that you can have already installed the AWS CLI and configured it, you can run the following command:
aws rds create-db-instance --db-instance-identifier pine_production --allocated-storage 1 --db-instance-class db.m1.small --engine postgres --master-username my-aws-user --master-user-password my-aws-password
This should create a database called pine_production with 1 GB storage with small db instance. You also need to provide the master user password and the master user name. But, it gave the error message:
An error occurred (InvalidParameterValue) when calling the CreateDBInstance operation: The parameter DBInstanceIdentifier is not a valid identifier. Identifiers must begin with a letter; must contain only ASCII letters, digits, and hyphens; and must not end with a hyphen or contain two consecutive hyphens.
Replace the underscore with hyphen for the database name.
aws rds create-db-instance --db-instance-identifier pine-production --allocated-storage 1 --db-instance-class db.m1.small --engine postgres --master-username top-secret --master-user-password impossible-to-crack
This gave the error:
An error occurred (InvalidParameterCombination) when calling the CreateDBInstance operation: RDS does not support creating a DB instance with the following combination: DBInstanceClass=db.m1.small, Engine=postgres, EngineVersion=9.6.2, LicenseModel=postgresql-license. For supported combinations of instance class and database engine version, see the documentation.
Let's use medium db size instead of small.
aws rds create-db-instance --db-instance-identifier pine-production --allocated-storage 1 --db-instance-class db.t2.medium --engine postgres
This gave another error:
An error occurred (InsufficientDBInstanceCapacity) when calling the CreateDBInstance operation: Cannot create a db.t2.medium database instance because there are no availability zones with sufficient capacity for non-VPC and storage type : standard for db.t2.medium. Please try the request again at a later time.
The problem with the Postgres RDS created by CLI was that by default it was in VPC. So it was not reachable by the Rails app. I had to use the UI. Even it had problems. Finally I had to create RDS Large and it worked. I need to reduce it to small or medium later.
Rails App Database Configuration
In the Rails app, you need to specify the values for the credentials using the environment variables that AWS provides. In database.yml file:
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'] %>
The environment variables you see above are automatically exposed by RDS to the Beanstalk environment. We don't need to do anything to set up the environment variables. I specified the values for these when I created the Postgres database using the AWS console for RDS using the browser GUI. Even the browser GUI kept giving errors until I changed the database to large. The screenshot shows the failure message for small database.
Testing the Database Connectivity
You can use the psql command in the terminal to connect to the RDS instance:
psql --host=pine-production.cjolxsmxijnx.us-east-1.rds.amazonaws.com --port=5432 --username=piner --password --dbname=piner_production
The host parameter provides the database URL. You also need to provide the port number, database user name and the database name. You must enter the database password at the command line prompt.
Frequently Used eb Command
To check the value of environment variable, you can run:
$ eb printenv
You can open the URL to your Rails app using:
$ eb open
You can check the logs with:
$ eb logs
You can go to the AWS console in your browser with:
This setup must be separate from Elasticbeanstalk. Rails app deployed using Elastic Beanstalk can now use this database instance.