Principal Software Engineer

Syed Sarmad Sabih

CreativeChaos

Full-Stack Software Engineer

Coding in Ruby for 6+ years

Aspiring Writer

Deploying To AWS With Rails And Capistrano

slides.com/sarmadsabih/aws-meetup-2

AWS EC2

Secure and resizable compute capacity in the cloud. Launch applications when needed without upfront commitments.

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers.

Amazon EC2’s simple web service interface allows you to obtain and configure capacity with minimal friction. It provides you with complete control of your computing resources and lets you run on Amazon’s proven computing environment. Amazon EC2 reduces the time required to obtain and boot new server instances to minutes, allowing you to quickly scale capacity, both up and down, as your computing requirements change. Amazon EC2 changes the economics of computing by allowing you to pay only for capacity that you actually use. Amazon EC2 provides developers the tools to build failure resilient applications and isolate them from common failure scenarios.

Benefits =>

  • ELASTIC WEB-SCALE COMPUTING
  • COMPLETELY CONTROLLED
  • FLEXIBLE CLOUD HOSTING SERVICES
  • INTEGRATED
  • RELIABLE
  • SECURE
  • INEXPENSIVE

AWS SES

Flexible, affordable, and highly-scalable email sending and receiving platform for businesses and developers

Amazon Simple Email Service (Amazon SES) is a cloud-based email sending service designed to help digital marketers and application developers send marketing, notification, and transactional emails. It is a reliable, cost-effective service for businesses of all sizes that use email to keep in contact with their customers.

You can use our SMTP interface or one of the AWS SDKs to integrate Amazon SES directly into your existing applications. You can also integrate the email sending capabilities of Amazon SES into the software you already use, such as ticketing systems and email clients.

Benefits =>

  • HIGH DELIVERABILITY
  • COST-EFFECTIVE
  • CONFIGURABLE

Use Cases =>

  • SEND TRANSACTIONAL MESSAGES
  • SEND NOTIFICATIONS
  • SEND MARKETING COMMUNICATIONS
  • RECEIVE INCOMING EMAIL

Benefits =>

Pay as you go, and pay only for what you use. There are no upfront fees, no time-consuming pricing negotiations, no fixed expenses, and no minimum charges. And, if you send from an application hosted in Amazon EC2, the first 62,000 emails you send every month are free.

If you wanna know more about AWS SES, you can refer to this article I wrote in 2016: https://www.sitepoint.com/deliver-the-mail-with-amazon-ses-and-rails/

AWS SQS

Fully managed message queues for microservices, distributed systems, and serverless applications

Amazon Simple Queue Service (SQS) is a fully managed message queuing service that enables you to decouple and scale microservices, distributed systems, and serverless applications. SQS eliminates the complexity and overhead associated with managing and operating message oriented middleware, and empowers developers to focus on differentiating work. Using SQS, you can send, store, and receive messages between software components at any volume, without losing messages or requiring other services to be available. Get started with SQS in minutes using the AWS console, Command Line Interface or SDK of your choice, and three simple commands.

SQS offers two types of message queues. Standard queues offer maximum throughput, best-effort ordering, and at-least-once delivery. SQS FIFO queues are designed to guarantee that messages are processed exactly once, in the exact order that they are sent.

Benefits =>

  • ELIMINATE ADMINISTRATIVE OVERHEAD
  • RELIABLY DELIVER MESSAGES
  • KEEP SENSITIVE DATA SECURE
  • SCALE ELASTICALLY AND COST-EFFECTIVELY

A remote server automation and deployment tool written in Ruby.

