Amazon ECS task state change events
The following scenarios cause task state change events:
- You call the
StartTask
,RunTask
, orStopTask
API operations, either directly or with the AWS Management Console, AWS CLI, or SDKs. -
Starting or stopping tasks creates new task resources or modifies the state of existing task resources.
- The Amazon ECS service scheduler starts or stops a task.
-
Starting or stopping tasks creates new task resources or modifies the state of existing task resources.
- The Amazon ECS container agent calls the
SubmitTaskStateChange
API operation. -
For the Amazon EC2 launch type, the Amazon ECS container agent monitors the state of your tasks on your container instances. The Amazon ECS container agent reports any state changes. State changes might include changes from
PENDING
toRUNNING
or fromRUNNING
toSTOPPED
.
- You force deregistration of the underlying container instance with the
DeregisterContainerInstance
API operation and theforce
flag, either directly or with the AWS Management Console or SDKs. -
Deregistering a container instance changes the status of the container instance and the connection status of the Amazon ECS container agent. If tasks are running on the container instance, the
force
flag must be set to allow deregistration. This stops all tasks on the instance. - The underlying container instance is stopped or terminated.
-
When you stop or terminate a container instance, the tasks that are running on it are transitioned to the
STOPPED
status. - A container in the task changes state.
-
The Amazon ECS container agent monitors the state of containers within tasks. For example, if a container that is running within a task stops, this container state change generates an event.
- A task using the Fargate Spot capacity provider receives a termination notice.
-
When a task is using the
FARGATE_SPOT
capacity provider and is stopped due to a Spot interruption, a task state change event is generated.
Example Task state change event
Task state change events are delivered in the following format. The
detail
section below resembles the Task object that is returned from a
DescribeTasks API
operation in the Amazon Elastic Container Service API Reference. If your containers are
using an image hosted with Amazon ECR, the imageDigest
field is
returned.
Note
The values for the createdAt
, connectivityAt
,
pullStartedAt
, startedAt
,
pullStoppedAt
, and updatedAt
fields are UNIX
timestamps in the response of a DescribeTasks
action whereas in
the task state change event they are ISO string timestamps.
For more information about CloudWatch Events parameters, see Events and Event Patterns in the Amazon EventBridge User Guide.
For information about how to configure configure an Amazon EventBridge event rule that only captures task events where the task has stopped running because one of its essential containers has terminated, see Sending Amazon Simple Notification Service alerts for Amazon ECS task stopped events
{
"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"
}
}