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 中断而停止时会生成任务状态更改事件。

例 任务状态更改事件

任务状态更改事件以下面的形式传送。请注意有关字段的以下内容:

  • 任务状态更改事件中未提供事件的运行状况。如果需要任务运行状况,则可以运行 describe-tasks

  • 当容器使用通过 Amazon ECR 托管的映像时,将返回 imageDigest 字段。

  • createdAtconnectivityAtpullStartedAtstartedAtpullStoppedAtupdatedAt 字段的值是 ISO 字符串时间戳。

  • detail-type 值为“ECS 任务状态更改”。

  • 当为已停止的任务生成事件时,stoppedReasonstoppedCode 字段会提供有关任务停止原因的其他信息(例如“用户启动”)。

有关 EventBridge 参数的更多信息,请参阅《Amazon EventBridge 事件参考》中的 Amazon service event metadata

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

{ "version": "0", "id": "105f6bb1-4da6-c630-4965-35383018cbca", "detail-type": "ECS Task State Change", "source": "aws.ecs", "account": "123456789012", "time": "2025-05-06T11:02:34Z", "region": "us-east-1", "resources": [ "arn:aws:ecs:us-east-1:123456789012:task/example-cluster/a1173316d40a45dea9" ], "detail": { "attachments": [ { "id": "fe3a9a46-6a47-40ee-afd9-7952ae90a75a", "type": "eni", "status": "ATTACHED", "details": [ { "name": "subnetId", "value": "subnet-0d0eab1bb38d5ca64" }, { "name": "networkInterfaceId", "value": "eni-0103a2f01bad57d71" }, { "name": "macAddress", "value": "0e:50:d1:c1:77:81" }, { "name": "privateDnsName", "value": "ip-10-0-1-163.ec2.internal" }, { "name": "privateIPv4Address", "value": "10.0.1.163" } ] } ], "attributes": [ { "name": "ecs.cpu-architecture", "value": "x86_64" } ], "availabilityZone": "us-east-1b", "capacityProviderName": "FARGATE", "clusterArn": "arn:aws:ecs:us-east-1:123456789012:cluster/example-cluster", "connectivity": "CONNECTED", "connectivityAt": "2025-05-06T11:02:17.19Z", "containers": [ { "containerArn": "arn:aws:ecs:us-east-1:123456789012:container/example-cluster/a1173316d40a45dea9/a0a99b87-baa8-4bf6-b9f1-a9a95917a635", "lastStatus": "RUNNING", "name": "web", "image": "nginx", "imageDigest": "sha256:c15da6c91de8d2f436196f3a768483ad32c258ed4e1beb3d367a27ed67253e66", "runtimeId": "a1173316d40a45dea9-0265927825", "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/example-cluster/a1173316d40a45dea9", "networkInterfaces": [ { "attachmentId": "fe3a9a46-6a47-40ee-afd9-7952ae90a75a", "privateIpv4Address": "10.0.1.163" } ], "cpu": "99", "memory": "100" }, { "containerArn": "arn:aws:ecs:us-east-1:123456789012:container/example-cluster/a1173316d40a45dea9/a2010e2d-ba7c-4135-8b79-e0290ff3cd8c", "lastStatus": "RUNNING", "name": "aws-guardduty-agent-nm40lC", "imageDigest": "sha256:bf9197abdf853607e5fa392b4f97ccdd6ca56dd179be3ce8849e552d96582ac8", "runtimeId": "a1173316d40a45dea9-2098416933", "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/example-cluster/a1173316d40a45dea9", "networkInterfaces": [ { "attachmentId": "fe3a9a46-6a47-40ee-afd9-7952ae90a75a", "privateIpv4Address": "10.0.1.163" } ], "cpu": "null" }, { "containerArn": "arn:aws:ecs:us-east-1:123456789012:container/example-cluster/a1173316d40a45dea9/dccf0ca2-d929-471f-a5c3-98006fd4379e", "lastStatus": "RUNNING", "name": "aws-otel-collector", "image": "public.ecr.aws/aws-observability/aws-otel-collector:v0.32.0", "imageDigest": "sha256:7a1b3560655071bcacd66902c20ebe9a69470d5691fe3bd36baace7c2f3c4640", "runtimeId": "a1173316d40a45dea9-4027662657", "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/example-cluster/a1173316d40a45dea9", "networkInterfaces": [ { "attachmentId": "fe3a9a46-6a47-40ee-afd9-7952ae90a75a", "privateIpv4Address": "10.0.1.163" } ], "cpu": "0" } ], "cpu": "256", "createdAt": "2025-05-06T11:02:13.877Z", "desiredStatus": "RUNNING", "enableExecuteCommand": false, "ephemeralStorage": { "sizeInGiB": 20 }, "group": "family:webserver", "launchType": "FARGATE", "lastStatus": "RUNNING", "memory": "512", "overrides": { "containerOverrides": [ { "name": "web" }, { "environment": [ { "name": "CLUSTER_NAME", "value": "example-cluster" }, { "name": "REGION", "value": "us-east-1" }, { "name": "HOST_PROC", "value": "/host_proc" }, { "name": "AGENT_RUNTIME_ENVIRONMENT", "value": "ecsfargate" }, { "name": "STAGE", "value": "prod" } ], "memory": 128, "name": "aws-guardduty-agent-nm40lC" }, { "name": "aws-otel-collector" } ] }, "platformVersion": "1.4.0", "pullStartedAt": "2025-05-06T11:02:24.162Z", "pullStoppedAt": "2025-05-06T11:02:33.493Z", "startedAt": "2025-05-06T11:02:34.325Z", "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/example-cluster/a1173316d40a45dea9", "taskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/webserver:5", "updatedAt": "2025-05-06T11:02:34.325Z", "version": 3 } }

