08 May 2018
How to Deploy the Ruby on Rails application on AWS?
Before we start exploring the topic described in the title, let’s explain why we consider it a good basis for building web applications - or even a decent backend for mobile projects. We outline five main reasons:
- It allows developers to automate tasks with customized setup routes.
- It’s quick, due to modular design and powerful plugins.
- It supports a lot of Rails libraries for any kind of app.
- It has test suites, allowing the enhancement of coding practices.
- It has a set of conventions for the application’s architecture that may help you in developing the app from scratch.
However, deploying a Ruby on Rails application is not as easy as it seems. The first challenge comes with the selection of the right tool. Although many developers prefer to use Capistrano for deploying Rails applications, it may cause a lot of problems right from the first setup. Fortunately, there is another nice tool for deploying and scaling applications, developed by Amazon for AWS: Elastic Beanstalk. After reading this article, you’ll learn how to deploy Ruby on Rails app on AWS using this tool.
What is Elastic Beanstalk?
As we have mentioned, Elastic Beanstalk is a quick and easy-to-use tool for deploying and scaling an app on AWS. The service performs deployment, allocates resources, provides load balancing and automatic scaling, and monitors the health of the application. Within minutes, the application is ready for use. As a result, AWS Elastic Beanstalk enables your application to handle peak loads or traffic minimizing costs.
Elastic Beanstalk setup
First, make sure that you have an account at AWS. After signing up, you are entitled to receive many free services during the year.
1. We’ve made this tutorial mainly for Mac users, so for installation of the CLI for EB you need to use macOS missing package manager brew
$ brew update $ brew install awsebcli $ eb --version EB CLI 3.2.2 (Python 3.4.3)
2. Let's create a Ruby on Rails app, if you haven’t done it already.
$ *rbenv or rvm optional setup with latest ruby version* $ gem install rails $ rails new eb-railsapp $ cd eb-railsapp $ git init && git add -A && git commit -m "Initial Commit"
3. You can create some example pages in your Ruby on Rails application with a scaffold.
$ rails g scaffold article title body:text $ rails db:migrate $ git add . $ git commit -am "Add Articles"
4. Make sure that the application works locally. After that, you can start deploying Rails Application to Elastic Beanstalk AWS. Go to your app directory and initialize the Elastic Beanstalk.
$ eb init
5. After running the command above, you’ll see several questions about your app, platform version, configuration etc.
Then, you need to select the correct time zone:
Select a default region 1) us-east-1 : US East (N. Virginia) 2) us-west-1 : US West (N. California) 3) us-west-2 : US West (Oregon) 4) eu-west-1 : EU (Ireland) 5) eu-central-1 : EU (Frankfurt) 6) ap-south-1 : Asia Pacific (Mumbai) 7) ap-southeast-1 : Asia Pacific (Singapore) 8) ap-southeast-2 : Asia Pacific (Sydney) 9) ap-northeast-1 : Asia Pacific (Tokyo) 10) ap-northeast-2 : Asia Pacific (Seoul) 11) sa-east-1 : South America (Sao Paulo) 12) cn-north-1 : China (Beijing) 13) cn-northwest-1 : China (Ningxia) 14) us-east-2 : US East (Ohio) 15) ca-central-1 : Canada (Central) 16) eu-west-2 : EU (London) 17) eu-west-3 : EU (Paris) (default is 3):
6. After that, the tool asks you to enter the application name. Then, Elastic Beanstalk confirms that you are using Ruby. Next, you need to choose a Ruby version. Finally, it asks you about the access via SSH to check if the remote access is possible and to perform a custom configuration.
Enter Application Name (default is "eb-railsapp"): Application eb-railsapp has been created. It appears you are using Ruby. Is this correct? (Y/n): y Select a platform version. 1) Ruby 2.3 (Puma) 2) Ruby 2.2 (Puma) 3) Ruby 2.1 (Puma) 4) Ruby 2.0 (Puma) 5) Ruby 2.3 (Passenger Standalone) 6) Ruby 2.2 (Passenger Standalone) 7) Ruby 2.1 (Passenger Standalone) 8) Ruby 2.0 (Passenger Standalone) 9) Ruby 1.9.3 (default is 1): 1 Do you want to set up SSH for your instances?
Setup the environment
Congrats! You have configured Elastic Beanstalk on your system. Let’s start to create the environment.
1. Basically, this is how Elastic Beanstalk lets you switch between different versions of your Ruby on Rails application.
$ eb create production
After running this command, you deploy rails to AWS. Elastic Beanstalk setup environment, load balancers, security group, EC2 server, auto-scaling group and CloudWatch alarm notifications and more are deployed.
2. The quick status check gets information about your environment and application URL.
Environment details for: production Application name: eb-railsapp Region: eu-central-1 Deployed Version: app-ad6f-180419_190755 Environment ID: e-cpd7aashrp Platform: arn:aws:elasticbeanstalk:eu-central-1::platform/Passenger with Ruby 2.4 running on 64bit Amazon Linux/2.7.2 Tier: WebServer-Standard-1.0 CNAME: production.p3i3g974nv.eu-central-1.elasticbeanstalk.com Updated: 2018-04-19 16:09:53.185000+00:00 Status: Ready Health: Green
3. And the last thing that we should do with the Rails app is to generate a SECRET_KEY_BASE environment variable.
$ rake secret long_secret_key $ eb setenv SECRET_KEY_BASE=long_secret_key
4. It will update the environment key in secrets.yml. You can update any environment variables using this command.
Now you can run the following command to open the web app:
$ eb open
Now everything is setup and should work.
As you can see, Ruby on Rails works well for developers, enabling the simple and effective development of web applications. We hope this guide will help regular users to perform an AWS Ruby on Rails deployment quickly and easily and choose this development tool to build their websites and applications.
With this post, we start a series of articles written by our development team. This section will be devoted to various aspects of custom web and mobile development.
Stay tuned for news
Useful articles from our content team right to your inbox!
Is there a challenge your organization or company needs help solving? We’d love to discuss it.