[アップデート] EC2 Image Builder の components をローカルでテストするツールがリリースされました

EC2 Image Builder がもっと使いやすくなりそうですね。
2020.08.04

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

哈喽大家好、コンサルティング部の西野です。

EC2 Image Builder components can now be developed locally

このたびリリースされたツール awstoe により、AMI を作成するための一連の手順を定義する components をローカルでテストできるようになりました。
Image Pipeline を実行する前に各種のエラーをチェックできるので、EC2 Image Builder の使い勝手が向上しそうです。

東京リージョン (ap-northeast-1) でも本日 2020/08/04 から使用できます。

やってみた

公式ドキュメントを参照しつつテスト試してみました。以下の手順は Docker(amazonlinux) on macOS で実施しています。

awstoe のインストール

実行ファイルを任意のディレクトリにダウンロードし、実行権限を付与します。

# mkdir /tmp/imagebuilder
# cd /tmp/imagebuilder
# wget https://awstoe-ap-northeast-1.s3.ap-northesat-1.amazonaws.com/latest/linux/386/awstoe
# chmod +x awstoe

awstoe の実行ファイルはプラットフォームおよびアーキテクチャごとに用意されています。
それぞれの URL は公式ドキュメントに記載されているのでこちらをご確認ください。

components ファイルの準備

components の YAML ファイルを用意します。今回はドキュメントに記載されているサンプルを使用します。

hello-world-linux.yml

name: Hello World
description: This is hello world testing document for Linux.
schemaVersion: 1.0
phases:
  - name: build
    steps:
      - name: HelloWorldStep
        action: ExecuteBash
        inputs:
          commands:
            - echo 'Hello World from the build phase.'
  - name: validate
    steps:
      - name: HelloWorldStep
        action: ExecuteBash
        inputs:
          commands:
            - echo 'Hello World from the validate phase.'
  - name: test
    steps:
      - name: HelloWorldStep
        action: ExecuteBash
        inputs:
          commands:
            - echo 'Hello World from the test phase.'

components のバリデーション

validate オプションによりシンタックスチェックができます。

# ./awstoe validate --documents hello-world-linux.yml
{
    "validationStatus": "success",
    "message": "Document(s) [hello-world-linux.yml] is/are valid."
}

validationStatussuccess と表示されます。チェックが上手く通ったみたいです。
これだけではつまらないので YAML ファイルを壊してみましょう。ファイルをコピーし、8行目の action 部分を下記のように書き換えます。

hello-world-linux-broken.yml

name: Hello World
description: This is hello world testing document for Linux.
schemaVersion: 1.0
phases:
  - name: build
    steps:
      - name: HelloWorldStep
        action: ExecuteZsh
        inputs:
          commands:
            - echo 'Hello World from the build phase.'
  - name: validate
    steps:
      - name: HelloWorldStep
        action: ExecuteBash
        inputs:
          commands:
            - echo 'Hello World from the validate phase.'
  - name: test
    steps:
      - name: HelloWorldStep
        action: ExecuteBash
        inputs:
          commands:
            - echo 'Hello World from the test phase.'
# ./awstoe validate --documents hello-world-linux-broken.yml
{
    "validationStatus": "failed",
    "message": "validate document failed for hello-world-linux-broken.yml. Error : Parsing step 'HelloWorldStep' in phase 'build' failed. Error: Unsupported action ExecuteZsh for os platform linux."
}

不正な箇所とその内容を詳しく表示してくれますね。

components の実行

run オプションにより実際に実行した場合のテストができます。build フェーズのみを指定して実行してみましょう。

./awstoe run --documents hello-world-linux.yml --phases build
{
    "executionId": "a5dec5f8-d5f7-11ea-a099-0242ac110002",
    "status": "success",
    "failedStepCount": 0,
    "executedStepCount": 1,
    "failureMessage": "",
    "logUrl": "/tmp/imagebuilder/TOE_2020-08-04_02-10-20_UTC-0_a5dec5f8-d5f7-11ea-a099-0242ac110002"
}

logUrl のディレクトリに実行結果のログが保存されています。

# cd /tmp/imagebuilder/TOE_2020-08-04_02-10-20_UTC-0_a5dec5f8-d5f7-11ea-a099-0242ac110002
# ls
0__hello-world-linux.yml  application.log  chaining.json  console.log  detailedoutput.json
# cat console.log
2020-08-04 02:10:20.473 Info Document hello-world-linux.yml
2020-08-04 02:10:20.473 Info Phase build
2020-08-04 02:10:20.473 Info Step HelloWorldStep
2020-08-04 02:10:20.476 Info Stdout: Hello World from the build phase.
2020-08-04 02:10:20.477 Info Command execution completed successfully
2020-08-04 02:10:20.477 Info Stderr:
2020-08-04 02:10:20.477 Info ExitCode 0
2020-08-04 02:10:20.478 Info TOE has completed execution successfully

AWS サービスへのログ出力

S3 や CloudWatch Logs にログを保存することもできます。

S3 へのログ出力

オプションでバケット名を指定します。必要に応じてプレフィックスを指定することもできます。

# ./awstoe run --documents hello-world-linux.yml --log-s3-bucket-name xiyegen-imagebuilder --log-s3-key-prefix test-prefix

CLoudWatch Logs へのログ出力

オプションでロググループ・ログストリーム名およびリージョンを指定します。

# ./awstoe run --documents hello-world-linux.yml --phases build --cw-log-group /imagebuilder --cw-log-region ap-northeast-1 --cw-log-stream stream1

終わりに

このブログがほんの少しでも世界を良くできれば嬉しいです。
コンサルティング部の西野 (@xiyegen) がお送りしました。