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:

production:
    <<: 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

packages:
 yum:
  git: []

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

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

development:
 host: ‘localhost’
 port: ‘6379’
test:
 host: ‘localhost’
 port: ‘6379’
production:
 host: ‘your-name.iz6wli.0001.use1.cache.amazonaws.com’
 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
#!/bin/bash

eb use meclub-staging
eb list
eb deploy


chmod 700 deploystaging.bash

// Execute script
./deploystaging.bash

References

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