LoginSignup
0
0

More than 1 year has passed since last update.

MacとDockerでAWSTOEをコマンド化する

Last updated at Posted at 2021-10-21

AWSTOE は、EC2 Image BuilderのComponentsなどを記述する際に利用されるツール。
YAML形式で記述したComponentをローカルで検証/実行するのに AWSTOE が使える。

ただし、Mac版はない。
正直あったとしてもMacBookのローカルで実行したくはない。
そこでDockerコンテナ内でAWSTOEを実行するための awstoe コマンドを用意する。

使用イメージ

検証したい時

YAMLを編集したあと、ローカルで検証できるだけで随分作業が楽になる。

awstoe validate -d component.yaml

実行したい時

-t オプションはターミナルに実行ログを表示できる。

awstoe run -t -d component1.yaml,component2.yaml -p build,test

ヘルプ

awstoe --help

動き

Linux用の awstoe をamazonlinux:2 のコンテナにインストールする。
https://awstoe-ap-northeast-1.s3.ap-northeast-1.amazonaws.com/latest/linux/386/awstoe

また、EC2インスタンスに環境を合わせるため、amazonlinux:2のイメージに含まれていないPRMもインストールしておく。

このコンテナ実行時にカレントディレクトリをmountし、指定されたYAMLを検証/実行する。
実行時はsystemdが起動するよう /usr/sbin/init--entrypoint にする。

Dockerfile

EC2インスタンスのAmazonLinux2と、Dockerイメージのamazonlinux:2を比較してPRMをインストールした。

Dockerfile
FROM amazonlinux:2

RUN curl -O https://awstoe-ap-northeast-1.s3.ap-northeast-1.amazonaws.com/latest/linux/386/awstoe \
 && chmod +x awstoe \
 && mv awstoe /usr/bin/

RUN yum update -y \
 && yum install -y \
      bash-completion \
      bc \
      bind-utils \
      bzip2 \
      cloud-init \
      cronie \
      dhclient \
      file \
      less \
      lsof \
      man \
      tar \
      tcpdump \
      unzip \
      which \
      zip

ENV WORKDIR /work
WORKDIR /work

ENTRYPOINT ["awstoe"]
CMD ["--version"]

awstoeコマンド

ただのbashスクリプト。
awstoe run でコンテナが起動するので、処理が終わったらログインして結果を確認する感じ。
S3Download などでAWSにアクアスする必要がある場合は ~/.aws/root/.aws にmountするなどしてクレデンシャルを渡さないといけない。

awstoe
#!/bin/bash

WORKDIR="/work"

cmd=$1

# awstoe run -d component.yaml
if [ "$cmd" = "run" -a ! "$2" = "--help" ]; then
  cid=$(docker container run -itd --mount type=bind,source="$(pwd)",target="$WORKDIR" --entrypoint /usr/sbin/init --privileged awstoe)
  time docker container exec -itw "$WORKDIR" "$cid" /usr/bin/awstoe $@
  echo
  echo "# コンテナにログインして実行結果を確認"
  echo "docker container exec -it $cid /bin/bash"
  echo
  echo "# コンテナを破棄"
  echo "docker container rm -f $cid"
# awstoe validate -d component.yaml
else
  docker container run --rm --mount type=bind,source="$(pwd)",target="$WORKDIR" awstoe $@
fi
0
0
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
0
0