Olumuyiwa Osiname
Posted on September 17, 2020
I have mostly always deployed my Rails and Express applications to Heroku but for my new project I wanted to try something different, so I decided to deploy to Elastic Beanstalk.
For this tutorial we're going to need an aws account so head on to amazon and create one if you don't and if you do, login.
Setup rails App
Let's quickly setup our small rails app. Because I am lazy, this is going to be a rails API, we would be using postgres as our database.
Note: You can skip to setup database config section of this part if you already have your rails app created.
Create application
Create a new rails app using
rails new tuts-api --api --database=postgresql
cd tuts-api
let's generate a Post scaffold
rails g scaffold Post
This should generate the following
In create_posts migration file add the following
t.string :title
t.string :description
Run migration
setup db and run migration
rails db:create && rails db:migrate
In your posts_controller, change
def post_params
params.fetch(:post, {}).permit(:title, :description)
end
to
def post_params
params.require(:post).permit(:title, :description)
end
Setup database.yml config
Go over to our local environment and head over to our database.yml file
Change the production section to look like this.
Github
Add Commit and push to Github(you should create a repo and set it as remote, this is beyond the scope of this tutorial, You can find a link to my github repo at the bottom)
We're done with Rails part for now.
Create Elastic beanstalk application
Now go to your aws console
Click on create a new application
- Enter application name
- Go to Platform section, select Ruby as platform and platform branch (your ruby version)
- Click on configure more options
Setup environment variables
In software section, click Edit.
Scroll down to Environment properties
Set RAILS_ENV as production
Go to your rails app on your laptop and generate a secret using rake secret on your terminal
Copy the value and set it as SECRET_KEY_BASE in the environment properties
Save
Add Postgres database
Next we'll configure a database
Scroll down to database section and click on Edit
Select postgres as database engine
set database username and password, then save
Now we click on create app to create our application. This will create an EC2 instance, RDS instance and other things for us. This will take about 10 minutes so let's quickly take a drink break, we deserve it.
OK! Our application and environment should be done by now, let's link our github repo to our application environment
Deploy with github
Navigate to codepipeline service and create a new pipeline
Set Pipeline name and then Next
Select Github as source provider and connect your github account. Select your repository and preferred branch and then Next. (Skip the build stage.)
Select AWS Elastic Beanstalk as Deploy provider, select your application name and application environment like so
Click on Next and then Create Pipeline. This will deploy our github source code to our application environment and also ensure that subsequent commits to master are deployed as well.
You can navigate back to your application to get your application url and test using postman or curl.
You can find my source code here
Posted on September 17, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.