LoginSignup
3
1

More than 3 years have passed since last update.

copilot betaを触ってみる

Posted at

ecs-cliの第二世代、copilotのベータが出ていることは知っていたが、大々的にブログで紹介されていたのと、担当プロジェクトで近い将来採用される可能性があるので、試してみる。

やりたいこと

  • copilotベータを触って、理解する

Step by Step

  1. インストールする。ecs-cli同様、ワンライナーで完了。
% curl -Lo /usr/local/bin/copilot https://github.com/aws/copilot-cli/releases/download/v0.1.0/copilot-darwin-v0.1.0 && \
  chmod +x /usr/local/bin/copilot && \
  copilot --help
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   634  100   634    0     0   1690      0 --:--:-- --:--:-- --:--:--  1686
100 37.9M  100 37.9M    0     0  5482k      0  0:00:07  0:00:07 --:--:-- 7683k
👩‍✈️ Launch and manage applications on Amazon ECS and AWS Fargate.

Commands
  Getting Started 🌱
    init        Create a new ECS application.
    docs        Open the copilot docs.

  Develop ✨
    app         Commands for applications.
                Applications are a collection of services and environments.

    env         Commands for environments.
                Environments are deployment stages shared between services.

    svc         Commands for services.
                Services are long-running Amazon ECS services.

  Release 🚀
    pipeline    Commands for pipelines.
                Continuous delivery pipelines to release services.

    deploy      Deploy your service.

  Settings ⚙️
    version     Print the version number.
    completion  Output shell completion code.

Flags
  -h, --help      help for copilot
  -v, --version   version for copilot

Examples
  Displays the help menu for the "init" command.
  `$ copilot init --help`

ecs-cliとはサブコマンドが随分様変わりしている。
envとか、どちらかというとamplify CLIに近いかも?

  1. Dockerファイルを用意

以前ecs-cliのテストの際に使ったものをそのまま使う。

FROM centos

RUN yum update -y \
    && yum install httpd php -y \
    && echo "Hello from httpd" > /var/www/html/index.html \
    && echo "<?php phpinfo(); " > /var/www/html/index.php \
    && yum clean all

EXPOSE 80

ENTRYPOINT ["/usr/sbin/httpd", "-DFOREGROUND"]

~/ECS/copilotというディレクトリを作り、ここにDockerfileを配置。

  1. 初期化する

上記ディレクトリでinitコマンドを実行する。

% copilot init
Note: It's best to run this command in the root of your Git repository.
Welcome to the Copilot CLI! We're going to walk you through some questions
to help you get set up with an application on ECS. An application is a collection of
containerized services that operate together.

Application name: copilot-test
Service type: Load Balanced Web Service
Service name: copilot-webservice
✘ ask svc init: no Dockerfiles found within . or a sub-directory level below

失敗。何かと思ったら、Dockerfileの先頭が小文字(dockerfile)だった。
気を取り直して再実行。

copilot init
Note: It's best to run this command in the root of your Git repository.
Welcome to the Copilot CLI! We're going to walk you through some questions
to help you get set up with an application on ECS. An application is a collection of
containerized services that operate together.

Application name: copilot-test
Service type: Load Balanced Web Service
Service name: copilot-webservice
Dockerfile: ./Dockerfile
Ok great, we'll set up a Load Balanced Web Service named copilot-webservice in application copilot-test listening on port 80.

✔ Created the infrastructure to manage services under application copilot-test.

✔ Wrote the manifest for service copilot-webservice at copilot-webservice/manifest.yml
Your manifest contains configurations like your container size and port (:80).

✔ Created ECR repositories for service copilot-webservice.

All right, you're all set for local development.

copilot-testという名前の、ALB-backedなappができた。ECRレポジトリも作ってくれたらしい。

スクリーンショット 2020-07-10 12.52.55.png

確かにいる。AWS CLIのプロファイルを読んでくれるようなので、既にaws configureでセットアップ済みの環境だと随分楽。

  1. テスト環境にデプロイ

そのままcopilotから「テスト環境にデプロイする?」と聞かれたので、yを選択して進んでみる。

Deploy: Yes

✔ Created the infrastructure for the test environment.
- Virtual private cloud on 2 availability zones to hold your services     [Complete]
- Virtual private cloud on 2 availability zones to hold your services     [Complete]
  - Internet gateway to connect the network to the internet               [Complete]
  - Public subnets for internet facing services                           [Complete]
  - Private subnets for services that can't be reached from the internet  [Complete]
  - Routing tables for services to talk with each other                   [Complete]
- ECS Cluster to hold your services                                       [Complete]
- Application load balancer to distribute traffic                         [Complete]
✔ Linked account 0XXXXXXXXXX1 and region ap-northeast-1 to application copilot-test.

