Deploy Rails to ECS Fargate with AWS Copilot

What is Copilot?

Copilot is the successor to the Amazon ECS CLI. It is an environment construction tool developed to make container execution in ECS easier. AWS resources built with Copilot are managed by CloudFormation Unlike AWS CDK (a tool that manages CloudFormation templates programmatically), you rarely need to write code!

GitHub - aws/copilot-cli Wiki

Copilot installation and preparation

Can be installed with homebrew or directly with curl

# homebrew
$ brew install aws/tap/copilot-cli

#Install directly with curl
$ curl -Lo /usr/local/bin/copilot https://github.com/aws/copilot-cli/releases/download/v0.3.0/copilot-darwin-v0.3.0 && chmod +x /usr/local/bin/copilot && copilot --help

#Version confirmation
$ copilot -v

Let's prepare other necessary items

Creating an ECS environment with Copilot

Let's actually deploy the web application to ECS using Copilot!

You can refer to the execution status of the deployment and the CloudFormation template to be executed by following the steps below. You can also check the AWS resources created here ** AWS Console> CloudFormation> Stack **

1. Prepare the application

Build the application you want to deploy using docker This time let's deploy this rails app using copilot

https://github.com/git-gen/copilot-rails-deploy

Initial setting

# config/database.yml

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  database: ## RDS database ##
  host: ## RDS host ##
  username: ## RDS username ##
  password: ## RDS password ##

development:
  <<: *default

test:
  <<: *default

production:
  <<: *default
$ docker-compose build

$ docker-compose up -d

If you can confirm it at http: // localhost: 3000, you are ready to go!

It is the mechanism of the application created by Copilot I will create these one by one

--Application: Copilot Application Summary --Enviroment: A mechanism to divide applications by environment such as test environment and production environment --Service: Container application running on ECS

85873795-7da9c480-b786-11ea-9990-9604a3cc5f01.png Environments · aws/copilot-cli Wiki · GitHub

2. Creating a copilot application

Create an application with Copilot At that time, you will be asked for the application name, so let's set it

$ copilot app init
What would you like to name your application? [? for help]
> copilot-app

#Confirm the created application
$ copilot app init

3. Creating a copilot environment

By creating multiple environments, you can divide the environment into a test environment and a production environment. You will be asked to set the VPC / subnet when creating the environment, The default settings will generate a new VPC / subnet / security group

$ copilot env init
What is your environment's name? [? for help]
> development

#environment confirmation
$ copilot env ls

** If you want to use an existing VPC / subnet, add an option like this ** By the way, the security group setting is It seems that Copilot does not support it yet and can not be set

$ copilot env init \
 --import-vpc-id vpc-****** \
 --import-public-subnets subnet-******,subnet-****** \
 --import-private-subnets subnet-******

4. Creating a copilot service

Add rails service I call rails directly from ALB without using nginx etc.

$ copilot svc init

Which service type best represents your service's architecture?  [Use arrows to move, type to filter, ? for more help]
  > Load Balanced Web Service
    Backend Service

What do you want to name this Load Balanced Web Service? [? for help]
  > app

Which Dockerfile would you like to use for app?  [Use arrows to move, type to filter, ? for more help]
  > ./Dockerfile

#service confirmation
$ copilot svc ls

The Dockerfile looks like this Please refer to here for Docker conversion of Rails! https://docs.docker.com/compose/rails/

FROM ruby:2.7.0-alpine3.11

ENV LANG=C.UTF-8
ENV TZ=Asia/Tokyo

RUN apk update && \
  apk upgrade && \
  apk add --no-cache \
  gcc \
  g++ \
  git \
  libc-dev \
  libxml2-dev \
  linux-headers \
  make \
  nodejs \
  mariadb-dev \
  tzdata && \
  apk add --virtual build-packs --no-cache \
  build-base \
  curl-dev

RUN mkdir /app
WORKDIR /app

COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app
RUN apk del build-packs

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

5. Check the configuration file

Now you are ready to deploy! Check that the configuration file is generated in the root directory of the project.

copilot-rails-deploy/
 ├ copilot/
 │ ├ app/
 │ │ └ manifest.yml
 │ └ .workspace
 │...

You can set task specs and constants in manifest.yml ECS sidecar configuration etc. can also be set here

name: app
type: Load Balanced Web Service

image:
  build: ./Dockerfile
  port: 3000

http:
  path: "/"
  healthcheck: "/health_check"