(https://capistranorb.com)

A Simple Task

role :demo, %w{example.com example.org example.net}
task :uptime do
  on roles(:demo), in: :parallel do |host|
    uptime = capture(:uptime)
    puts "#{host.hostname} reports: #{uptime}"
  end
end

For Any Language

Capistrano is written in Ruby, but it can easily be used to deploy any language.

If your language or framework has special deployment requirements, Capistrano can easily be extended to support them.

Overview Of The Setup

Requirements

  1. Create and setup Rails app
  2. Spin up EC2 instance
  3. Setup Rails environment on EC2
  4. Install and configure Nginx
  5. Provision DB through AWS RDS Postgres
  6. Connect the Rails app to the DB
  7. Prepare for deployment with Capistrano

Advanced Deployment Strategies with AWS, Rails and Capistrano

Advanced Deployment Strategies with AWS, Rails and Capistrano

Create and setup Rails app

  1. We're gonna be using Ruby 2.5.3 for our application
  2. Rails version is 5.2.2
  3. Postgres as the DB
  4. Create a new Rails application by typing: rails new aws_meetup_2 -d postgresql
  5. Create a Git repository and push the application
  6. Git Repo: https://github.com/sarmad90/aws_meetup_2
  1. Uncomment the mini_racer gem from the Gemfile
  2. Add the required gems to the Gemfile in the development group.
  3. Add SQS and Shoryuken gems to the Gemfile.

Make Changes To The Gemfile

group :development do
    gem "capistrano3-puma"
    gem "capistrano"
    gem "capistrano-bundler", require: false
    gem "capistrano-rvm"
    gem "capistrano-rails", require: false
    gem 'capistrano-shoryuken'
end
gem 'aws-sdk-sqs'
gem 'shoryuken'

Setup EC2 instance using this Sitepoint article of mine: https://www.sitepoint.com/continous-deployment-of-rails-with-semaphoreci/

  1. Create SSH key and add to Github.
  2. Copy your development machine's public SSH key to EC2 instance's authorized_keys file.
  3. Install Ruby through RVM or RBENV (RVM preferred)
  4. Install Bundler Gem
  5. Install Nginx
  6. Install Git
  7. Install libpq-dev library which helps Rails app to communicate with Postgres on the RDS

Setup Infrastructure

Configure Nginx In /etc/nginx/sites-available/default:

upstream app {
  # Path to Puma SOCK file, as defined previously
  server unix:///home/ubuntu/aws_meetup_2/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {
  listen 80;
  server_name localhost;

  root /home/ubuntu/aws_meetup_2/current/public;

  try_files $uri/index.html $uri @app;

  location / {
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Connection '';
    proxy_pass http://app;
  }

  location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}
  1. Setup Capistrano: cap install

  2. Set main ec2 server config in config/deploy/production.rb

  3. Capfile configuration

  4. Configure config/deploy.rb file

Note: Your EC2 servers' SSH key must be added to your Github, Bitbucket or Gitlab account. And your deployment machine's SSH key must be added in all of your EC2 instances' authorized_keys file.

Setup Capistrano

  1. Create RDS
  2. Configure database.yml
  3. Adjust credentials file
  4. Deploy: cap production deploy
  5. Verify the deployment by visiting the EC2 instance

Prepare For The First Deployment

CreativeChaos is looking for candidates for Summer Internship Program

News for Students =>

Get in touch...

Get In Touch

  • Website: www.sarmadsabih.com
  • Email: sam_sarmad@hotmail.com, ssabih@csquareonline.com
  • Skype: sam_sarmad
  • Twitter: @syedsarmadsabih
  • LinkedIn: https://www.linkedin.com/in/sarmad-sabih-754b5264

  • Github: https://www.github.com/sarmad90

I Write At

  • Sitepoint: https://www.sitepoint.com/author/ssarmad/
  • Medium: https://medium.com/@sarmadsabih

If this talk has inspired you to take Ruby on Rails on a spin, then you should give this piece a read:

https://medium.com/@sarmadsabih/a-brief-guide-to-learning-ruby-on-rails-b5c0e32acabb

Deploying to AWS using Rails and Capistrano

By Sarmad Sabih

Deploying to AWS using Rails and Capistrano

Slide for AWS Karachi Meetup #2. Demonstration of Amazon Web Services like SQS, SES, S3, EC2 and RDS.

  • 673