This starter application and attached guide is perfect for launching a Ruby on Rails application
into elastic beanstalk and configuring it to utilize an RDS PostgreSQL instance, S3 for secure
token/password storage, and configuring top to bottom SSL. This example also uses rails 5.1 and includes yarn
in the
deployment. This means that webpack can be used to create Angular, React, Vue, or other modern Javascript
front-ends.
This example has been created using ActiveRecord as a session store, but could trivially be modified to use Redis or another solution.
git clone https://github.com/xrnm/rails-elastic-beanstalk.git
From the app directory run eb init
There is a guided setup process
- Select a region
- Create an application
- Select Ruby 2.3 (Puma)
- Choose to use code commit or not
- Setup SSH if you would like
This will create a file .elasticbeanstalk/config.yml
In order to deploy you will need to setup a few things in AWS
- Create or select an RDS instance to use
- Create an S3 bucket to hold your configuration files.
- Create an IAM role for your environment. This is an EC2 role which should have
the
AWSElasticBeanstalkWebTier
,AWSElasticBeanstalkMulticontainerDocker
, andAWSElasticBeanstalkWorkerTier
policies - Add a policy to your new IAM role to read the bucket you just created.
You can use the AWS UI or the below JSON policy. If using the policy JSON Be sure to replace
<bucket>
with your S3 bucket's name - Ensure your RDS instance is accessible
Policy JSON:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "s3:Get*",
"Resource": [
"arn:aws:s3:::*/*",
"arn:aws:s3:::<bucket>"
]
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": "s3:List*",
"Resource": [
"arn:aws:s3:::*/*",
"arn:aws:s3:::<bucket>"
]
}
]
}
- Create a database.yml file that contains the endpoint and credentials for your new or existing RDS instance
- Create your application.yml file to hold any environment variables of interest.
rails-elastic-beanstalk
comes withfigaro
installed - Create a secrets.yml file. Use may use
rake secret
to generate a new token - Find or create your SSL key and certificate for your desired domain
- Upload all of these files to your new S3 bucket
This configuration file executes during deployment and pulls important files from S3 so sensitive data does not have to be stored in the repository. The 5 files that are stored in S3 for this starter application are:
- SSL Key
- SSL Certificate
- application.yml - May contain API keypairs or other sensitive data.
- database.yml - Will contain access credentials to RDS
- secrets.yml - Contains a secret used by Rails for cookie verification
In .ebextensions/01_files.config
overwrite the placeholders with your bucket name, region, and the role you created in step 3
Once all of that is completed, from the application root directory commit all of your changes
and execute the command eb create <environment> -ip <role>
You can pick any environment name that serves your purposes
but <role>
must be the IAM role you created in step #2
Once the deployment completes do
eb open
Optionally, add a DNS rule to give your site a nice domain name
Copyright (c) 2018 Justin Edwards
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.