【AWS】AWS CLIを使って、ECSクラスターとECSサービスを作成する【コマンド集】
やること
AWS CLIを使って、以下を行う(それに合わせて、各コマンドも紹介)
- ECSクラスターの作成
- タスク定義の作成
- ECSサービスの作成
ECRについて
ECRにDocker Imageをアップロードする方法は こちら にまとめてあります
ECSクラスター
作成
$ aws ecs create-cluster --cluster-name test-cluster
一覧取得
$ aws ecs list-clusters
詳細表示
$ aws ecs describe-clusters --cluster test-cluster
削除
$ aws ecs delete-cluster --cluster test-cluster
タスク定義
作成
タスク定義用のjsonファイルを準備します
{
"family": "test-task",
"networkMode": "awsvpc",
"containerDefinitions": [
{
"name": "test-task",
"image": <docker image url>,
"essential": true
}
],
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "256",
"memory": "512",
"executionRoleArn": "arn:aws:iam::<account_id>:role/ecsTaskExecutionRole",
"taskRoleArn": "arn:aws:iam::<account_id>:role/ecsTaskExecutionRole"
}
このファイルを読み込む形で、以下のコマンドでタスク定義をAWSに登録できます
$ aws ecs register-task-definition --cli-input-json file://test.json
一覧
$ aws ecs list-task-definitions
サービス
作成
$ aws ecs create-service --cluster test-cluster --service-name test-service --task-definition test-task:1 --desired-count 1 --launch-type "FARGATE" --network-configuration "awsvpcConfiguration={subnets=[subnet-id],securityGroups=[sg-id]}"
一覧
$ aws ecs list-services --cluster test-cluster
詳細
$ aws ecs describe-services --cluster test-cluster --services test-service
削除
$ aws ecs delete-service --cluster test-cluster --service test-service --force