The Blackninja Blog
Expand your Cloud Application Development and DevOps knowledge with detailed tutorials and case studies.
This post is going to show you how to deploy a Ruby on Rails application to Elastic Beanstalk on Amazon Web Services using the Elastic Beanstalk Command Line Interface (CLI).
The Elastic Beanstalk CLI (EB CLI) is a high level interface provided by Amazon to work specifically with Elastic Beanstalk Applications and Environments. You may have already installed the AWS CLI, this one is different. You can see all the commands availble using the EB CLI using the EB CLI Command Reference provided by Amazon.
This guide assumes you are familiar with the macOS Terminal and have the mac package manager Homebrew installed. This guide also assumes you have an Elastic Beanstalk Application and Environment setup using Step 1: Configuring an Elastic Beanstalk Environment for a Ruby on Rails Application or on your own.
The EB CLI is installed using python’s pip package manager. This means you need to have python installed on your mac to use it.
The following command will install python 3 to your mac.
brew install python
pip3 install awsebcli --upgrade --user
Once you have finished installing awsebcli you will notice that if you type eb --version
in the Terminal you will get command not found. This is because the awsebcli package was installed to a location that is not being covered by your PATH variable. We will fix that by adding the location to the PATH variable.
pip3 list
in your Terminalpip3 show awsebcli
in your Terminalexport PATH=~/Library/Python/3.7/bin:$PATH
(notice we added /bin to the path - where the eb executable lives)source ~/.bash_profile
eb --version
(you should see the version number)Congratulations! You installed the EB CLI!
Now we are going to configure your Rails Application directory to use a specific Elastic Beanstalk application and environment inside your AWS account. This step assumes you have NOT initialized your application directory with Elastic Beanstalk. Removing the .elasticbeanstalk folder from your application directory would return it to an uninitialized state if that’s what you would like to do.
Part of this configuration involves providing programmatic access to your AWS account using a key and secret. Creating a user with these credentials and ensuring they have the permissions to access Elastic Beanstalk is beyond the scope of this guide. However, you should consider using AWSElasticBeanstalkFullAccess for this user as it gives you everything you need for the EB CLI without creating an Admin user will full access to your AWS account.
eb init
When you deploy your Rails application to Elastic Beanstalk it will run in production configuration by default. In production configuration, Rails looks for an environment variable named SECRET_KEY_BASE. If you don’t set that up in Elastic Beanstalk your Rails application with throw an exception.
rake secret | pbcopy
eb setenv SECRET_KEY_BASE=(paste your secret here)
Running the setenv command may take a moment to finish. You can check the status of your Elastic Beanstalk environment using the command: eb status
. When your configuration update is complete, you can ensure the variable is set using the command: eb printenv
.
One last caveat before you deploy… if your Rails application doesn’t have a default route, then you should add one now.
eb deploy
(this will take some time)eb open
Congratulations! You just deployed your Rails application to Elastic Beanstalk!