Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Deploying Rails applications to AWS

Deploying Rails applications to AWS

A manifesto of best practices

Hugh Evans

March 25, 2015
Tweet

More Decks by Hugh Evans

Other Decks in Programming

Transcript

  1. 12 factor apps • VI. Processes
 Execute the app as

    one or more stateless processes • VIII. Concurrency
 Scale out via the process model • XI. Logs
 Treat logs as event streams • Read it all! http://12factor.net/
  2. Command Line Interface Install with: $ pip install awscli $

    brew install awscli Configure with: $ aws configure

  3. Asset Sync AssetSync.configure do |config| config.aws_iam_roles = true config.fog_provider =

    'AWS' config.fog_region = 'ap-southeast-2' config.fog_directory = 'your-app-bucket' ! # Automatically replace files with their equivalent # gzip compressed version config.gzip_compression = true end
  4. Refile Refile consists of several parts: 1. Backends: cache and

    persist files (filesystem, S3, etc.) 2. Model attachments: map files to model columns 3. A Rack application: streams files and accepts uploads 4. Rails helpers: conveniently generate markup in your views 5. A JavaScript library: facilitates direct uploads
  5. Amazon Machine Images (AMIs) • Start with a base AMI

    (Ubuntu, Amazon Linux, etc.) • Provision your application decencies and create your own AMI • Look to automate the provisioning with a tool like Ansible • Provision all the way up to the point at which you can deploy your application and it will run
  6. Route 53 DNS Cloud Front CDN Elastic Load Balancer Availability

    Zone A Availability Zone B S3 0. /assets/* 1. /* * autoscale * logs go into CloudWatch
  7. Auto Scaling • Triggered by auto-scaling events • Auto-scaling events

    typically triggered by CloudWatch alerts • Also triggered by an Availability Zone outages • Instances join the active group when their health check is “green” • Launch configurations can contain user data, which can include scripts to pull down application code
  8. Cloud Watch • AWS-level monitoring (CPU, Network, etc.) • System

    level monitoring (Memory, Disk, etc.), using perl agent + 5 minute cron schedule • Log streams • Define alerts • CloudWatch alerts > SNS topics > PagerDuty
  9. CloudFront CDN • Create a distribution and assign your apps

    primary domain as a CNAME, eg. youapp.com • You can potentially assign other CNAME aliases assets-1.yourapp.com, assets-1.yourapp.com, etc. • Set up multiple origins and behaviours to route some traffic to S3 and the rest to your ELB/Rails app • Upload your SSL certificate
  10. Route 53 • DNS • Alias DNS to AWS serving

    points (S3 buckets, CloudFront distributions, ELBs) • Domain registration • Health Checks (pings) • Health Check > SNS Topic > PagerDuty
  11. RDS • MySQL • Postgres • Aurora • Oracle •

    MS SQL • Auto-patching • Multi-AZ (replication) • Automated backups/minor version patching • CloudWatch monitoring
  12. VPCs • Isolate environments/applications into their own group/network • Routing

    tables • Internet gateway • Peering two VPCs is possible if you want a shared services tier, e.g. ActiveDirectory
  13. Splitting it all up • Setting up your VPC CloudFormation

    • Provisioning your AMI Ansible / Packer • Provisioning your application AWS resources CloudFormation • Deploying your application Capistrano / Code Deploy • Packaging your last deploy for new autoscaled instances to pick up before going “green” on the load balancer User data shell script to tarball deploy directory and put in S3