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)
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 http://archive.ubuntu.com/ubuntu $(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
Download the latest version of pip:
Run the downloaded script with python3
python3 get-pip.py --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 .
Now , install AWS CLI , using pip:
pip install awsebcli --upgrade --user
Verify , eb (Elastic Beanstalk ) is installed correctly :
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:
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 https://get.rvm.io | 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
sudo apt-get install nodejs
Run , to check if Rails is installed properly:
Create a rails app with postgres as database:
rails new myapp --database=postgresql
Get inside the newly created Rails app directory:
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:
And create a packages.config file in the directory
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:
Save the file. Now edit the config/database.yml file as follows:
host: <%= ENV['C9_IP'] %>
port: <%= ENV['C9_PORT'] %>
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 :
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
Save all changes . Initialize a git repository.
Commit all changes
git commit -am 'First deploy to AWS EB'
And deploy your app to AWS EB:
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
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
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)