cpu: 1024
memory: 2048
count: 1

variables:
  RAILS_ENV: development

Although it is a task definition spec, The supported numbers are fixed, so if you want to change it, set it within this range.

CPU value Memory value(MiB)
256 (.25 vCPU) 512 (0.5 GB)、1024 (1 GB)、2048 (2 GB)
512 (.5 vCPU) 1024 (1 GB)、2048 (2 GB)、3072 (3 GB)、4096 (4 GB)
1024 (1 vCPU) 2048 (2 GB)、3072 (3 GB)、4096 (4 GB)、5120 (5 GB)、6144 (6 GB)、7168 (7 GB)、8192 (8 GB)
2048 (2 vCPU) 4096 (4 GB) ~ 16384 (16 GB) (1024 (1 GB)Increment)
4096 (4 vCPU) 8192 (8 GB) ~ 30720 (30 GB) (1024 (1 GB)Increment)

6. Deploy

Deploy the app with Copilot

$ copilot deploy

If the deployment is successful, the URL will be written to the command line, If you access and open the rails screen, the deployment is successful! deploy.png

Application removal

AWS resources created with Copilot can be deleted in bulk with the delete command Let's delete the application created this time

#See copilot app
$ copilot app ls
copilot-app

#Delete the app
$ copilot app delete copilot-app
Are you sure you want to delete application copilot-app? Yes

Recommended Posts

Deploy Rails to ECS Fargate with AWS Copilot
Deploy to heroku with Docker (Rails 6, MySQL)
Rails deploy with Docker
How to deploy Java to AWS Lambda with Serverless Framework
[AWS] How to automatically deploy a Web application created with Rails 6 to ECR / ECS using CircleCI ① Preparation [Container deployment]
[For super beginners (more screenshots)] Automatically deploy to AWS ECR / ECS with Ruby2.6 x Rails6 x CircleCi [Hands-on format]
How to deploy a kotlin (java) app on AWS fargate
How to deploy a Rails application on AWS (article summary)
How to deploy jQuery on Rails
Deploy to EC2 with CircleCi + Capistrano
Connect to Rails server with iPhone
How to get along with Rails
Deploy Rails on Docker to heroku
Deploy SpringBoot application to AWS EC2
Introducing React to Rails with react-rails
How to deploy to AWS using NUXTJS official S3 and CloudFront? With docker-compose
Easy deployment with Capistrano + AWS (EC2) + Rails
[Rails] How to use rails console with docker
Knowledge required to bring Rails apps to AWS
Deploy to Heroku [Ruby on Rails] Beginner
Try deploying Rails app to EC2-Part 2 (Deploy)-
Getting Started with Micronaut 2.x ~ Native Build and Deploy to AWS Lambda ~
[AWS] Publish rails app with nginx + puma
How to build Rails 6 environment with Docker
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
How to deploy a container on AWS Lambda
[Rails] rails new to create a database with PostgreSQL
Deploy Java web app to Azure with maven
How to use Java framework with AWS Lambda! ??
One way to redirect_to with parameters in rails
I want to play with Firestore from Rails
[Rails] How to easily implement numbers with pull-down
How to build API with GraphQL and Rails
Rails beginners tried to get started with RSpec
Error deploying rails5 + Mysql to heroku with Docker-compose
[Rails] How to build an environment with Docker
Try to summarize the common layout with rails
Deploy Line bot with rails5 + Docker + Heroku Note
[Rails] I want to load CSS with webpacker
Easy to display hello world with Rails + Docker
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (5)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (6)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (3)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (2)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (1)
[Part 1] How to deploy Docker containers and static files with CircleCI + ECS + ECR + CloudFront
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (4)
[Part 2] Automatically test and deploy WEB services created with Rails + Nuxt + MySQL + Docker with ECS / ECR / CircleCI to make them terraform
How to make batch processing with Rails + Heroku configuration
[Rails] How to search by multiple values ​​with LIKE
Submit a job to AWS Batch with Java (Eclipse)
[Introduction to Docker x ECS] ECS deployment with docker compose up
How to push an app developed with Rails to Github
[Heroku] Associate AWS S3 with the deployed Rails app
Run Rubocop and RSpec on CircleCI and deploy to ECS
How to delete a new_record object built with Rails
How to make an almost static page with rails
How to manually generate a JWT with Rails Knock
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
I want to authenticate users to Rails with Devise + OmniAuth
[How to insert a video in haml with Rails]