文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
DescribeTasks
搭配 a AWS SDK 或 CLI 使用
下列程式碼範例示範如何使用 DescribeTasks
。
- CLI
-
- AWS CLI
-
測驗 1:描述單一任務任務
下列
describe-tasks
範例會擷取叢集中任務的詳細資訊。您可以使用任務的 ID 或完整 ARN 來指定任務。此範例使用任務的完整 ARN。aws ecs describe-tasks \ --cluster
MyCluster
\ --tasksarn:aws:ecs:us-east-1:123456789012:task/MyCluster/4d590253bb114126b7afa7b58EXAMPLE
輸出:
{ "tasks": [ { "attachments": [], "attributes": [ { "name": "ecs.cpu-architecture", "value": "x86_64" } ], "availabilityZone": "us-east-1b", "clusterArn": "arn:aws:ecs:us-east-1:123456789012:cluster/MyCluster", "connectivity": "CONNECTED", "connectivityAt": "2021-08-11T12:21:26.681000-04:00", "containerInstanceArn": "arn:aws:ecs:us-east-1:123456789012:container-instance/test/025c7e2c5e054a6790a29fc1fEXAMPLE", "containers": [ { "containerArn": "arn:aws:ecs:us-east-1:123456789012:container/MyCluster/4d590253bb114126b7afa7b58eea9221/a992d1cc-ea46-474a-b6e8-24688EXAMPLE", "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/MyCluster/4d590253bb114126b7afa7b58EXAMPLE", "name": "simple-app", "image": "httpd:2.4", "runtimeId": "91251eed27db90006ad67b1a08187290869f216557717dd5c39b37c94EXAMPLE", "lastStatus": "RUNNING", "networkBindings": [ { "bindIP": "0.0.0.0", "containerPort": 80, "hostPort": 80, "protocol": "tcp" } ], "networkInterfaces": [], "healthStatus": "UNKNOWN", "cpu": "10", "memory": "300" } ], "cpu": "10", "createdAt": "2021-08-11T12:21:26.681000-04:00", "desiredStatus": "RUNNING", "enableExecuteCommand": false, "group": "service:testupdate", "healthStatus": "UNKNOWN", "lastStatus": "RUNNING", "launchType": "EC2", "memory": "300", "overrides": { "containerOverrides": [ { "name": "simple-app" } ], "inferenceAcceleratorOverrides": [] }, "pullStartedAt": "2021-08-11T12:21:28.234000-04:00", "pullStoppedAt": "2021-08-11T12:21:33.793000-04:00", "startedAt": "2021-08-11T12:21:34.945000-04:00", "startedBy": "ecs-svc/968695068243EXAMPLE", "tags": [], "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/MyCluster/4d590253bb114126b7afa7b58eea9221", "taskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/console-sample-app-static2:1", "version": 2 } ], "failures": [] }
如需詳細資訊,請參閱 Amazon ECS 開發人員指南中的 Amazon Word 任務定義。 ECS
測驗 2:描述多個任務
下列
describe-tasks
範例會擷取叢集中多個任務的詳細資訊。您可以使用任務的 ID 或完整 ARN 來指定任務。此範例使用任務的完整 IDs。aws ecs describe-tasks \ --cluster
MyCluster
\ --tasks"74de0355a10a4f979ac495c14EXAMPLE"
"d789e94343414c25b9f6bd59eEXAMPLE"
輸出:
{ "tasks": [ { "attachments": [ { "id": "d9e7735a-16aa-4128-bc7a-b2d51EXAMPLE", "type": "ElasticNetworkInterface", "status": "ATTACHED", "details": [ { "name": "subnetId", "value": "subnet-0d0eab1bb3EXAMPLE" }, { "name": "networkInterfaceId", "value": "eni-0fa40520aeEXAMPLE" }, { "name": "macAddress", "value": "0e:89:76:28:07:b3" }, { "name": "privateDnsName", "value": "ip-10-0-1-184.ec2.internal" }, { "name": "privateIPv4Address", "value": "10.0.1.184" } ] } ], "attributes": [ { "name": "ecs.cpu-architecture", "value": "x86_64" } ], "availabilityZone": "us-east-1b", "clusterArn": "arn:aws:ecs:us-east-1:123456789012:cluster/MyCluster", "connectivity": "CONNECTED", "connectivityAt": "2021-12-20T12:13:37.875000-05:00", "containers": [ { "containerArn": "arn:aws:ecs:us-east-1:123456789012:container/MyCluster/74de0355a10a4f979ac495c14EXAMPLE/aad3ba00-83b3-4dac-84d4-11f8cEXAMPLE", "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/MyCluster/74de0355a10a4f979ac495c14EXAMPLE", "name": "web", "image": "nginx", "runtimeId": "74de0355a10a4f979ac495c14EXAMPLE-265927825", "lastStatus": "RUNNING", "networkBindings": [], "networkInterfaces": [ { "attachmentId": "d9e7735a-16aa-4128-bc7a-b2d51EXAMPLE", "privateIpv4Address": "10.0.1.184" } ], "healthStatus": "UNKNOWN", "cpu": "99", "memory": "100" } ], "cpu": "256", "createdAt": "2021-12-20T12:13:20.226000-05:00", "desiredStatus": "RUNNING", "enableExecuteCommand": false, "group": "service:tdsevicetag", "healthStatus": "UNKNOWN", "lastStatus": "RUNNING", "launchType": "FARGATE", "memory": "512", "overrides": { "containerOverrides": [ { "name": "web" } ], "inferenceAcceleratorOverrides": [] }, "platformVersion": "1.4.0", "platformFamily": "Linux", "pullStartedAt": "2021-12-20T12:13:42.665000-05:00", "pullStoppedAt": "2021-12-20T12:13:46.543000-05:00", "startedAt": "2021-12-20T12:13:48.086000-05:00", "startedBy": "ecs-svc/988401040018EXAMPLE", "tags": [], "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/MyCluster/74de0355a10a4f979ac495c14EXAMPLE", "taskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/webserver:2", "version": 3, "ephemeralStorage": { "sizeInGiB": 20 } }, { "attachments": [ { "id": "214eb5a9-45cd-4bf8-87bc-57fefEXAMPLE", "type": "ElasticNetworkInterface", "status": "ATTACHED", "details": [ { "name": "subnetId", "value": "subnet-0d0eab1bb3EXAMPLE" }, { "name": "networkInterfaceId", "value": "eni-064c7766daEXAMPLE" }, { "name": "macAddress", "value": "0e:76:83:01:17:a9" }, { "name": "privateDnsName", "value": "ip-10-0-1-41.ec2.internal" }, { "name": "privateIPv4Address", "value": "10.0.1.41" } ] } ], "attributes": [ { "name": "ecs.cpu-architecture", "value": "x86_64" } ], "availabilityZone": "us-east-1b", "clusterArn": "arn:aws:ecs:us-east-1:123456789012:cluster/MyCluster", "connectivity": "CONNECTED", "connectivityAt": "2021-12-20T12:13:35.243000-05:00", "containers": [ { "containerArn": "arn:aws:ecs:us-east-1:123456789012:container/MyCluster/d789e94343414c25b9f6bd59eEXAMPLE/9afef792-609b-43a5-bb6a-3efdbEXAMPLE", "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/MyCluster/d789e94343414c25b9f6bd59eEXAMPLE", "name": "web", "image": "nginx", "runtimeId": "d789e94343414c25b9f6bd59eEXAMPLE-265927825", "lastStatus": "RUNNING", "networkBindings": [], "networkInterfaces": [ { "attachmentId": "214eb5a9-45cd-4bf8-87bc-57fefEXAMPLE", "privateIpv4Address": "10.0.1.41" } ], "healthStatus": "UNKNOWN", "cpu": "99", "memory": "100" } ], "cpu": "256", "createdAt": "2021-12-20T12:13:20.226000-05:00", "desiredStatus": "RUNNING", "enableExecuteCommand": false, "group": "service:tdsevicetag", "healthStatus": "UNKNOWN", "lastStatus": "RUNNING", "launchType": "FARGATE", "memory": "512", "overrides": { "containerOverrides": [ { "name": "web" } ], "inferenceAcceleratorOverrides": [] }, "platformVersion": "1.4.0", "platformFamily": "Linux", "pullStartedAt": "2021-12-20T12:13:44.611000-05:00", "pullStoppedAt": "2021-12-20T12:13:48.251000-05:00", "startedAt": "2021-12-20T12:13:49.326000-05:00", "startedBy": "ecs-svc/988401040018EXAMPLE", "tags": [], "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/MyCluster/d789e94343414c25b9f6bd59eEXAMPLE", "taskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/webserver:2", "version": 3, "ephemeralStorage": { "sizeInGiB": 20 } } ], "failures": [] }
如需詳細資訊,請參閱 Amazon ECS 開發人員指南中的 Amazon Word 任務定義。 ECS
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DescribeTasks
。
-
- Java
-
- Java 2.x 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecs.EcsClient; import software.amazon.awssdk.services.ecs.model.DescribeTasksRequest; import software.amazon.awssdk.services.ecs.model.DescribeTasksResponse; import software.amazon.awssdk.services.ecs.model.EcsException; import software.amazon.awssdk.services.ecs.model.Task; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListTaskDefinitions { public static void main(String[] args) { final String usage = """ Usage: <clusterArn> <taskId>\s Where: clusterArn - The ARN of an ECS cluster. taskId - The task Id value. """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String clusterArn = args[0]; String taskId = args[1]; Region region = Region.US_EAST_1; EcsClient ecsClient = EcsClient.builder() .region(region) .build(); getAllTasks(ecsClient, clusterArn, taskId); ecsClient.close(); } public static void getAllTasks(EcsClient ecsClient, String clusterArn, String taskId) { try { DescribeTasksRequest tasksRequest = DescribeTasksRequest.builder() .cluster(clusterArn) .tasks(taskId) .build(); DescribeTasksResponse response = ecsClient.describeTasks(tasksRequest); List<Task> tasks = response.tasks(); for (Task task : tasks) { System.out.println("The task ARN is " + task.taskDefinitionArn()); } } catch (EcsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
-
如需 API 詳細資訊,請參閱 DescribeTasks AWS SDK for Java 2.x 參考中的 API。
-
DescribeServices
ListClusters