✔ Created environment test in region ap-northeast-1 under application copilot-test.
Sending build context to Docker daemon  65.02kB
Step 1/4 : FROM centos
 ---> 831691599b88
Step 2/4 : RUN yum update -y     && yum install httpd php -y     && echo "Hello from httpd" > /var/www/html/index.html     && echo "<?php phpinfo(); " > /var/www/html/index.php     && yum clean all
 ---> Using cache
 ---> 562fe7bfcb7e
Step 3/4 : EXPOSE 80
 ---> Using cache
 ---> 4fe58b868bcb
Step 4/4 : ENTRYPOINT ["/usr/sbin/httpd", "-DFOREGROUND"]
 ---> Using cache
 ---> 54ba23bdc480
Successfully built 54ba23bdc480
Successfully tagged 016831194521.dkr.ecr.ap-northeast-1.amazonaws.com/copilot-test/copilot-webservice:5e3bc3e
Login Succeeded
The push refers to repository [016831194521.dkr.ecr.ap-northeast-1.amazonaws.com/copilot-test/copilot-webservice]
a81bf2899a05: Pushed
eb29745b8228: Pushed
5e3bc3e: digest: sha256:ee1d990b9fce495fe9338c5b6c047d5fe7d6f6c2ec5ecb9ac70f398bb71d4de4 size: 741


✔ Deployed copilot-webservice, you can access it at http://copil-Publi-18XGAVCRBAQVU-300840140.ap-northeast-1.elb.amazonaws.com.

全部できてしまった模様。
インストール開始から2コマンド(+Dockerfileコピー)でここまで辿り着いてしまった。

Fargateクラスター。
スクリーンショット 2020-07-10 13.01.05.png

サービス定義。
スクリーンショット 2020-07-10 13.03.08.png

なんと、Route 53プライベートホステッドゾーンとAレコードまで。。。
Descriptionを見ると、CloudMapが裏で動いたようだ。

スクリーンショット 2020-07-10 13.07.04.png

  1. CLIをいくつか叩いてみる。
% copilot svc ls
Name                Type
------------------  -------------------------
copilot-webservice  Load Balanced Web Service

% copilot svc status
Showing status of service copilot-webservice deployed in environment test
Service Status

  ACTIVE 1 / 1 running tasks (0 pending)

Last Deployment

  Updated At        31 minutes ago
  Task Definition   arn:aws:ecs:ap-northeast-1:01XXXXXXXXX1:task-definition/copilot-test-test-copilot-webservice:1

Task Status

  ID                Image Digest        Last Status         Health Status       Started At          Stopped At
  b3407406          ee1d990b            RUNNING             UNKNOWN             32 minutes ago      -

Alarms

  Name              Health              Last Updated        Reason

「テスト」環境でタスクがデプロイされて稼働していることが分かる。

% copilot svc show
About

  Application       copilot-test
  Name              copilot-webservice
  Type              Load Balanced Web Service

Configurations

  Environment       Tasks               CPU (vCPU)          Memory (MiB)        Port
  test              1                   0.25                512                 80

Routes

  Environment       URL
  test              http://copil-Publi-18XXXXXXXXXXU-300840140.ap-northeast-1.elb.amazonaws.com

Service Discovery

  Environment       Namespace
  test              copilot-webservice.copilot-test.local:80

Variables

  Name                                Environment         Value
  COPILOT_APPLICATION_NAME            test                copilot-test
  COPILOT_ENVIRONMENT_NAME            test                test
  COPILOT_LB_DNS                      test                copil-Publi-18XXXXXXXXXXU-300840140.ap-northeast-1.elb.amazonaws.com
  COPILOT_SERVICE_DISCOVERY_ENDPOINT  test                copilot-test.local
  COPILOT_SERVICE_NAME                test                copilot-webservice

より細かいサービスの情報はshowで取れる模様。
Fargate、ALB、Route 53、CloudMap等々が、こびとさんのごとく働いてくれているのが見て取れる。

所感

copilot initだけで、関連リソースの作成からコンテナの立ち上げまで全部終わってしまった。なかなかの衝撃。
今回は雑に動かしたのでVPCなども雑に作成されたが、丁寧に作ればその辺もきっと綺麗にやってくれるんだろう。

コンテナでアプリを動かしたい、という目的がはっきりしている場合は、CloudFormationやAWS CLI、ましてやマネジメントコンソールでどうこうするより、こっちの方が全然楽。ecs-cliも楽だったが、さらにモダナイズされていて、先にも書いたがAmplifyに近い印象を受けた。

インフラエンジニアとしては、他のもっと難しい領域に時間と労力を使えるのが嬉しいところ。
CloudFormation書くのも、なかなか面倒なので。。。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1