AWS Elastic Beanstalk is a cloud deployment and provisioning service that automates the process of getting applications set up on the Amazon Web Services (AWS) infrastructure. You don’t pay for Elastic Beanstalk, it’s free service but you do pay for the resources it created.
First thing first you need an amason AWS account, simply sign up on Amazon AWS, it offers free tier for a year when this is written.
Assuming that you already have a rails app that is ready to be deployed. If not, please follow the commands below to create a sample rails app.
$ rails new blog
$ cd blog
$ git init && git add -A && git commit -m "Add rails scaffold"
$ rails generate scaffold post title:string body:text
$ bundle exec rake db:migrate
$ git add -A && git commit -am "Add post resource"
Install it via Homebrew (or follow this instruction for PC)
$ brew update
$ brew install aws-elasticbeanstalk
CD into your rails app directory, then initalise the app
cd blog
eb init
Select a default region
3) us-west-2 : US West (Oregon)
Select an application to use
[ Create new Application ]
Enter Application Name
blog
Application blog has been created.
It appears you are using Ruby. Is this correct?
(y/n): y
Select a platform version.
1) Ruby 2.2 (Puma)
Do you want to set up SSH for your instances?
(y/n): n
Note: you will need setup your AWS key id and secret ID if it’s the first you use Elastic Beanstalk Command Line Tools.
Run
eb create blog-env:
It may takes a few minutes to execute.
Then set the secret key and any other environment variables
# for mac user
rake secret | pbcopy
eb setenv SECRET_KEY_BASE={the secret key in your clipboard}
# database.yml
production:
<<: *default
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: Please remember to create db.t2.micro RDS instance only, it’s only free.
Push the changes then do
eb deploy
You app is now live!