ECSのサービスの詳細を取得する場合、Cluster名とService名を指定する必要があり、すべて取得するとなるとClusterでループしさらにServiceでもループさせて取得する必要がある。それぞれ実行してたら日が暮れるので1行でサクッと取れる方法を調べてみた。
ChatGPT様回答
※下記でよいとのこと。さすがChatGPT様。
# aws ecs list-clusters | jq -r '.clusterArns[]' | xargs -I {} sh -c "aws ecs list-services --cluster {} | jq -r '.serviceArns[]' | xargs -I {service} aws ecs describe-services --cluster {} --services {service}"
実践!
1.CloudShellで下記を実行
$ aws ecs list-clusters | jq -r '.clusterArns[]' | xargs -I {} sh -c "aws ecs list-services --cluster {} | jq -r '.serviceArns[]' | xargs -I {service} aws ecs describe-services --cluster {} --services {service}"
※取れた!下記は1つずつしかないが複数個あっても取れます。
{ "services": [ { "serviceArn": "arn:aws:ecs:ap-northeast-1:xxxxxxxxxxxx:service/test_my_cluster/test_my_service", "serviceName": "test_my_service", "clusterArn": "arn:aws:ecs:ap-northeast-1:xxxxxxxxxxxx:cluster/test_my_cluster", "loadBalancers": [], "serviceRegistries": [], "status": "ACTIVE", "desiredCount": 2, "runningCount": 2, "pendingCount": 0, "launchType": "FARGATE", "platformVersion": "LATEST", "platformFamily": "Linux", "taskDefinition": "arn:aws:ecs:ap-northeast-1:xxxxxxxxxxxx:task-definition/test_my_task:3", "deploymentConfiguration": { "deploymentCircuitBreaker": { "enable": false, "rollback": false }, "maximumPercent": 200, "minimumHealthyPercent": 100 }, "deployments": [ { "id": "ecs-svc/2828737226229126325", "status": "PRIMARY", "taskDefinition": "arn:aws:ecs:ap-northeast-1:xxxxxxxxxxxx:task-definition/test_my_task:3", "desiredCount": 2, "pendingCount": 0, "runningCount": 2, "failedTasks": 0, "createdAt": "2023-11-10T07:14:37.901000+00:00", "updatedAt": "2023-11-10T07:15:14.781000+00:00", "launchType": "FARGATE", "platformVersion": "1.4.0", "platformFamily": "Linux", "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-0e71a0ea85a43e175", "subnet-01d7fc3fec309c469" ], "securityGroups": [ "sg-0dd54db1f11572af9" ], "assignPublicIp": "ENABLED" } }, "rolloutState": "COMPLETED", "rolloutStateReason": "ECS deployment ecs-svc/2828737226229126325 completed." } ], "roleArn": "arn:aws:iam::xxxxxxxxxxxx:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS", "events": [ { "id": "b16689fb-9656-4144-9e75-a3ff30d8a139", "createdAt": "2023-11-10T07:15:14.789000+00:00", "message": "(service test_my_service) has reached a steady state." }, { "id": "d2bd8853-661e-4eb8-9ff7-9d3b46a38691", "createdAt": "2023-11-10T07:15:14.788000+00:00", "message": "(service test_my_service) (deployment ecs-svc/2828737226229126325) deployment completed." }, { "id": "0d319851-b6a1-4021-9aa9-f5f3b5d78ae2", "createdAt": "2023-11-10T07:14:44.698000+00:00", "message": "(service test_my_service) has started 2 tasks: (task 166f6247d2bd4d6797b7d024ee6babbe) (task 5af0639671cd40fcb5c334fd9208eba0)." } ], "createdAt": "2023-11-10T07:14:37.901000+00:00", "placementConstraints": [], "placementStrategy": [], "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-0e71a0ea85a43e175", "subnet-01d7fc3fec309c469" ], "securityGroups": [ "sg-0dd54db1f11572af9" ], "assignPublicIp": "ENABLED" } }, "schedulingStrategy": "REPLICA", "deploymentController": { "type": "ECS" }, "createdBy": "arn:aws:iam::xxxxxxxxxxxx:user/vaultuser", "enableECSManagedTags": true, "propagateTags": "SERVICE", "enableExecuteCommand": false } ], "failures": [] }
感想
Bashを覚えると他を忘れるという、、、もっと頭よくなりたい。。。