Deploy Rails App from Github to AWS Elastic Beanstalk
Tags: aws, github, rails
Published: 2020-09-17 17:00:59

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](https://aws.amazon.com/) 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](####setup-database.yml-config) to setup database config section of this part if you already have your rails app created.

#### Create application
Create a new rails app using
```sh
rails new tuts-api --api --database=postgresql
```

```sh
cd tuts-api
```

let's generate a Post scaffold
```sh
rails g scaffold Post
```
This should generate the following

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/he9qz4kg7d9fnjze1u1k.png)

In create_posts migration file add the following

```ruby
t.string :title
t.string :description
```
![migration file](https://dev-to-uploads.s3.amazonaws.com/i/ahi1g5lw8xc98jq2s70s.png)

#### Run migration
setup db and run migration
```sh
rails db:create && rails db:migrate
```

In your posts_controller, change
```ruby
def post_params
  params.fetch(:post, {}).permit(:title, :description)
end
```

to

```ruby
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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2zp5oys71ulowp1sxfts.png)

#### 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](https://console.aws.amazon.com/elasticbeanstalk/home?region=us-east-1#/welcome)

Click on create a new application
- Enter application name
- Go to Platform section, select Ruby as platform and platform branch (your ruby version)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0rq9mzrziz0li500nuhb.png)
- 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
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ts0xg3b5z9i43mqz2nbq.png)
Copy the value and set it as SECRET_KEY_BASE in the environment properties

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/h0f0cptiyn239llr1a6v.png)

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
![Setup postgres](https://dev-to-uploads.s3.amazonaws.com/i/r0b7hs3dcheznxt1cj1s.png)


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

![Up and running](https://dev-to-uploads.s3.amazonaws.com/i/235wmf38co6e3l5edekj.png)

#### 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

![Deploy with beanstalk](https://dev-to-uploads.s3.amazonaws.com/i/p5ropu1s36qel21noss9.png)

![Successful](https://dev-to-uploads.s3.amazonaws.com/i/ao5x4ohx8rqrlnv6uc8q.png)

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](https://github.com/oluosiname/tuts-api)