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!
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
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 **
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
Environments · aws/copilot-cli Wiki · GitHub
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
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-******
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"]
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) |
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!
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