下面是 EC2 启动类型的任务状态更改事件的示例。

{ "version": "0", "id": "a65cf262-f104-0dd5-ceda-4b09ba71a441", "detail-type": "ECS Task State Change", "source": "aws.ecs", "account": "123456789012", "time": "2025-05-12T13:12:06Z", "region": "us-east-1", "resources": [ "arn:aws:ecs:us-east-1:123456789012:task/example/c1ffa94f19a540ed8d9f7e1d2a5d" ], "detail": { "attachments": [ { "id": "52333e3b-b812-41a8-b057-9ed184bbe5e1", "type": "eni", "status": "ATTACHED", "details": [ { "name": "subnetId", "value": "subnet-0d0eab1bb38d5ca64" }, { "name": "networkInterfaceId", "value": "eni-0ea90f746500773a4" }, { "name": "macAddress", "value": "0e:d5:9b:ce:49:fb" }, { "name": "privateDnsName", "value": "ip-10-0-1-37.ec2.internal" }, { "name": "privateIPv4Address", "value": "10.0.1.37" } ] } ], "attributes": [ { "name": "ecs.cpu-architecture", "value": "x86_64" } ], "availabilityZone": "us-east-1b", "capacityProviderName": "Infra-ECS-Cluster-example-fa84e0cc-AsgCapacityProvider-OseQJU9pizmp", "clusterArn": "arn:aws:ecs:us-east-1:123456789012:cluster/example", "connectivity": "CONNECTED", "connectivityAt": "2025-05-12T13:11:44.98Z", "containerInstanceArn": "arn:aws:ecs:us-east-1:123456789012:container-instance/example/d1d84798400f49f3b21cb61610c1e", "containers": [ { "containerArn": "arn:aws:ecs:us-east-1:123456789012:container/example/c1ffa94f19a540ed8d9f7e1d2a5d3626/197d0994-5367-4a6d-9f9a-f075e4a6", "lastStatus": "RUNNING", "name": "aws-otel-collector", "image": "public.ecr.aws/aws-observability/aws-otel-collector:v0.32.0", "imageDigest": "sha256:7a1b3560655071bcacd66902c20ebe9a69470d5691fe3bd36baace7c2f3c4640", "runtimeId": "8e926f0ccd8fe2b459926f49584ba6d33a3d9f61398dbabe944ee6a13a8ff3a1", "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/example/c1ffa94f19a540ed8d9f7e1d2a5d", "networkInterfaces": [ { "attachmentId": "52333e3b-b812-41a8-b057-9ed184bbe5e1", "privateIpv4Address": "10.0.1.37" } ], "cpu": "0" }, { "containerArn": "arn:aws:ecs:us-east-1:123456789012:container/example/c1ffa94f19a540ed8d9f7e1d2a5d3626/cab39ef0-9c50-459d-844b-b9d51d73d", "lastStatus": "RUNNING", "name": "web", "image": "nginx", "imageDigest": "sha256:c15da6c91de8d2f436196f3a768483ad32c258ed4e1beb3d367a27ed67253e66", "runtimeId": "9f1c73f0094f051541d9e5c2ab1e172d83c4eb5171bcc857c4504b02770ff3b8", "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/example/c1ffa94f19a540ed8d9f7e1d2a5d", "networkInterfaces": [ { "attachmentId": "52333e3b-b812-41a8-b057-9ed184bbe5e1", "privateIpv4Address": "10.0.1.37" } ], "cpu": "99", "memory": "100" } ], "cpu": "256", "createdAt": "2025-05-12T13:11:44.98Z", "desiredStatus": "RUNNING", "enableExecuteCommand": false, "group": "family:webserver", "launchType": "EC2", "lastStatus": "RUNNING", "memory": "512", "overrides": { "containerOverrides": [ { "name": "aws-otel-collector" }, { "name": "web" } ] }, "pullStartedAt": "2025-05-12T13:11:59.491Z", "pullStoppedAt": "2025-05-12T13:12:05.896Z", "startedAt": "2025-05-12T13:12:06.053Z", "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/example/c1ffa94f19a540ed8d9f7e1d2a5d", "taskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/webserver", "updatedAt": "2025-05-12T13:12:06.053Z", "version": 4 } }