(途中で断念)AWS ECS Workshopをやってみる(copilot編)

11 min read

教材

AWS ECS Workshop

必要なもの

必須

あったほうがいい

実施手順が用意されているリージョン

Cloud9を起動していくのですが手順が用意されているのは以下のリージョンでした。

実際にやってみた

copiliot initコマンドを初めて入力

Image from Gyazo

最後のtest環境へのデプロイを実行した際、
Docker Imageの作成を含めて一度目は失敗したんですが、
もう一度同じコマンドを入力していくと成功しました。

  1. Application name: ecsworkshop
  2. Service Type: Load Balanced Web Service
  3. What do you want to name this Load Balanced Web Service: ecsdemo-frontend
  4. Dockerfile: ./Dockerfile
  5. Would you like to deploy a test Enviorment?: y

ログバッファが流れてしまって拾えないんですが途中で、
apt-get ~ cannot foundが表示されたような...

続けます。copilot appのコマンドは以下で、

> copilot app
Commands for applications.
Applications are a collection of services and environments.

Usage
  copilot app [command]

Available Commands
  init        Creates a new empty application.
  ls          Lists all the applications in your account.
  show        Shows info about an application.
  delete      Delete all resources associated with the application.

Flags
  -h, --help   help for app

実際に作りたての情報を確認すると、

$ copilot app ls
ecsworkshop
$ 
$ copilot app show ecsworkshop
About

  Name              ecsworkshop
  Version           v1.0.1 
  URI               

Environments

  Name              AccountID           Region
  ----              ---------           ------
  test              ************        ap-northeast-1

Services

  Name              Type
  ----              ----
  ecsdemo-frontend  Load Balanced Web Service

Pipelines

  Name
  ----
$ 
$ copilot env show -n test
About

  Name              test
  Production        false
  Region            ap-northeast-1
  Account ID        005680786868

Services

  Name              Type
  ----              ----
  ecsdemo-frontend  Load Balanced Web Service

Tags

  Key                  Value
  ---                  -----
  copilot-application  ecsworkshop
  copilot-environment  test
$ copilot svc status -n ecsdemo-frontend
Service ecsdemo-frontend found in environment test
Service Status

  ACTIVE 1 / 1 running tasks (0 pending)

Last Deployment

  Updated At         16 minutes ago
  Task Definition    arn:aws:ecs:ap-northeast-1:**********:task-definition/ecsworkshop-test-ecsdemo-frontend:1

Task Status

  ID                Image Digest        Last Status         Started At          Stopped At          Capacity Provider    Health Status
  --                ------------        -----------         ----------          ----------          -----------------    -------------
  b7173b70          76528dc9            RUNNING             17 minutes ago      -                   -                    UNKNOWN

Alarms

  Name              Condition           Last Updated        Health
  ----              ---------           ------------        ------
$ 
$ cat ./copilot/ecsdemo-frontend/manifest.yml | grep count
count: 1       # Number of tasks that should be running in your service.
#    count: 2               # Number of tasks to run for the "test" environment.
$ 

count: 3に更新して copilot svc deploy すると、

$ copilot svc status -n ecsdemo-frontend
Service ecsdemo-frontend found in environment test
Service Status

  ACTIVE 3 / 3 running tasks (0 pending)

Last Deployment

  Updated At         28 seconds ago
  Task Definition    arn:aws:ecs:ap-northeast-1:005680786868:task-definition/ecsworkshop-test-ecsdemo-frontend:2

Task Status

  ID                Image Digest        Last Status         Started At          Stopped At          Capacity Provider    Health Status
  --                ------------        -----------         ----------          ----------          -----------------    -------------
  4406cf41          47085b7f            RUNNING             4 minutes ago       -                   -                    UNKNOWN
  df7997ea          47085b7f            RUNNING             3 minutes ago       -                   -                    UNKNOWN
  f795e0e7          47085b7f            RUNNING             4 minutes ago       -                   -                    UNKNOWN

Alarms

  Name              Condition           Last Updated        Health
  ----              ---------           ------------        ------

