How to configure Elastic Beanstalk for using Rails with Postgres?

Elastic Beanstalk has built in support for RDS. But, I want to use Postgresql with Elastic Beanstalk. Which will be the best approach for doing this?


Solution 1:

The easiest and cleanest way I found, assuming you're using Elastic Beanstalk CLI eb:

  1. Create an instance (follow the steps):

    eb init
    

    This will create an instance with MySQL.

  2. Once it's created, stop it.

    eb stop
    

    This will stop your instance and delete the MySQL database that was created by the initial setup.

  3. Edit your instance configuration file in .elasticbeanstalk directory at the root of your project, replace DBEngine entry in the [aws:rds:dbinstance] section by postgres.

    vi .elasticbeanstalk/optionsettings.<my-app>-env
    
    [aws:rds:dbinstance]
    DBDeletionPolicy=Delete
    DBEngine=postgres
    DBInstanceClass=db.t1.micro
    DBSnapshotIdentifier=
    DBUser=<dbrootuser>
    
  4. Restart the instance:

    eb start
    
    Starting application ".<my-app>".
    Waiting for environment "<my-app>-env" to launch.
    2014-07-17 17:53:15 INFO    createEnvironment is starting.
    2014-07-17 17:53:21 INFO    Using elasticbeanstalk-us-east-1-xxxxxxx as Amazon S3 storage bucket for environment data.
    2014-07-17 17:53:52 INFO    Created EIP: nn.nnn.nnn.nnn
    2014-07-17 17:53:58 INFO    Created security group named: awseb-e-xxx
    2014-07-17 17:54:10 INFO    Creating RDS database named: <db name>. This may take a few minutes.
    2014-07-17 18:06:09 INFO    Created RDS database named: <db name>
    2014-07-17 18:07:07 INFO    Waiting for EC2 instances to launch. This may take a few minutes.
    
  5. Check the status. Once it gets green, you'll now have a postgres database.

    eb status --verbose
    
    URL         : <my-app>-env-xxxxx.elasticbeanstalk.com
    Status      : Ready
    Health      : Green
    
    RDS Database: YYYYDatabase | <db name>.zzzzzz.us-east-1.rds.amazonaws.com:5432
    Database Engine:    postgres 9.3.3
    Allocated Storage:  10
    Instance Class:     db.t1.micro
    Multi AZ:           False
    Master Username:    <dbrootuser>
    Creation Time:      2014-07-17 18:02:28
    DB Instance Status: available
    

Solution 2:

You can set your custom postgres database running anywhere and make your rails application to use it from your database.yml file settings.

For e.g.

You can get a hosted postgres instance on Heroku Postgres - it gives very nicely formatted configuration details for ActiveRecord. You just have to get them in your database.yml file.