Wednesday, July 26, 2017

Deploying Rails App to Elastic Beanstalk using AWS CLI

$ brew update
$ brew install aws-elasticbeanstalk

$ **rvm or rbenv optional setup here**
$ gem install rails
$ rails new pine
$ cd pine
$ git init && git add -A && git commit -m "Rails 5.1 Skeleton"

$ rails g health index

Define route and return { status: 'ok' } JSON in index action.

$ git add .
$ git commit -am "Added health controller"
$ git push

$ eb init
Select a default region
1) us-east-1 : US East (N. Virginia)
Select an application to use
1)[ Create new Application ]
Enter Application Name
(default is "rails5app"):
Application rails5app has been created.
It appears you are using Ruby. Is this correct?
(y/n): y
Select a platform version.
1) Ruby 2.3 (Puma)
Do you want to set up SSH for your instances?
(y/n): y
Select a keypair.
1) [ Create new KeyPair ]
(default is 1): 1

$ git commit -am "Post EB init"
$ eb create production

Wait for some time.

$ eb status

$ eb setenv SECRET_KEY_BASE=$(rails secret)

eb deploy

to push any changes

Add 'pg' gem to Gemfile. Add:

    <<: default="" p="">    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'] %>
to database.yml.

Setup RDS instance
How to do the above using CLI?
Add a folder .ebextensions, and a file ruby.config

  git: []

Add a file 0001_setup_swap.config in the .ebextensions folder with the following text:

    command: echo “noswap”#dd if=/dev/zero of=/swapfile bs=1M count=3072
    command: echo “noswap”#mkswap /swapfile
    command: echo “noswap”#swapon /swapfile
How to setup redis using CLI?

 host: ‘localhost’
 port: ‘6379’
 host: ‘localhost’
 port: ‘6379’
 host: ‘’
 port: ‘6379’

How to setup background job processing SQS using CLI?

eb init - Creating Application
eb create - Creating environment
eb console - Opening Elastic Beanstalk Dashboard
eb list - Show current environment
eb use - Switch environment
eb logs - Logs Checking
eb deploy - Deployment

Type eb create
For environment name prompt, enter your-app-name-staging.

Type eb create
For environment name prompt, enter your-app-name-production

eb console

Check the UI for the health status of both environments.

// Switching and deploying to staging
eb use your-app-name-staging
eb deploy

// Switching and deploying to production
eb use your-app-name-production
eb deploy
// Filename: deploystaging.bash

eb use meclub-staging
eb list
eb deploy

chmod 700 deploystaging.bash

// Execute script


Configure AWS Elastic Beanstalk to Scale
How to setup and deploy a Rails 5 app on AWS Beanstalk with Postgresql, Redis and more.
Elastic Beanstalk Docs