ログも見れるの(何かエラー出している...

$ copilot svc logs -a ecsworkshop -n ecsdemo-frontend --follow
Service ecsdemo-frontend found in environment test
copilot/ecsdemo-frontend/   bin/spring:13:in `<top (required)>'
copilot/ecsdemo-frontend/   bin/rails:3:in `load'
copilot/ecsdemo-frontend/   bin/rails:3:in `<main>'
copilot/ecsdemo-frontend/ I, [2021-05-15T07:18:53.021290 #1]  INFO -- : Started GET "/" for 10.0.0.27 at 2021-05-15 07:18:53 +0000
copilot/ecsdemo-frontend/ I, [2021-05-15T07:18:53.022243 #1]  INFO -- : Processing by ApplicationController#index as HTML
copilot/ecsdemo-frontend/ E, [2021-05-15T07:18:53.022698 #1] ERROR -- : bad argument (expected URI object or URI string)
copilot/ecsdemo-frontend/ E, [2021-05-15T07:18:53.022765 #1] ERROR -- : bad argument (expected URI object or URI string)
copilot/ecsdemo-frontend/ I, [2021-05-15T07:18:53.023377 #1]  INFO -- :   Rendered application/index.html.erb within layouts/application (0.1ms)
copilot/ecsdemo-frontend/ I, [2021-05-15T07:18:53.023631 #1]  INFO -- : Completed 200 OK in 1ms (Views: 0.7ms | ActiveRecord: 0.0ms)
copilot/ecsdemo-frontend/ I, [2021-05-15T07:18:53.027866 #1]  INFO -- : Started GET "/" for 10.0.1.223 at 2021-05-15 07:18:53 +0000

logsコマンドのオプション

$ copilot svc logs -h
Displays logs of a deployed service.

Usage
  copilot svc logs [flags]

Flags
  -a, --app string          Name of the application. (default "ecsworkshop")
      --end-time string     Optional. Only return logs before a specific date (RFC3339).
                            Defaults to all logs. Only one of end-time / follow may be used.
  -e, --env string          Name of the environment.
      --follow              Optional. Specifies if the logs should be streamed.
  -h, --help                help for logs
      --json                Optional. Outputs in JSON format.
      --limit int           Optional. The maximum number of log events returned. Default is 10
                            unless any time filtering flags are set.
  -n, --name string         Name of the service.
      --since duration      Optional. Only return logs newer than a relative duration like 5s, 2m, or 3h.
                            Defaults to all logs. Only one of start-time / since may be used.
      --start-time string   Optional. Only return logs after a specific date (RFC3339).
                            Defaults to all logs. Only one of start-time / since may be used.
      --tasks strings       Optional. Only return logs from specific task IDs.

Examples
  Displays logs of the service "my-svc" in environment "test".
  `$ copilot svc logs -n my-svc -e test`
  Displays logs in the last hour.
  `$ copilot svc logs --since 1h`
  Displays logs from 2006-01-02T15:04:05 to 2006-01-02T15:05:05.
  `$ copilot svc logs --start-time 2006-01-02T15:04:05+00:00 --end-time 2006-01-02T15:05:05+00:00`
        Displays logs from specific task IDs.
  `$ copilot svc logs --tasks 709c7eae05f947f6861b150372ddc443,1de57fd63c6a4920ac416d02add891b9`
  Displays logs in real time.
  `$ copilot svc logs --follow`

https://github.com/brentley/ecsdemo-frontend からForkして、
自身のGithubリポジトリを追加して、パイプライン追加

$ copilot pipeline update
✔ Successfully added pipeline resources to your application: ecsworkshop
Are you sure you want to update an existing pipeline: pipeline-ecsworkshop-ecsdemo-frontend? Yes
✔ Successfully updated pipeline: pipeline-ecsworkshop-ecsdemo-frontend

Recommended follow-up actions:
- Run `copilot pipeline status` to see the state of your pipeline.
- Run `copilot pipeline show` for info about your pipeline.
$ 

GitHubを2段階認証している人はパイプライン追加がいつまでも進まないです。
自分はハマったので別のRepositoryを新規で作成しました。
そうするとすぐにパイプライン追加処理が終わりました。。

$ git remote rm upstream
$ 
$ git remote add upstream https://github.com/midnight480-shibao/ecsdemo-frontend.git
$ 
$ git remote -v
origin  https://github.com/brentley/ecsdemo-frontend (fetch)
origin  https://github.com/brentley/ecsdemo-frontend (push)
upstream        https://github.com/midnight480-shibao/ecsdemo-frontend.git (fetch)
upstream        https://github.com/midnight480-shibao/ecsdemo-frontend.git (push)

CodePipelineがエラーになってしまったので、後日やり直すために記録として残します。

$ copilot pipeline delete
Are you sure you want to delete pipeline pipeline-ecsworkshop-ecsdemo-frontend from application ecsworkshop? Yes
✔ Deleted pipeline pipeline-ecsworkshop-ecsdemo-frontend from application ecsworkshop.
admin:~/environment/ecsdemo-frontend (main) $ copilot app delete 
Are you sure you want to delete application ecsworkshop? Yes
✔ Deleted service ecsdemo-frontend from environment test.
✔ Deleted resources of service ecsdemo-frontend from application ecsworkshop.
✔ Deleted service ecsdemo-frontend from application ecsworkshop.
✔ Deleted environment test from application ecsworkshop.
✔ Cleaned up deployment resources.
✔ Deleted pipeline pipeline-ecsworkshop-ecsdemo-frontend from application ecsworkshop.
✔ Deleted application resources.
✔ Deleted application configuration.
✔ Deleted local .workspace file.
$ 

この記事のまとめ

途中でエラーが出るなど、うまく行かないケースがあり、
CodePipelineのエラーもCodeStarとの連携不備だったりするので、
後日別リージョンでやってみることにします。

Discussion

ログインするとコメントできます