How to Deploy Ruby on Rails to AWS: Step-by-Step Guide
To deploy a Ruby on Rails app to AWS, use
AWS Elastic Beanstalk which manages servers for you. Prepare your app with a Procfile and database.yml, then use the eb cli commands to create and deploy your environment.Syntax
Deploying Rails to AWS Elastic Beanstalk involves these main steps:
- Initialize Elastic Beanstalk: Use
eb initto set up your app with AWS. - Create environment: Use
eb createto launch a server environment. - Deploy app: Use
eb deployto upload your Rails app. - Manage app: Use
eb opento open your app in a browser.
You also need a Procfile to tell Elastic Beanstalk how to start your Rails server, and configure your database in config/database.yml.
bash
web: bundle exec puma -C config/puma.rb
Example
This example shows how to deploy a simple Rails app to AWS Elastic Beanstalk using the EB CLI.
bash
# 1. Install EB CLI if not installed pip install awsebcli --upgrade --user # 2. Initialize Elastic Beanstalk in your Rails app folder eb init -p ruby-3.1 my-rails-app --region us-east-1 # 3. Create an environment eb create my-rails-env # 4. Deploy your app eb deploy # 5. Open your deployed app in the browser eb open
Output
Creating application version archive "app-230601_123456"...
Uploading my-rails-app/app-230601_123456.zip to S3...
Application version "app-230601_123456" has been created.
Environment details for: my-rails-env
Application name: my-rails-app
Region: us-east-1
Deployed Version: app-230601_123456
Opening http://my-rails-env.us-east-1.elasticbeanstalk.com in your default browser...
Common Pitfalls
Common mistakes when deploying Rails to AWS include:
- Not setting up the
Procfilecorrectly, causing the app not to start. - Forgetting to configure the database connection for production in
config/database.yml. - Not precompiling assets before deployment, leading to missing CSS or JS.
- Using SQLite in production instead of a managed database like Amazon RDS.
Always test your app locally in production mode before deploying.
bash
Wrong Procfile: web: rails server Right Procfile: web: bundle exec puma -C config/puma.rb
Quick Reference
Summary tips for deploying Rails to AWS Elastic Beanstalk:
- Use
eb initto connect your app to AWS. - Use
eb createto launch your environment. - Use
eb deployto push updates. - Configure
Procfileto start Puma server. - Use Amazon RDS for production database.
- Precompile assets with
rails assets:precompile.
Key Takeaways
Use AWS Elastic Beanstalk and EB CLI to simplify Rails deployment.
Always configure a proper Procfile to start your Rails server.
Set up production database settings correctly, preferably with Amazon RDS.
Precompile assets before deploying to avoid missing styles or scripts.
Test your app locally in production mode before deploying to AWS.