ecspressoをラップしたkubectl runっぽいことをするツールを書いた

ecspressoをラップしたkubectl runっぽいことをするツールを書いた。

github.com

これはなに?

~/.demitas/ 配下に設定ファイルの元をおいてランタイムでimage等を上書きし、最終的にecspresso runを実行するツール。

~/.demitas
├── db
│   ├── ecs-container-def.jsonnet
│   ├── ecs-service-def.jsonnet
│   ├── ecs-task-def.jsonnet
│   └── ecspresso.yml
├── ecs-container-def.jsonnet
├── ecs-service-def.jsonnet
├── ecs-task-def.jsonnet
└── ecspresso.yml
~% demitas -c '{image: "public.ecr.aws/runecast/busybox:1.33.1", command: [echo, test]}'
2021/10/30 16:52:45 hello/hello Running task
2021/10/30 16:52:45 hello/hello Registering a new task definition...
2021/10/30 16:52:46 hello/hello Task definition is registered busybox:46
2021/10/30 16:52:46 hello/hello Running task
2021/10/30 16:52:47 hello/hello Task ARN: arn:aws:ecs:ap-northeast-1:822997939312:task/hello/d51ca2de190548e2a2b8e8c9644cfab1
2021/10/30 16:52:47 hello/hello Waiting for run task...(it may take a while)
2021/10/30 16:52:47 hello/hello Watching container: busybox
2021/10/30 16:52:47 hello/hello logGroup: /ecs/busybox
2021/10/30 16:52:47 hello/hello logStream: ecs/busybox/d51ca2de190548e2a2b8e8c9644cfab1
...

clusterとかとかserviceとかsecurity groupとかもコマンドラインで上書きできる。 上書き用の引数の値はYAMLJSONを取るのでややシンプルに書ける。

containerDefinitionsの配列の上書きは難しいので、割り切って1コンテナとした。

~% cat ~/.demitas/ecs-container-def.jsonnet
{
  cpu: 0,
  essential: true,
  name: 'busybox',
  logConfiguration: {
    logDriver: 'awslogs',
    options: {
      'awslogs-group': '/ecs/busybox',
      'awslogs-region': 'ap-northeast-1',
      'awslogs-stream-prefix': 'ecs',
    },
  },
  linuxParameters: {
    initProcessEnabled: true,
  },
}

~/.demitas配下にprofileフォルダを掘っておくと demitas -p <profile>で切り替えられる。

モチベーションとか

  • kubectl runっぽいことがやりたいけど、imageの上書きとかがめんどくさい(環境変数を使うとか)
  • アプリケーションデプロイ用のタスク定義にoneshotのための設定を混ぜ込みたくない
  • 作業ディレクトリにあんまり縛られたくない

こう、アプリケーションのデプロイ用のタスク定義と二重管理になっているのが気になっているが、サービス使って常駐するのものとoneshotのものでは微妙に定義が違ったりするので、分けておいた方がいいかなぁ、と。 kubectl runほど柔軟ではないけどちょっと作業するタスクを起動するのはよいかなと思ってます。 oneshotのためのタスク定義ファミリー名とかロールとかCloudWatch Logsとか用意する必要あるけど

その他

先日調査したECS Fargateでのポートフォワーディング用のラッパーも作った。

github.com

(ただのラッパー。シェルスクリプトと変わらず)

demitasでstoneを起動して、ecs-exec-pfでポートフォワーディングして手元からDB操作とかできるはず。

demitas -c '{image: public.ecr.aws/o0p0b7e7/stone, command: ["db-endpoint:5432", "5432"]}' -- --wait-until=running
ecs-exec-pf -c my-cluster -t 0113f61a4b1044d99c627daeee8c0d0c -p 5432 -l 15432
psql  -h localhost -p 15432