あきろぐ

いろいろめもするよ🐈🐈🐈

ecs-cliを使ってみる

概要

awsコマンドラインツールはaws-cliがありますが、ecsに特化したecs-cliを使えば、ローカルからクラスタやタスクの作成や簡単なモニタリングをすることが可能です。 そのecs-cliの導入とちょっと使ってみたときのメモです。

導入

macOSの場合

ecs-cliのバイナリをダウンロードする

~ $ sudo curl -o /usr/local/bin/ecs-cli https://amazon-ecs-cli.s3.amazonaws.com/ecs-cli-darwin-amd64-latest

ecs-cliの検証をする

オプションの手順ではありまずが、ダウンロードしたバイナリファイルの有効性を検証するために、MD5サムを使います。 コマンド実行後、同じ文字列が出力されていることを確認します。

~ $ sudo curl -o /usr/local/bin/ecs-cli https://amazon-ecs-cli.s3.amazonaws.com/ecs-cli-darwin-amd64-latest

xxxxxxxxxxx
xxxxxxxxxxx

実行権限を付与する

ダウンロードしたバイナリファイルに実行権限を付与してコマンドとして使えるようにします。

~ $ sudo chmod +x /usr/local/bin/ecs-cli

# 下記コマンドが実行できればOK
~ $ ecs-cli --version
ecs-cli version 1.18.1 (7e9df84)

使ってみる

困ったらhelp コンテナインスタンスをスケールさせることができたり、Dockerイメージをプルしたりプッシュすることもできます。

~ $ ecs-cli --help
NAME:
   ecs-cli - Command line interface for Amazon ECS

USAGE:
   ecs-cli [global options] command [command options] [arguments...]

VERSION:
   1.18.1 (7e9df84)

AUTHOR:
   Amazon Web Services

COMMANDS:
     configure         Stores a single cluster configuration.
     up                Creates the ECS cluster (if it does not already exist) and the AWS resources required to set up the cluster.
     down              Deletes the CloudFormation stack that was created by ecs-cli up and the associated resources.
     scale             Modifies the number of container instances in your cluster. This command changes the desired and maximum instance count in the Auto Scaling group created by the ecs-cli up command. You can use this command to scale up (increase the number of instances) or scale down (decrease the number of instances) your cluster.
     ps                Lists all of the running containers in your ECS cluster.
     push              Pushes an image to an Amazon ECR repository.
     pull              Pulls an image from an Amazon ECR repository.
     images            Lists images from an Amazon ECR repository. Lists all images in all repositories by default.
     license           Prints the LICENSE files for the ECS CLI and its dependencies.
     compose           Executes docker-compose-style commands on an ECS cluster.
     check-attributes  Checks if a given list of container instances can run a given task definition by checking their attributes. Outputs attributes that are required by the task definition but not present on the container instances.
     logs              Retrieves container logs from CloudWatch logs. Assumes your Task Definition uses the awslogs driver and has a log stream prefix specified.
     registry-creds    Facilitates the creation and use of private registry credentials within ECS.
     local             Runs your ECS tasks locally.
     help, h           Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --endpoint value  Use a custom endpoint with the ECS CLI
   --help, -h        show help
   --version, -v     print the version

ecs-cli psコマンド ECS クラスターで実行中のすべてのコンテナを一覧表示する

~ $ aws-vault exec my-profile -- ecs-cli ps --cluster test-develop
Name                                                  State    Ports                           TaskDefinition                       Health
xxxxx-xxxx-xxxx-xxxx-xxxxx0c773e/test  RUNNING                                  test-test:3               HEALTHY

参考文献

https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/ECS_CLI_installation.html