RAILS API ON AWS – PART 5: AWS setup & deploy

Post presents how to setup ECR, Elastic Beanstalk, API Gateway and deploy Rails application to AWS.

 

List of Posts:

  1. part 1: Local server setup
  2. part 2: Basic Rails App with PostGIS
  3. part 3: Docker
  4. part 4: AWS setup
  5. part 5: AWS setup & deploy

 

 

  1. Introduction
  2. Update project
  3. AWS CLI
  4. ECR
  5. Setup Elastic Beanstalk
  6. Deploy App to Elastic Beanstalk

 

1. Introduction

We will start with building Rails project along with NGINX and store Docker Images on AWS ECR. Next, we prepare our project to deploy to Elastic Beanstalk and connect with RDS. After that, we test if everything goes well so far. In the last step, we setup API Gateway.

 

2. Update project

Firstly, we have update configuration of our project. When we deploy the application on AWS, Rails app will be connected to RDS instance, instead of local PostGIS container.

Update production settings in app/config/database.yml:

Optionally, we can also update docker-compose.yml to use the same Environment Variables. However, we don’t need this file to upload our application to AWS Elastic Beanstalk.

 

 

3. AWS CLI

 

Install

To upload Docker images to AWS ECR, we need to install AWS CLI. But first, we need PIP to install AWS CLI:

Add to ~/.bashrc:

And run:

Test if PIP is installed:

Next, we install AWS CLI with PIP:

 

Configure

 

 

4. ECR

 

Build local images

First, we have to build docker images locally:

Preview docker images:

 

Setup ECR and push images

  1. Go to Services > ECS (EC2 Container Service)
  2. Click Get started
  3. Cancel Wizard.
  4. You should see now ECS Dashboard and on the left pane following options: Clusters, Task Definitions and Repositories. Click on Repositories
  5. Get Started
  6. Configure Repository:
    1. Give a name, eg.: raoa_app
  7. Run following to log in to ECR:
  8. Tag and push image:

    If you get the error no basic auth credentials, run again command from step 7, copy output, paste into the terminal and run. At the end of the output, you should see “Login succeeded”.

Now you can check if the image was uploaded successfully. Go to AWS Console > ECS > Repositories and click on the repository.

We have uploaded Rails app image and now we have to repeat this for NGINX image:

  1. Go to Services > ECS > Repositories
  2. Create Repository
  3. Give a name, e.g.: raoa_proxy
  4. Tag and push image:

     

5. Setup Elastic Beanstalk

  1. Go to Services > Elastic Beanstalk
  2. Get started
  3. Create a web app:
    1. Application name, eg.: railsapionaws
    2. Platform: Multi-container Docker
    3. Select Sample application
  4. Click Configure more options

    1. Software
      1. Add environment variables:
        RAOW_DB_USER_AWS: xxx
        RAOW_DB_PASS_AWS: yyy
        RAOW_DB_HOST_AWS: zzz.eu-central-1.rds.amazonaws.com
    2. Network
      1. VPC – select our VPC
      2. Select Public subnets
  5. Click Create App

For real production deployment, we should configure more options in step 4.
Elastic Beanstalk needs a few minutes to setup everything. For now, it setups a sample application. We can check it by clicking on URL in following format: <app-name>.<region>.elasticbeanstalk.com.

 

6. Deploy App to Elastic Beanstalk

Firstly, we have to create Dockerrun.aws.json file which we will upload later on Elastic Beanstalk.

To keep everything in one place, create in our Rails project a new folder and file:

Dockerrun.aws.json:

Replace highlighted lines with your data. image is a Repository URI, which you can find under: Services > ECS > Repositories and then clicking on specific repository.

 

Additional AWS setup

Add ECR Policy to Elastic Beanstalk EC2 role

Elastic Beanstalk will download images from ECR, so we have to give access to ECR. Go to: Services > IAM > Roles tab and select role assigned to Elastic Beanstalk’s EC2s. Default name should be: aws-elasticbeanstalk-ec2-role. On Role Summary, select Permissions tab and click Attach policy. Search AmazonEC2ContainerRegistryReadOnly and attach policy.

 

Allow EB’s EC2 instances to connect to RDS 

EC2 probably are not able yet to connect to RDS. To give them an access, go to: Services > EC2 > Security Groups tab and select RDS security group. Next, in Security Group details, select Inbound tab and click Edit. Add new Rule with following params:
Type: PostgreSQL
Source: Custom – sg-xxxx (AWSEBSecurityGroup)

 

 

Deploy

To deploy our application, go to: Services > Elastic Beastalk > Select application and click Upload and deploy. In modal window, select file and give unique version label. Wait a few minutes and observe Recent Events table.

 

 

 

Tomasz Antas

Ruby on Rails developer and Web designer.
The area of his interest includes Popular Science, Internet of Things, Wearables, AI and Virtual/Augmented Reality.

Latest posts by Tomasz Antas (see all)

Thanks for reading!