Principal Software Engineer

Syed Sarmad Sabih

CreativeChaos

Full-Stack Software Engineer

Coding in Ruby for 6+ years

Aspiring Writer

Serverless with AWS and Ruby

AWS Lambda

Run code without thinking about servers. Pay only for the compute time you consume.

  • AWS Lambda lets you run code without provisioning or managing servers.
  • You pay only for the compute time you consume.
  • There is no charge when your code is not running.
  • Run code for virtually any type of application or backend service - all with zero administration.
  • Upload your code and Lambda takes care of everything required to run and scale your code with high availability.

Requirements

  1. Load Balancer
  2. Auto Scaling Group
  3. EC2 Instance
  4. EC2 Instance Image/AMI
  5. RDS - Postgres
  6. Nginx
  7. Capistrano
  8. AWS SDK for Ruby

Let's Get The Hands Dirty

  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 deploy_like_a_pro -d postgresql
  5. Create a Git repository and push the application
  6. Git Repo: https://github.com/sarmad90/deploy_like_a_pro
  1. Uncomment the mini_racer gem from the Gemfile
  2. Add the required gems to the Gemfile in the development group.

Make Changes To The Gemfile

gem "capistrano3-puma"
gem "capistrano"
gem "capistrano-bundler", require: false
gem "capistrano-rvm"
gem "capistrano-rails", require: false
gem "aws-sdk-autoscaling"
gem "aws-sdk-ec2"

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
  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/deploy_like_a_pro/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {
  listen 80;
  server_name localhost;

  root /home/ubuntu/deploy_like_a_pro/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;
}

Create an AMI image of the EC2 instance, which will be used as a template by the Auto Scaling group when scaling up servers.

  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

  1. Create Load Balancer
  2. Verify visiting the application through Load Balancer
  3. Create AutoScaling Group

Taking It Further

  1. Make changes to /deploy/production.rb to deploy to multiple remote servers
  2. Deploy: cap production deploy
  3. Verify the whole setup

Final Steps

Get In Touch

  1. Website: www.sarmadsabih.com
  2. Email: sam_sarmad@hotmail.com, ssabih@creativechaos.co
  3. Skype: sam_sarmad
  4. Twitter: @syedsarmadsabih
  5. LinkedIn: https://www.linkedin.com/in/sarmad-sabih-754b5264

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

I Write At

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

If this meetup 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

Serverless with AWS and Ruby

By Sarmad Sabih

Serverless with AWS and Ruby

  • 404