Deploy Rails App image in serverless way using AWS Fargate

Introduction to AWS ECS Fargate

  1. Networking Only is the serverless way to deploy your application without having to manage servers or clusters. This is powered by Fargate
  2. EC2 Linux + Networking is a highly scalable, high-performance container orchestration service that supports Docker containers and allows you to easily run and scale containerized applications on AWS. You will need to manage EC2.
  3. EC2 Windows + Networking similar to #2 but instead of Linux AMI it will create Windows OS AMI.

Getting Started

High level flow for our deployment on Fargate
  • AWS Account
  • RDS Postgres DB
  • Elastic Container Registry (ECR) in order for us to push our image in repository
  • ECS Fargate
  1. Create AWS Account with a user with right permissions similar to this:
It’s not necessary to have FullAccess for ECS and ECR but this to simplify this tutorial.
➜  ~ aws configure
AWS Access Key ID [****************IFZS]:
AWS Secret Access Key [****************LjSB]:
Default region name [ap-southeast-1]:
Default output format [None]:

Setting up ECR

  1. Create an ECR Repository via terminal.
➜  ~ aws ecr create-repository --repository-name rails_docker
➜  ~ $(aws ecr get-login --no-include-email)
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Login Succeeded
➜  ~ docker build -t production .
➜  ~ docker tag rails_docker:latest <repositoryUri-from-step-1>
➜  ~ docker push <repositoryUri-from-step-1>

Setting up RDS

  1. Go to AWS RDS and create a database. We will be using Postgres as our production DB.
  2. Templates will be Free Tier just for this tutorial. Select Production if you are going to deploy production app.
  3. DB instance size is db.t2.micro since we selected Free Tier template
  4. For the remaining items just choose the default selections and create database.
  5. After the DB instance has been provisioned. Select the DB instance and copy the Endpoint under Connectivity & Security Tab.
Change the source to anywhere or specify the IP address from where are you going to create the DB
default: &defaultadapter: postgresqlencoding: unicodepool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
production: <<: *default database: <%= Rails.application.credentials.database_name %> username: <%= Rails.application.credentials.database_username %> password: <%= Rails.application.credentials.database_password %> host: <%= Rails.application.credentials.database_endpoint %> port: 5432
➜  ~ rake db:create db:migrate RAILS_ENV=production

Setting up ECS Fargate

  1. Create a task definition under AWS ECS Console by clicking “Create new task definition”
General config for our container in Task Definition
Environment Variables
  • Name your cluster e.g. rails-docker-cluster. No need to specify VPC and Tags
  • configure network by adding default VPC.
  • For subnets just select one of available subnets under your region. In our case ap-southeast-1
  • Set load balancer to none
  • For auto scaling we will not specify
  • Review and create your service
For task definition, select the task definition we created earlier.
  • Go to your cluster
  • Click Tasks tab
  • Select the created task
  • In network click ENI-Id
  • Access the IPv4 public IP in your browser. e.g http://3.0.XX.XX/
  • If there are any errors you can check the logs under tasks tab then created task.

Moving forward

  • Access rails console using ssh. This will be interesting because ECS Fargate doesn’t create EC2 since it’s serverless
  • Integration of rails docker with SQS
  • Orchestrate using AWS EKS cluster



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Allen Oliver M Chun

Allen Oliver M Chun

Full Stack Software Developer based in Singapore. Software Developer since 2012. Specialized in Ruby on Rails, Vue.js. AWS Development.