

# Amazon ECS 容器实例状态更改事件
<a name="ecs_container_instance_events"></a>

以下方案将引起容器实例状态更改事件：

您调用 `StartTask`、`RunTask` 或 `StopTask` API 操作（直接调用，或者通过 AWS 管理控制台或开发工具包调用）。  
在容器实例上放置或停止任务将修改容器实例上的可用资源（例如 CPU、内存和可用端口）。

Amazon ECS 服务调度器启动或停止任务。  
在容器实例上放置或停止任务将修改容器实例上的可用资源（例如 CPU、内存和可用端口）。

针对状态为 `RUNNING` 的任务，Amazon ECS 容器代理调用状态为 `STOPPED` 的 `SubmitTaskStateChange` API 操作。  
Amazon ECS 容器代理监控容器实例上的任务状态，并报告任何状态更改。如果应为 `STOPPED` 的任务转换为 `RUNNING`，则代理将释放分配给已停止任务的资源（例如 CPU、内存和可用端口）。

您可以使用 `DeregisterContainerInstance` API 操作（直接调用，或者通过 AWS 管理控制台或开发工具包调用）注销容器实例。  
注销容器实例将更改容器实例的状态以及 Amazon ECS 容器代理的连接状态。

EC2 实例停止后，任务也停止。  
在停止容器实例时，该实例上运行的任务会转换为 `STOPPED` 状态。

Amazon ECS 容器代理首次注册容器实例。  
当 Amazon ECS 容器代理首次注册容器实例（在启动时注册，或者在首次运行时手动注册）时，将为实例创建状态更改事件。

Amazon ECS Container 代理与 Amazon ECS 连接或断开连接。  
当 Amazon ECS 容器代理与 Amazon ECS 后端连接或断开连接时，将更改容器实例的 `agentConnected` 状态。  
Amazon ECS 容器代理每小时断开连接并重新连接多次，这是其正常操作的一部分，因此代理连接事件应该是可以预期的。这些事件并不表示容器代理或容器实例存在问题。

升级实例上的 Amazon ECS 容器代理。  
容器实例详细信息包含容器代理版本的对象。如果您升级代理，此版本信息将发生更改并且将生成事件。

**Example 容器实例状态更改事件**  
容器实例状态更改事件以下面的形式传送。下面的 `detail` 部分类似于从 *Amazon Elastic Container Service API 参考* 中的 [DescribeContainerInstances](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_DescribeContainerInstances.html) API 操作返回的 [ContainerInstance](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_ContainerInstance.html) 对象。有关 EventBridge 参数的更多信息，请参阅《Amazon EventBridge 用户指南》**中的 [AWS service event metadata](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-events-structure.html)。  

```
{
  "version": "0",
  "id": "8952ba83-7be2-4ab5-9c32-6687532d15a2",
  "detail-type": "ECS Container Instance State Change",
  "source": "aws.ecs",
  "account": "111122223333",
  "time": "2016-12-06T16:41:06Z",
  "region": "us-east-1",
  "resources": [
    "arn:aws:ecs:us-east-1:111122223333:container-instance/b54a2a04-046f-4331-9d74-3f6d7f6ca315"
  ],
  "detail": {
    "agentConnected": true,
    "attributes": [
      {
        "name": "com.amazonaws.ecs.capability.logging-driver.syslog"
      },
      {
        "name": "com.amazonaws.ecs.capability.task-iam-role-network-host"
      },
      {
        "name": "com.amazonaws.ecs.capability.logging-driver.awslogs"
      },
      {
        "name": "com.amazonaws.ecs.capability.logging-driver.json-file"
      },
      {
        "name": "com.amazonaws.ecs.capability.docker-remote-api.1.17"
      },
      {
        "name": "com.amazonaws.ecs.capability.privileged-container"
      },
      {
        "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18"
      },
      {
        "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19"
      },
      {
        "name": "com.amazonaws.ecs.capability.ecr-auth"
      },
      {
        "name": "com.amazonaws.ecs.capability.docker-remote-api.1.20"
      },
      {
        "name": "com.amazonaws.ecs.capability.docker-remote-api.1.21"
      },
      {
        "name": "com.amazonaws.ecs.capability.docker-remote-api.1.22"
      },
      {
        "name": "com.amazonaws.ecs.capability.docker-remote-api.1.23"
      },
      {
        "name": "com.amazonaws.ecs.capability.task-iam-role"
      }
    ],
    "clusterArn": "arn:aws:ecs:us-east-1:111122223333:cluster/default",
    "containerInstanceArn": "arn:aws:ecs:us-east-1:111122223333:container-instance/b54a2a04-046f-4331-9d74-3f6d7f6ca315",
    "ec2InstanceId": "i-f3a8506b",
    "registeredResources": [
      {
        "name": "CPU",
        "type": "INTEGER",
        "integerValue": 2048
      },
      {
        "name": "MEMORY",
        "type": "INTEGER",
        "integerValue": 3767
      },
      {
        "name": "PORTS",
        "type": "STRINGSET",
        "stringSetValue": [
          "22",
          "2376",
          "2375",
          "51678",
          "51679"
        ]
      },
      {
        "name": "PORTS_UDP",
        "type": "STRINGSET",
        "stringSetValue": []
      }
    ],
    "remainingResources": [
      {
        "name": "CPU",
        "type": "INTEGER",
        "integerValue": 1988
      },
      {
        "name": "MEMORY",
        "type": "INTEGER",
        "integerValue": 767
      },
      {
        "name": "PORTS",
        "type": "STRINGSET",
        "stringSetValue": [
          "22",
          "2376",
          "2375",
          "51678",
          "51679"
        ]
      },
      {
        "name": "PORTS_UDP",
        "type": "STRINGSET",
        "stringSetValue": []
      }
    ],
    "status": "ACTIVE",
    "version": 14801,
    "versionInfo": {
      "agentHash": "aebcbca",
      "agentVersion": "1.13.0",
      "dockerVersion": "DockerVersion: 1.11.2"
    },
    "updatedAt": "2016-12-06T16:41:06.991Z"
  }
}
```