Amazon EC2 任务状态更改事件 - Amazon Elastic Container Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

Amazon EC2 任务状态更改事件

以下方案将引起任务状态更改事件:

您调用 StartTaskRunTaskStopTask API 操作(直接调用,或者通过 Amazon Web Services Management Console、Amazon CLI 或开发工具包调用)。

启动或停止任务将创建新的任务资源或修改现有任务资源的状态。

Amazon ECS 服务调度器启动或停止任务。

启动或停止任务将创建新的任务资源或修改现有任务资源的状态。

Amazon ECS 容器代理调用 SubmitTaskStateChange API 操作。

对于 Amazon EC2 启动类型,Amazon ECS 容器代理将监控容器实例上的任务的状态。Amazon ECS 容器代理将报告任何状态更改。状态更改可能包括从 PENDINGRUNNING 或从 RUNNINGSTOPPED 的更改。

您可以使用 DeregisterContainerInstance API 操作和 force 标志(直接调用,或者通过 Amazon Web Services Management Console或开发工具包调用)强制取消注册基础容器实例。

注销容器实例将更改容器实例的状态以及 Amazon ECS 容器代理的连接状态。如果任务正在容器实例上运行,则必须设置 force 标志以允许取消注册。这将停止实例上的所有任务。

停止或终止基础容器实例。

在停止或终止容器实例时,该实例上运行的任务会转换为 STOPPED 状态。

任务中的容器状态发生更改。

Amazon ECS 容器代理监控任务中的容器状态。例如,如果在任务中运行的容器停止,则此容器状态更改将生成事件。

使用 Fargate Spot 容量提供程序的任务会收到终止通知。

当任务正在使用 FARGATE_SPOT 容量提供程序并且由于 Spot 中断而停止时会生成任务状态更改事件。

例 任务状态更改事件

任务状态更改事件以下面的形式传送。下面的 detail 部分类似于 Amazon Elastic Container Service API Reference 中的 DescribeTasks API 操作返回的任务对象。如果您的容器使用通过 Amazon ECR 托管的映像,则返回 imageDigest 字段。

注意

createdAtconnectivityAtpullStartedAtstartedAtpullStoppedAtupdatedAt 字段的值是 DescribeTasks 操作的响应中的 UNIX 时间戳,而在任务状态更改事件中,它们是 ISO 字符串时间戳。

有关 CloudWatch Events 参数的更多信息,请参阅 Amazon EventBridge 用户指南中的事件和事件模式

有关如何配置一个仅捕获其中的任务因某个主要容器终止而停止的任务事件的 Amazon EventBridge 事件规则,请参阅 针对 Amazon ECS 任务停止事件发送 Amazon Simple Notification Service 警报

{ "version": "0", "id": "3317b2af-7005-947d-b652-f55e762e571a", "detail-type": "ECS Task State Change", "source": "aws.ecs", "account": "111122223333", "time": "2020-01-23T17:57:58Z", "region": "us-west-2", "resources": [ "arn:aws:ecs:us-west-2:111122223333:task/FargateCluster/c13b4cb40f1f4fe4a2971f76ae5a47ad" ], "detail": { "attachments": [ { "id": "1789bcae-ddfb-4d10-8ebe-8ac87ddba5b8", "type": "eni", "status": "ATTACHED", "details": [ { "name": "subnetId", "value": "subnet-abcd1234" }, { "name": "networkInterfaceId", "value": "eni-abcd1234" }, { "name": "macAddress", "value": "0a:98:eb:a7:29:ba" }, { "name": "privateIPv4Address", "value": "10.0.0.139" } ] } ], "availabilityZone": "us-west-2c", "clusterArn": "arn:aws:ecs:us-west-2:111122223333:cluster/FargateCluster", "containers": [ { "containerArn": "arn:aws:ecs:us-west-2:111122223333:container/cf159fd6-3e3f-4a9e-84f9-66cbe726af01", "lastStatus": "RUNNING", "name": "FargateApp", "image": "111122223333.dkr.ecr.us-west-2.amazonaws.com/hello-repository:latest", "imageDigest": "sha256:74b2c688c700ec95a93e478cdb959737c148df3fbf5ea706abe0318726e885e6", "runtimeId": "ad64cbc71c7fb31c55507ec24c9f77947132b03d48d9961115cf24f3b7307e1e", "taskArn": "arn:aws:ecs:us-west-2:111122223333:task/FargateCluster/c13b4cb40f1f4fe4a2971f76ae5a47ad", "networkInterfaces": [ { "attachmentId": "1789bcae-ddfb-4d10-8ebe-8ac87ddba5b8", "privateIpv4Address": "10.0.0.139" } ], "cpu": "0" } ], "createdAt": "2020-01-23T17:57:34.402Z", "launchType": "FARGATE", "cpu": "256", "memory": "512", "desiredStatus": "RUNNING", "group": "family:sample-fargate", "lastStatus": "RUNNING", "overrides": { "containerOverrides": [ { "name": "FargateApp" } ] }, "connectivity": "CONNECTED", "connectivityAt": "2020-01-23T17:57:38.453Z", "pullStartedAt": "2020-01-23T17:57:52.103Z", "startedAt": "2020-01-23T17:57:58.103Z", "pullStoppedAt": "2020-01-23T17:57:55.103Z", "updatedAt": "2020-01-23T17:57:58.103Z", "taskArn": "arn:aws:ecs:us-west-2:111122223333:task/FargateCluster/c13b4cb40f1f4fe4a2971f76ae5a47ad", "taskDefinitionArn": "arn:aws:ecs:us-west-2:111122223333:task-definition/sample-fargate:1", "version": 4, "platformVersion": "1.3.0" } }