Deploy Ruby On Rails Application To AWS Elastic Beanstalk (Amazon Linux 2)

Ben Tran
5 min readJun 4, 2021

This guide mainly focus on practicing deployment Ruby On Rails application to AWS Elastic Beanstalk, don't have much explanation. So you need to understand basically about AWS EB infrastructure and have enough Ruby On Rails knowledge.

I have added some GIF images — a little bit heavy, please wait a while to see them. You can also go to image url on caption to load image quicker.

What is AWS Elastic Beanstalk (AWS EB)?

Before starting deployment, please read below links to know about AWS EB infrastructure:

Setup CLI & AWS Profile Credentials

Install awscli & awsebcli in MacOS

brew update
brew install awscli
brew install awsebcli

Config aws profile credentials & region

Note: We should config aws profile by project name. In this post, my project called merlin so please replace it with your project name in all shell script and source code.

aws configure --profile merlin

Your need to enter your account credentials Access key ID, Secret access key, Region in each prompt from AWS CLI in your terminal.

Create AWS EB Application & Environment

You must have an AWS root account or AWS IAM account. If you use AWS IAM account, please make sure that your IAM account have AdministratorAccess-AWSElasticBeanstalk , AmazonEC2FullAccess policies.

Access to AWS Console, go to Elastic Beanstalk then create a new application.

Note: Choose correct region before creating the application, currently i use ap-southeast-1 region (Singapore) to run my application.

Create AWS EB application & environment — https://cdn-images-1.medium.com/max/800/1*u8Gjh5oLjABkqI8XrW6sBA.gif

After click on Create Environment, take a coffee cup and wait util they are built completely. If everything go well, you will have the environment dashboard like this:

Environment dashboard after created successfully

At this time, you can visit the default deployed website from AWS EB. Check it by the link in the red box in above image.

Default deployed site from AWS EB

Note: If your application ruby version is different from latest version in AWS EB, you must update it to match with the version in AWS EB.

Next, we go to make some changes in the application environment config.

Add RAILS_MASTER_KEY & others environment variables if you have:

Add environment variables — https://cdn-images-1.medium.com/max/800/1*E4sTuDTOlb21gpJuaZe2BA.gif

Modify auto scaling capacity & instance type (for demo purposes, i use only one t2.micro instance):

Modify auto scaling capacity and instance type — https://cdn-images-1.medium.com/max/800/1*jhDN-hcIHbwYSZ_VGiS0DQ.gif

Add Postgres Database with AWS RDS (for demo purposes, i use only t2.micro)

Add postgres database with AWS RDS — https://cdn-images-1.medium.com/max/800/1*3x9CWreCMZB_x9vHui3ahQ.gif

Add AWS EB Deploy Scripts To Rails Application

Init AWS EB configuration

cd /path/to/project
eb init --profile merlin
eb init — https://cdn-images-1.medium.com/max/800/1*ttMivrkxstOLJ9IsUaE51w.gif

That will generate new file in .elasticbeanstalk/config.yml include detail about aws eb application environment, also update your .gitignore so please commit changes.

Config linux swap & webapp directory

Create file .ebextensions/system.config

Database configuration

Create file config/database.yml.eb

Note: RDS_DB_NAME, RDS_USERNAME, RDS_PASSWORD, RDS_HOSTNAME, RDS_PORT are set automatically after you add AWS RDS to AWS EB environment, so you don’t need to add them again in AWS EB Configuration.

Create file .platform/hooks/prebuild/01_config_database.sh

Install NodeJS & Yarn

Create file .platform/hooks/prebuild/02_install_node_and_yarn.sh

Yarn install

Create file .platform/hooks/prebuild/03_install_packages.sh

Add nginx config for packs

By default, AWS EB has already configured nginx completely for basic rails application. With Rails 6 we need to modify nginx config a little bit to serve all packs sources.

Create file .platform/hooks/nginx/conf.d/elasticbeanstalk/webpack.conf

Add nginx config for ActionCable

Create file .platform/hooks/nginx/conf.d/elasticbeanstalk/cable.conf

Important: Give permission for all files in .platform/hooks/*/*.sh

We mustset permission for all *.sh from our scripts, that’s required for AWS EB instance can execute them.

Finally, git commit changes before deploy. AWS EB will zip the source code in latest git commit so please remember to commit all changes what you want to deploy.

git add .
git commit -m 'Add basic aws eb deploy config'

Do deployment

Run this command from application root path to start the deployment

eb deploy [environment-name] --profile [aws-profile]

So, for my application i need to run

eb deploy merlin-production --profile merlin

After deploy successfully you will see some things like this

Deploy successfully messages

If there is any errors while deployment, you can:

  • Check the errors from AWS EB environment dashboard from AWS Console.
  • Check AWS EB activities errors from log files: /var/log/eb-engine.log, /var/log/eb-hooks.log, /var/log/cfn-init.log

SSH To AWS EB Instances

Setup ssh key (do it only for first time)

eb ssh [environment-name] --profile [aws-profile] --setup

It should be take a while to re-init instance. After setup completed, you can ssh to instances by

eb ssh [environment-name] --profile [aws-profile]

Your application is located at /var/app/current

Logs

EB activities:

  • /var/log/eb-engine.log
  • /var/log/eb-hooks.log
  • /var/log/cfn-init.log

Nginx:

  • /var/log/nginx/error.log
  • /var/log/nginx/access.log

Puma:

  • /var/log/puma/puma.log

Rails:

  • /var/app/current/log/production.log

Rails Console

SSH to AWS EB instances then

Well, that's all for basic Rails production deployment to AWS EB. For more complex Rails application with Redis — Sidekiq, Whenever, … Please follow my profile to get updates for them.

Related Resources & Contact

Read more tech posts from https://goldenowl.asia/blog. And fill in https://goldenowl.asia/contact-us if you want to hire me! ✌✌️

--

--