こんにちは。AWS CLIが好きな福島です。
- はじめに
- 利用するコマンド,サブコマンド
- クラスタの一覧
- 特定のクラスタで稼働しているサービス一覧
- タスク定義の一覧
- 最新の全タスク定義のJSON情報(ファイルに出力)
- 全クラスタで稼働しているサービス一覧
- 全クラスタで稼働しているサービスのタスク定義およびイメージURL
- 全クラスタで稼働しているサービスに割り当てているCPU,メモリ
- 全クラスタで稼働しているサービスのタスク実行ロール
- 全クラスタで稼働しているサービスの変数とシークレット
- 全クラスタで稼働しているサービスのログ
- 終わりに
はじめに
今回は、ECS 関連の情報を取得する方法をご紹介いたします。
利用するコマンド,サブコマンド
まず、AWS CLIの構造は以下の通りです。
aws <command> <subcommand> [options and parameters]
上記を前提に今回使う <command>,<subcommand>
は、以下の通りです。
<command>
ecs
<subcommand>
* list-clusters ⇒ECS Clusterの一覧を出力するコマンド * list-services ⇒ECS Clusterで稼働しているサービス一覧を出力するコマンド * describe-services ⇒サービスの詳細を出力するコマンド * describe-task-definition ⇒タスク定義の詳細を出力するコマンド
クラスタの一覧
- 実行コマンド
aws ecs list-clusters --query "clusterArns" --output text | tr "\t" "\n"
- 実行結果
# aws ecs list-clusters --query "clusterArns" --output text | tr "\t" "\n" arn:aws:ecs:ap-northeast-1:012345678912:cluster/fk-test-cluster-001 arn:aws:ecs:ap-northeast-1:012345678912:cluster/fk-test-cluster-002 #
特定のクラスタで稼働しているサービス一覧
- 変数定義
cluster="" #クラスタ名(ARNでも可)
- 実行コマンド
aws ecs list-services --cluster $cluster --query "[serviceArns]" --output text | tr "\t" "\n"
- 実行結果
# aws ecs list-services --cluster $cluster --query "[serviceArns]" --output text | tr "\t" "\n" arn:aws:ecs:ap-northeast-1:012345678912:service/fk-test-cluster-001/service-web arn:aws:ecs:ap-northeast-1:012345678912:service/fk-test-cluster-001/service-app #
タスク定義の一覧
- 実行コマンド
aws ecs list-task-definitions --query "taskDefinitionArns" --output text | tr "\t" "\n"
- 実行結果
# aws ecs list-task-definitions --query "taskDefinitionArns" --output text | tr "\t" "\n" arn:aws:ecs:ap-northeast-1:012345678912:task-definition/fk-test-service-web-task:1 arn:aws:ecs:ap-northeast-1:012345678912:task-definition/fk-test-service-web-task:2 arn:aws:ecs:ap-northeast-1:012345678912:task-definition/fk-test-service-ap-task:1 arn:aws:ecs:ap-northeast-1:012345678912:task-definition/fk-test-service-ap-task:2 arn:aws:ecs:ap-northeast-1:012345678912:task-definition/fk-test-service-ap-task:3
最新の全タスク定義のJSON情報(ファイルに出力)
- 実行コマンド
aws ecs list-task-definitions --query "taskDefinitionArns" --output text | tr "\t" "\n" | awk -F/ '{print $2}' | awk -F: '{print $1}' | uniq | while read task do aws ecs describe-task-definition --task-definition $task > $task done
- 実行結果
# aws ecs list-task-definitions --query "taskDefinitionArns" --output text | tr "\t" "\n" | awk -F/ '{print $2}' | awk -F: '{print $1}' | uniq | while read task > do > aws ecs describe-task-definition --task-definition $task > $task > done # ls -lrt -rw-r--r-- 1 root root 2648 Mar 30 19:37 fk-test-web-task -rw-r--r-- 1 root root 2827 Mar 30 19:37 fk-test-ap-task #
全クラスタで稼働しているサービス一覧
- 実行コマンド
aws ecs list-clusters --query "clusterArns" --output text | tr "\t" "\n" | while read cluster do aws ecs list-services --cluster $cluster --query "[serviceArns]" --output text | tr "\t" "\n" done
- 実行結果
# aws ecs list-clusters --query "clusterArns" --output text | tr "\t" "\n" | while read cluster > do > aws ecs list-services --cluster $cluster --query "[serviceArns]" --output text | tr "\t" "\n" > done arn:aws:ecs:ap-northeast-1:012345678912:service/fk-test-cluster-001/service-web arn:aws:ecs:ap-northeast-1:012345678912:service/fk-test-cluster-001/service-app arn:aws:ecs:ap-northeast-1:012345678912:service/fk-test-cluster-002/service-web arn:aws:ecs:ap-northeast-1:012345678912:service/fk-test-cluster-002/service-app #
全クラスタで稼働しているサービスのタスク定義およびイメージURL
- 実行コマンド
aws ecs list-clusters --query "clusterArns" --output text | tr "\t" "\n" | awk -F/ '{print $2}' | while read cluster do aws ecs list-services --cluster $cluster --query "serviceArns" --output text | tr "\t" "\n" | awk -F/ '{print $3}' | while read service do aws ecs describe-services --cluster $cluster --services $service --query "services[].[taskDefinition]" --output text | while read task do aws ecs describe-task-definition --task-definition $task --query "taskDefinition.['$cluster','$service','${task#*/}',containerDefinitions[0].name,containerDefinitions[0].image]" --output text done done done
- 実行結果
# aws ecs list-clusters --query "clusterArns" --output text | tr "\t" "\n" | awk -F/ '{print $2}' | while read cluster > do > aws ecs list-services --cluster $cluster --query "serviceArns" --output text | tr "\t" "\n" | awk -F/ '{print $3}' | while read service > do > aws ecs describe-services --cluster $cluster --services $service --query "services[].[taskDefinition]" --output text | while read task > do > aws ecs describe-task-definition --task-definition $task --query "taskDefinition.['$cluster','$service','${task#*/}',containerDefinitions[0].name,containerDefinitions[0].image]" --output text > done > done > done fk-test-cluster-001 fk-test-service-web fk-test-service-web-task:22 fk-test-service-web 012345678912.dkr.ecr.ap-northeast-1.amazonaws.com/fk-test-web-ecr:0.0.19 fk-test-cluster-001 fk-test-service-ap fk-test-service-ap-task:22 fk-test-service-ap 012345678912.dkr.ecr.ap-northeast-1.amazonaws.com/fk-test-ap-ecr:0.0.19 fk-test-cluster-002 fk-test-service-web fk-test-service-web-task:22 fk-test-service-web 012345678912.dkr.ecr.ap-northeast-1.amazonaws.com/fk-test-web-ecr:0.0.19 fk-test-cluster-002 fk-test-service-ap fk-test-service-ap-task:22 fk-test-service-ap 012345678912.dkr.ecr.ap-northeast-1.amazonaws.com/fk-test-ap-ecr:0.0.19
全クラスタで稼働しているサービスに割り当てているCPU,メモリ
1つのサービスに含まれるコンテナ2つまで表示します。
aws ecs list-clusters --query "clusterArns" --output text | tr "\t" "\n" | awk -F/ '{print $2}' | while read cluster do aws ecs list-services --cluster $cluster --query "serviceArns" --output text | tr "\t" "\n" | awk -F/ '{print $3}' | while read service do aws ecs describe-services --cluster $cluster --services $service --query "services[].[taskDefinition]" --output text | while read task do aws ecs describe-task-definition --task-definition $task --query "taskDefinition.['$cluster','$service','${task#*/}',cpu,memory,containerDefinitions[0].name,containerDefinitions[0].cpu,containerDefinitions[0].memory,containerDefinitions[1].name,containerDefinitions[1].cpu,containerDefinitions[1].memory]" --output text done done done
# aws ecs list-clusters --query "clusterArns" --output text | tr "\t" "\n" | awk -F/ '{print $2}' | while read cluster > do > aws ecs list-services --cluster $cluster --query "serviceArns" --output text | tr "\t" "\n" | awk -F/ '{print $3}' | while read service > do > aws ecs describe-services --cluster $cluster --services $service --query "services[].[taskDefinition]" --output text | while read task > do > aws ecs describe-task-definition --task-definition $task --query "taskDefinition.['$cluster','$service','${task#*/}',cpu,memory,containerDefinitions[0].name,containerDefinitions[0].cpu,containerDefinitions[0].memory,containerDefinitions[1].name,containerDefinitions[1].cpu,containerDefinitions[1].memory]" --output text > done > done > done fk-test-cluster-001 fk-test-service-web fk-test-service-web-task:22 256 512 fk-test-service-web 0 None None None None fk-test-cluster-002 fk-test-service-web fk-test-service-web-task:22 2048 10240 fk-test-service-web 2048 10240 None None None
全クラスタで稼働しているサービスのタスク実行ロール
aws ecs list-clusters --query "clusterArns" --output text | tr "\t" "\n" | awk -F/ '{print $2}' | while read cluster do aws ecs list-services --cluster $cluster --query "serviceArns" --output text | tr "\t" "\n" | awk -F/ '{print $3}' | while read service do aws ecs describe-services --cluster $cluster --services $service --query "services[].[taskDefinition]" --output text | while read task do aws ecs describe-task-definition --task-definition $task --query "taskDefinition.['$cluster','$service','${task#*/}',executionRoleArn]" --output text done done done
# aws ecs list-clusters --query "clusterArns" --output text | tr "\t" "\n" | awk -F/ '{print $2}' | while read cluster > do > aws ecs list-services --cluster $cluster --query "serviceArns" --output text | tr "\t" "\n" | awk -F/ '{print $3}' | while read service > do > aws ecs describe-services --cluster $cluster --services $service --query "services[].[taskDefinition]" --output text | while read task > do > aws ecs describe-task-definition --task-definition $task --query "taskDefinition.['$cluster','$service','${task#*/}',executionRoleArn]" --output text > done > done > done fk-test-cluster-001 fk-test-service-web fk-test-service-web-task:22 arn:aws:iam::012345678912:role/ecsTaskExecutionRole fk-test-cluster-002 fk-test-service-web fk-test-service-web-task:22 arn:aws:iam::012345678912:role/ecsTaskExecutionRole
全クラスタで稼働しているサービスの変数とシークレット
aws ecs list-clusters --query "clusterArns" --output text | tr "\t" "\n" | awk -F/ '{print $2}' | while read cluster do aws ecs list-services --cluster $cluster --query "serviceArns" --output text | tr "\t" "\n" | awk -F/ '{print $3}' | while read service do aws ecs describe-services --cluster $cluster --services $service --query "services[].[taskDefinition]" --output text | while read task do aws ecs describe-task-definition --task-definition $task --query "taskDefinition.containerDefinitions[].[name,environment,secrets]" done done done
- 実行結果
# aws ecs list-clusters --query "clusterArns" --output text | tr "\t" "\n" | awk -F/ '{print $2}' | while read cluster > do > aws ecs list-services --cluster $cluster --query "serviceArns" --output text | tr "\t" "\n" | awk -F/ '{print $3}' | while read service > do > aws ecs describe-services --cluster $cluster --services $service --query "services[].[taskDefinition]" --output text | while read task > do > aws ecs describe-task-definition --task-definition $task --query "taskDefinition.containerDefinitions[].[name,environment,secrets]" > done > done > done [ [ "fk-test-service-web", [], null ] ] [ [ "fk-test-service-ap", [], [ { "name": "DB_HOST", "valueFrom": "arn:aws:secretsmanager:ap-northeast-1:012345678912:secret:secretsmanager-fk-test-db:host::" }, { "name": "DB_NAME", "valueFrom": "arn:aws:secretsmanager:ap-northeast-1:012345678912:secret:secretsmanager-fk-test-db:dbname::" }, { "name": "DB_USERNAME", "valueFrom": "arn:aws:secretsmanager:ap-northeast-1:012345678912:secret:secretsmanager-fk-test-db:username::" }, { "name": "PASSWORD", "valueFrom": "arn:aws:secretsmanager:ap-northeast-1:012345678912:secret:secretsmanager-fk-test-db:password::" } ] ] ]
全クラスタで稼働しているサービスのログ
aws ecs list-clusters --query "clusterArns" --output text | tr "\t" "\n" | awk -F/ '{print $2}' | while read cluster do aws ecs list-services --cluster $cluster --query "serviceArns" --output text | tr "\t" "\n" | awk -F/ '{print $3}' | while read service do aws ecs describe-services --cluster $cluster --services $service --query "services[].events[].['$cluster','$service',createdAt,message]" --output text done done
- 実行結果
# aws ecs list-clusters --query "clusterArns" --output text | tr "\t" "\n" | awk -F/ '{print $2}' | while read cluster > do > aws ecs list-services --cluster $cluster --query "serviceArns" --output text | tr "\t" "\n" | awk -F/ '{print $3}' | while read service > do > aws ecs describe-services --cluster $cluster --services $service --query "services[].events[].['$cluster','$service',createdAt,message]" --output text > done > done fk-test-cluster-001 fk-test-service-web 2023-03-23T12:06:32.395000+09:00 (service fk-test-service-web) has started 1 tasks: (task 083fxxxdfdfer47d98dfddfererererererec). fk-test-cluster-001 fk-test-service-web 2023-03-23T12:07:19.199000+09:00 (service fk-test-service-web) registered 1 targets in (target-group arn:aws:elasticloadbalancing:ap-northeast-1:012345678912:targetgroup/fk-test-alb-tg/exxxxxxxxxxe5) fk-test-cluster-001 fk-test-service-web 2023-03-23T12:07:38.434000+09:00 (service fk-test-service-web) has reached a steady state. fk-test-cluster-001 fk-test-service-web 2023-03-23T18:07:56.049000+09:00 (service fk-test-service-web) has reached a steady state. fk-test-cluster-001 fk-test-service-web 2023-03-24T00:08:34.272000+09:00 (service fk-test-service-web) has reached a steady state.
終わりに
今回は、ECSの情報出力するCLIコマンドをまとめてみました。 どなかたのお役に立てれば幸いです。