Creating a Rails 5 Web Application with postgresql and Puma and deploying in Amazon Web Services Elastic Beanstalk in under an hour

Without further ado , lets get straight to the code , but first ensure that you have a Amazon AWS account. Now , fireup a console/terminal window in your Linux environment and begin :
Find out the version of python installed in your system ( Yes , we will be building our app in Ruby on Rails , Python is required for AWS CLI- Command Line Interface)
python --version

If the version of Python on your system is less than 3.4 , you need to install it first

sudo apt-get update
sudo apt-get upgrade

Additionally , if your are using Cloud9 IDE , you may have to additionally type:

sudo add-apt-repository "deb $(lsb_release -sc) main universe restricted multiverse"

Now , you are ready to install python 3 and related libraries:

sudo apt-get install python*-dev
sudo apt-get install python3.4

Check , if python 3.4 (or newer ) is now installed

python3 --version

Download the latest version of pip:

curl -O

Run the downloaded script with python3

python3 --user

Note the folder where pip gets installed in the above command , and add that path(say , LOCAL_PATH) to your environment variable:


Run , the following command to see if pip >8.1.2 is installed .

pip --version

Now , install AWS CLI , using pip:

pip install awsebcli --upgrade --user

Verify , eb (Elastic Beanstalk ) is installed correctly :

eb --version

If , you face any problems installing AWS CLI , refer the AWS CLI Installation Instruction

So , with AWS CLI already installed , move over to Rails,

Find out what vesion of rails is installed on your computer:

rails -v

If it says Rails 5.0.1 or something , you are ready to go , If not install rails first, in the following steps:

Install rvm , ruby :

\curl -L | bash -s stable --ruby

If , curl is not installed on your system , use

sudo apt-get install curl

to install curl , first. Now , install the most current stable Rails relase using:

gem install rails

Chances are , if rails was not already installed on your system , Node.js is also not installed. Install it , as a javascript runtime is required on your system for running Rails apps and Node.js fits the bill perfectly:

sudo apt-get install nodejs

Run , to check if Rails is installed properly:

rails -v

Create a rails app with postgres as database:

rails new myapp --database=postgresql

Get inside the newly created Rails app directory:

cd myapp

Initilize an AWS EB Application

eb init -i

The above commands runs in interactive mode , and allows you to choose from defaults. It will also automatically find out that you are using Rails stack.

Also , the above command will create a hidden directory .elasticbeanstalk in you curent directory. Get into the directory:

cd .elasticbeanstalk

And create a packages.config file in the directory

touch packages.config

This file basically tells AWS to install anther required packages. In our case , we shall be installing postgresql . Using vim or your favorite editor , modify packages.config so that it contains the following lines:

# .ebextensions/packages.config
postgresql93-devel: []

Save the file. Now edit the config/database.yml file as follows:

adapter: postgresql
encoding: unicode
host: <%= ENV['C9_IP'] %>
port: <%= ENV['C9_PORT'] %>
adapter: postgresql
encoding: unicode
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>

Note that C9_IP and C9_PORT are the ip and port for postgres server on Cloud 9 IDE . You can change it as per your local system . Alternately , if you are concerned only with AWS deployment , you can omit the development group altogether .

Now , find out your Rails secret , run :

rake secret

The secret will be something like 2b6abf574e0d271f9ab68b4e8d16731b54a43158aa300ce8c61a872803f213007136a94364e3411c2b72ff28dd0c0a6 . Copy the secret. Now you are ready to create and deploy your Rails instance on AWS . Run:

eb create -d -db.engine postgres  -db.size 5  

Your app is now ready to be deployed . But since Rails 5 does not supply you a default route , we will create a route:

rails g controller Welcome hello

The above command will generate a controller named ‘Welcome’ with a view named ‘hello’ and a path ‘get ‘welcome/hello’ in your route. Change your route to in config/routes.rb

root 'welcome#hello'

Save all changes . Initialize a git repository.

git init

Commit all changes

git commit -am 'First deploy to AWS EB'

And deploy your app to AWS EB:

eb deploy

The command will take some time to run initially because in the background AWS needs to set up a lot of things for you , In the end , you shall be greeted with a success message . You can now open your app with

eb open

Congratulations , you just set up your Rails 5 app on AWS . Now you are free to add features to your Rails App. Just add those to git commit and use eb deploy . You also do not need to run rake db:migrate . eb deploy takes care of all migrations.

Now should you see any error in deployment , you can get access to logs and even to your rails console . Set up a SSH with your AWS instance:

eb ssh --setup

Do remember your passphrase expression that you provide in above command . For access in future you just have to run eb ssh and provide your passphrase.

You can now access your rails console using:

ssh> cd /var/app/current && bin/rails c

and see your logs using:

ssh > tail -f /var/app/current/log/production.log

or ,

tail -f /var/log/eb-activity.log

Now go ahead and build your Rails5 App hosted on AWS.!!

(This article was first published on my blog)