

# Amazon EC2 实例的计划事件
<a name="monitoring-instances-status-check_sched"></a>

为确保基础设施的可靠性和性能，AWS 可以计划事件来重启、停止和停用您的实例。这些事件不会频繁发生。

如果您的一个实例将受某计划事件影响，则 AWS 将使用与您的 AWS 账户关联的电子邮件地址提前通知您。该电子邮件将提供有关该事件的详细信息，包括开始和结束日期。根据事件类型的不同，您也许能够执行操作来控制事件的发生时间。AWS 还会发送 AWS Health 事件，您可以使用 Amazon EventBridge 对该事件进行监控和管理。有关更多信息，请参阅 [Monitoring events in AWS Health with Amazon EventBridge](https://docs.aws.amazon.com/health/latest/ug/cloudwatch-events-health.html)。

计划的事件由 AWS 管理。您无法为实例计划事件。不过，您可以：
+ 查看实例的计划事件。
+ 您可以自定义计划事件通知，以便在电子邮件通知中包含或删除标签。
+ 重新计划某些已计划的事件。
+ 为计划事件创建自定义事件窗口。
+ 在实例按计划重启、停止或停用时执行操作。

为确保您会收到计划事件的通知，请在[账户](https://console.aws.amazon.com/billing/home?#/account)页面上验证您的联系信息。

**注意**  
当实例受计划事件的影响并且它属于自动扩缩组的一部分时，Amazon EC2 Auto Scaling 最终将其替换为其运行状况检查的一部分，您无需采取进一步措施。有关由 Amazon EC2 Auto Scaling 执行的运行状况检查的更多信息，请参阅《*Amazon EC2 Auto Scaling 用户指南*》中的 [Health checks for instances in an Auto Scaling group](https://docs.aws.amazon.com/autoscaling/ec2/userguide/ec2-auto-scaling-health-checks.html)。

## 计划的事件类型
<a name="types-of-scheduled-events"></a>

Amazon EC2 可以创建以下类型的计划实例事件，这些事件会在计划的时间发生：


| 事件类型 | 事件代码 | 事件操作 | 
| --- | --- | --- | 
| 实例停止 | instance-stop | 实例将在计划的时间停止。再次启动实例时，实例会迁移至新主机。仅适用于具有 Amazon EBS 根卷的实例。 | 
| 实例指令引退 | instance-retirement | 如果实例具有 Amazon EBS 根卷，则实例将在计划的时间停止；如果实例具有实例存储根卷，则实例将在计划的时间终止。 | 
| 实例重启 | instance-reboot | 实例将在计划的时间重启。实例将保留在主机上，并且主机将在重启期间进行维护。这称为就地重启。 | 
| 系统重启 | system-reboot | 实例将在计划的时间重启并迁移至新的主机。这称为重启迁移。 | 
| 系统维护 | system-maintenance | 实例可能会在计划的时间因网络维护或电源维护而临时受到影响。 | 

## 确定事件类型
<a name="scheduled-event-type"></a>

您可以检查为实例计划的事件类型。

------
#### [ Console ]

**确定事件类型**

1. 通过以下网址打开 Amazon EC2 控制台：[https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)。

1. 在导航窗格中，选择 **Events**。

1. 事件代码会显示在表中的**事件类型**列中。

1. 要对该表进行筛选以仅显示涉及相关实例的事件，请在搜索字段的筛选条件列表中选择**资源类型：实例**。

------
#### [ AWS CLI ]

**确定实例的事件类型**  
使用 [describe-instance-status](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instance-status.html) 命令。如果实例具有关联的计划事件，则输出会提供有关该计划事件的信息。

```
aws ec2 describe-instance-status \
    --instance-id i-1234567890abcdef0 \
    --query InstanceStatuses[].Events
```

下面是示例输出。计划的事件代码是 `system-reboot`。

```
[
    "Events": [
        {
            "InstanceEventId": "instance-event-0d59937288b749b32",
            "Code": "system-reboot",
            "Description": "The instance is scheduled for a reboot",
            "NotAfter": "2020-03-14T22:00:00.000Z",
            "NotBefore": "2020-03-14T20:00:00.000Z",
            "NotBeforeDeadline": "2020-04-05T11:00:00.000Z"
        }
    ]
]
```

------
#### [ PowerShell ]

**确定实例的事件类型**  
使用 [Get-EC2InstanceStatus](https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2InstanceStatus.html) cmdlet。如果实例具有关联的计划事件，则输出会提供有关该计划事件的信息。

```
(Get-EC2InstanceStatus `
    -InstanceId i-1234567890abcdef0).Events
```

下面是示例输出。计划的事件代码是 `system-reboot`。

```
Code              : system-reboot
Description       : The instance is scheduled for a reboot
InstanceEventId   : instance-event-0d59937288b749b32
NotAfter          : 2020-03-14T22:00:00.000Z
NotBefore         : 2020-03-14T20:00:00.000Z
NotBeforeDeadline : 2020-04-05T11:00:00.000Z
```

------

**Topics**
+ [计划的事件类型](#types-of-scheduled-events)
+ [确定事件类型](#scheduled-event-type)
+ [管理计划停止或停用的 Amazon EC2 实例](schedevents_actions_retire.md)
+ [管理计划重启的 Amazon EC2 实例](schedevents_actions_reboot.md)
+ [管理计划维护的 Amazon EC2 实例](schedevents_actions_maintenance.md)
+ [查看会影响 Amazon EC2 实例的计划事件](viewing_scheduled_events.md)
+ [针对 EC2 实例自定义计划事件通知](customizing_scheduled_event_notifications.md)
+ [重新计划 EC2 实例的计划事件](reschedule-event.md)
+ [为会影响 Amazon EC2 实例的计划事件创建自定义事件窗口](event-windows.md)

# 管理计划停止或停用的 Amazon EC2 实例
<a name="schedevents_actions_retire"></a>

当 AWS 检测到您的实例的基础主机存在无法修复的故障时，它会计划停止或终止实例，这取决于实例的根卷类型。
+ 如果实例具有 Amazon EBS 根卷，则将计划停止实例。
+ 如果实例具有实例存储根卷，则将计划终止实例。

有关更多信息，请参阅 [实例指令引退](instance-retirement.md)。

**重要**  
实例停止、休眠或终止后，实例存储卷上存储的所有数据都会丢失。这包括附加到具有 EBS 根卷的实例的实例存储卷。在实例停止、休眠或终止之前，请务必保存实例存储卷中以后可能需要的数据。

## 您可以执行的操作
<a name="actions-you-can-take-for-scheduled-stop-or-retire-event"></a>

**可以对具有 EBS 根卷的实例执行的操作**

收到计划 `instance-stop` 事件通知时，您可以执行以下操作之一：
+ **等待计划停止：**您可等待实例在其计划维护时段内停止。
+ **执行手动停止并启动：**您也可在适合的时间自行停止并启动实例，这会将实例迁移至新的主机。这与重启实例不同。有关更多信息，请参阅 [启动和停止 Amazon EC2 实例](Stop_Start.md)。
+ **自动停止并启动：**您可以自动立即停止并启动以响应计划的 `instance-stop` 事件。有关更多信息，请参阅《*AWS Health 用户指南*》中的[自动在 EC2 实例上运行操作以响应 AWS Health 中的事件](https://docs.aws.amazon.com/health/latest/ug/automating-instance-actions.html)。

**可以对具有实例存储根卷的实例执行的操作**

收到计划 `system-retirement` 事件通知，并且您希望保留数据时，您可以执行以下操作之一：

1. 从最新的 AMI 启动替换实例。

1. 在实例按计划终止之前，将所有必需的数据迁移至替换实例。

1. 终止原始实例，或等待其按计划终止。

有关您可以执行的操作的更多信息，请参阅[实例指令引退](instance-retirement.md)。

# 管理计划重启的 Amazon EC2 实例
<a name="schedevents_actions_reboot"></a>

当 AWS 必须执行安装更新或维护底层主机等任务时，可以计划实例重启。在计划重启期间，实例可以保留在同一主机上，也可迁移到其他主机，具体取决于事件，如下所示：
+ `instance-reboot` 事件
  + 在重启期间，实例仍保留在主机上。这称为*就地重启*。
  + 当前主机正在执行维护。
  + 这通常会在几秒钟内完成。
+ `system-reboot` 事件
  + 实例将在重启期间迁移至新的主机。这称为*重启迁移*，
  + 通常在几分钟内完成。

要检查为实例安排计划的事件类型，请参阅[确定事件类型](monitoring-instances-status-check_sched.md#scheduled-event-type)。

## 您可以执行的操作
<a name="actions-you-can-take-for-scheduled-reboot-event"></a>

收到计划的 `instance-reboot` 或 `system-reboot` 事件通知时，您可以执行以下操作之一：
+ **等待计划重启：**您可以等待实例在计划的维护时段内重启。
+ **将计划重启改期：**您可以将实例重启[改期](reschedule-event.md)为适合自己的其他日期和时间。
+ **执行用户发起的重启：**您可以在自己方便的时间自行手动[重启](ec2-instance-reboot.md)实例。但结果因事件而异：
  + `instance-reboot` 事件：实例将保留在当前硬件上（就地重启），不会进行主机维护，并且事件将保持打开状态。
  + `system-reboot` 事件
    + 如果实例启用了重启迁移，则用户发起的重启会尝试将实例迁移到新硬件上。如果操作成功，事件将被清除。如果操作不成功，则会执行就地重启并保留计划事件。
    + 如果实例禁用了重启迁移，则用户发起的重启会使实例保留在相同的硬件上（就地重启），不会进行主机维护但会保留计划事件。当计划事件最终发生时，AWS 会将实例移至新硬件（重启迁移）。

**在 AWS 重启实例后**

在 AWS 重启实例后，将会发生以下情况：
+ 该计划事件将被清除。
+ 事件描述将被更新。
+ 对于 `instance-reboot` 事件：
  + 底层主机的维护将会完成。
+ 对于 `system-reboot` 事件：
  + 实例将迁移至新主机。
  + 实例将保留其 IP 地址和 DNS 名称。
  + 本地实例存储卷上的所有数据都会保留。
+ 您可在实例完全启动之后使用实例。

**替代方案**

如果无法将重启事件改期或为用户发起的重启启用重启迁移，但希望在计划维护时段内保持正常运行，则可以执行以下操作：
+ **对于具有 EBS 根卷的实例**
  + 手动停止并启动实例，以将其迁移至新的主机。这与手动重启实例不同，手动重启时实例会保留在同一主机上。
  + 此外也可以自动立即停止并启动实例以响应计划重启事件。有关更多信息，请参阅《*AWS Health 用户指南*》中的[自动在 EC2 实例上运行操作以响应 AWS Health 中的事件](https://docs.aws.amazon.com/health/latest/ug/automating-instance-actions.html)。
**重要**  
停止实例后，实例存储卷上的数据将会丢失。有关更多信息，请参阅 [启动和停止 Amazon EC2 实例](Stop_Start.md)。
+ **对于具有实例存储根卷的实例**

  1. 从最新的 AMI 启动替换实例。

  1. 在计划维护时段开始之前，将所有必需的数据迁移至替换实例。

  1. 终止原始实例。

## 启用或禁用重启迁移
<a name="reboot-migration"></a>

在为实例计划某个 `system-reboot` 事件后，您可以在事件到来之前重启该实例。用户发起的重启的结果取决于实例的重启迁移设置：
+ 已启用：用户发起的重启会尝试将实例迁移到新硬件上（重启迁移）。如果操作成功，事件将被清除。如果操作不成功，则会执行就地重启并保留计划事件。请注意，即使启用了重启迁移，也只能在实例满足[重启迁移要求](#requirements-for-reboot-migration)的前提下进行重启迁移。
+ 已禁用：用户发起的重启会使实例保留在相同的硬件上（就地重启），不会进行主机维护但会保留计划事件。当计划事件最终发生时，AWS 会将实例移至新硬件（重启迁移）。

迁移后重启所需的时间比就地重启的时间长：
+ 就地重启：大约 30 秒
+ 迁移后重启：几分钟

**注意**  
收到 `system-reboot` 事件通知的实例会为用户发起的重启默认启用此功能。

### 启用重启迁移的要求
<a name="requirements-for-reboot-migration"></a>

只有满足以下条件的实例才能启用重启迁移：

**实例类型**  
并非所有实例类型都支持启用重启迁移。您可以查看支持启用重启迁移的实例类型。  

**查看支持启用重启迁移的实例类型**

1. 通过以下网址打开 Amazon EC2 控制台：[https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)。

1. 在左侧导航窗格中，选择 **Instance Types**（实例类型）。

1. 在筛选条件栏中，输入**重启迁移支持：已支持**。选择输入字符时出现的筛选条件名称。

   **实例类型**表会显示支持启用重启迁移的所有实例类型。
**查看支持启用重启迁移的实例类型**  
使用 [describe-instance-types](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instance-types.html) 命令和 `reboot-migration-support` 筛选条件。

```
aws ec2 describe-instance-types \
    --filters Name=reboot-migration-support,Values=supported \
    --query "InstanceTypes[*].[InstanceType]" \
    --output text | sort
```
**查看支持启用重启迁移的实例类型**  
使用 [Get-EC2InstanceType](https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2InstanceType.html) cmdlet 和 `reboot-migration-support` 筛选条件。

```
Get-EC2InstanceType `
    -Filter @{Name="reboot-migration-support";Values="true"} | `
    Select InstanceType | Sort-Object InstanceType
```

**租赁**  
+ 已共享
+ 专用实例
有关更多信息，请参阅 [Amazon EC2 专用实例](dedicated-instance.md)。

**限制**

具有以下特征的实例**不**支持重启迁移：
+ 平台：在 Xen 虚拟机监控程序上原生运行的实例
+ 实例大小：`metal` 实例
+ 租赁：专属主机。对专属主机使用[专属主机自动恢复](dedicated-hosts-recovery.md)
+ 存储：具有实例存储卷的实例
+ 网络：使用 Elastic Fabric Adapter 的实例
+ 自动扩缩：属于自动扩缩组的实例

### 启用或禁用重启迁移的步骤
<a name="configure-reboot-migration-behavior"></a>

实例收到 `system-reboot` 事件后，则会默认启用重启迁移。您可以禁用重启迁移，以便在用户发起的重启期间将实例保留在相同的硬件上（就地重启）。

`default` 配置不能为不受支持的实例启用重启迁移。有关更多信息，请参阅 [启用重启迁移的要求](#requirements-for-reboot-migration)。

您可以在正在运行或已停止的实例上禁用或启用重启迁移。

------
#### [ AWS CLI ]

**禁用重启迁移**  
使用e [modify-instance-maintenance-options](https://docs.aws.amazon.com/cli/latest/reference/ec2/modify-instance-maintenance-options.html) 命令，并将 `--reboot-migration` 参数设置为 `disabled`。

```
aws ec2 modify-instance-maintenance-options \ 
    --instance-id i-0abcdef1234567890 \ 
    --reboot-migration disabled
```

**启用重启迁移**  
使用e [modify-instance-maintenance-options](https://docs.aws.amazon.com/cli/latest/reference/ec2/modify-instance-maintenance-options.html) 命令，并将 `--reboot-migration` 参数设置为 `default`。

```
aws ec2 modify-instance-maintenance-options \ 
    --instance-id i-0abcdef1234567890 \ 
    --reboot-migration default
```

------
#### [ PowerShell ]

**禁用重启迁移**  
使用 [Edit-EC2InstanceMaintenanceOption](https://docs.aws.amazon.com/powershell/latest/reference/items/Edit-EC2InstanceMaintenanceOption.html) cmdlet。

```
Edit-EC2InstanceMaintenanceOption `
    -InstanceId  `
    -RebootMigration Disabled
```

**启用重启迁移**  
使用 [Edit-EC2InstanceMaintenanceOption](https://docs.aws.amazon.com/powershell/latest/reference/items/Edit-EC2InstanceMaintenanceOption.html) cmdlet。

```
Edit-EC2InstanceMaintenanceOption `
    -InstanceId i-1234567890abcdef0 `
    -RebootMigration Enabled
```

------

# 管理计划维护的 Amazon EC2 实例
<a name="schedevents_actions_maintenance"></a>

当 AWS 必须维护实例的基础主机时，它将计划实例的维护。维护事件有两种：网络维护和电源维护。
+ 在网络维护期间，计划的实例会在短时间内失去网络连接。在维护完成后，将恢复与实例的正常网络连接。
+ 在电源维护期间，计划的实例将短时间脱机，然后重启。执行重启后，将保留您的所有实例的配置设置。

在实例重启后 (这通常需要几分钟)，验证您的应用程序是否按预期运行。此时，您的实例应该不再具有与之关联的计划事件，或者如果有，该计划事件的描述应该以 **[Completed]** 开头。实例状态说明的刷新有时需要长达 1 个小时。已完成的维护事件将在 Amazon EC2 控制台面板上显示长达一周时间。

## 您可以执行的操作
<a name="actions-you-can-take-for-scheduled-maintenance-event"></a>

**可以对具有 EBS 根卷的实例执行的操作**

收到 `system-maintenance` 事件通知时，您可以执行以下操作之一：
+ **等待执行计划维护：**您可等待按计划执行维护。
+ **执行手动停止并启动：**您可以停止并启动实例，这会将实例迁移至新的主机。这与重启实例不同。有关更多信息，请参阅 [启动和停止 Amazon EC2 实例](Stop_Start.md)。
+ **自动停止并启动：**您可以自动立即停止并启动以响应计划维护事件。有关更多信息，请参阅《*AWS Health 用户指南*》中的[自动在 EC2 实例上运行操作以响应 AWS Health 中的事件](https://docs.aws.amazon.com/health/latest/ug/automating-instance-actions.html)。

**可以对具有实例存储根卷的实例执行的操作**

收到 `system-maintenance` 事件通知时，您可以执行以下操作之一：
+ **等待执行计划维护：**您可等待按计划执行维护。
+ **启动替换实例：**如果想在计划维护时段内保持正常运行，请执行以下操作：

  1. 从最新的 AMI 启动替换实例。

  1. 在计划维护时段开始之前，将所有必需的数据迁移至替换实例。

  1. 终止原始实例。

# 查看会影响 Amazon EC2 实例的计划事件
<a name="viewing_scheduled_events"></a>

除了通过电子邮件接收计划事件的通知外，您还可以查看计划的事件。

------
#### [ Console ]

**查看实例的计划事件**

1. 通过以下网址打开 Amazon EC2 控制台：[https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)。

1. 控制面板的**计划的事件**下将显示与事件关联的所有资源。  
![\[使用控制面板查看事件。\]](http://docs.aws.amazon.com/zh_cn/AWSEC2/latest/UserGuide/images/dashboard-scheduled-events.png)

1. 有关更多详细信息，在导航窗格中，选择**事件**。将显示与事件关联的所有资源。您可以按事件类型、资源类型和可用区等特征进行筛选。  
![\[使用“Events”页查看事件。\]](http://docs.aws.amazon.com/zh_cn/AWSEC2/latest/UserGuide/images/events-instance-scheduled-stop.png)

------
#### [ AWS CLI ]

**查看实例的计划事件**  
使用 [describe-instance-status](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instance-status.html) 命令。

```
aws ec2 describe-instance-status \
    --instance-id i-1234567890abcdef0 \
    --query "InstanceStatuses[].Events"
```

以下示例输出显示重启事件：

```
[
    "Events": [
        {
            "InstanceEventId": "instance-event-0d59937288b749b32",
            "Code": "system-reboot",
            "Description": "The instance is scheduled for a reboot",
            "NotAfter": "2019-03-15T22:00:00.000Z",
            "NotBefore": "2019-03-14T20:00:00.000Z",
            "NotBeforeDeadline": "2019-04-05T11:00:00.000Z"
         }

    ]
]
```

以下示例输出显示实例停用事件。

```
[
    "Events": [
        {
            "InstanceEventId": "instance-event-0e439355b779n26",                        
            "Code": "instance-stop",
            "Description": "The instance is running on degraded hardware",
            "NotBefore": "2015-05-23T00:00:00.000Z"
        }
    ]
]
```

------
#### [ PowerShell ]

**查看实例的计划事件**  
使用以下 [Get-EC2InstanceStatus](https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2InstanceStatus.html) 命令。

```
(Get-EC2InstanceStatus -InstanceId i-1234567890abcdef0).Events
```

以下示例输出显示实例停用事件。

```
Code         : instance-stop
Description  : The instance is running on degraded hardware
NotBefore    : 5/23/2015 12:00:00 AM
```

------
#### [ Instance metadata ]

**使用实例元数据查看实例的计划事件**  
您可以使用实例元数据服务版本 2 或实例元数据服务版本 1 从[实例元数据](ec2-instance-metadata.md)中检索有关实例的活动维护事件的信息。

**IMDSv2**

```
[ec2-user ~]$ TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` \
&& curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/events/maintenance/scheduled
```

**IMDSv1**

```
[ec2-user ~]$ curl http://169.254.169.254/latest/meta-data/events/maintenance/scheduled
```

以下是 JSON 格式的计划系统重启事件信息的示例输出。

```
[ 
  {
    "NotBefore" : "21 Jan 2019 09:00:43 GMT",
    "Code" : "system-reboot",
    "Description" : "scheduled reboot",
    "EventId" : "instance-event-0d59937288b749b32",
    "NotAfter" : "21 Jan 2019 09:17:23 GMT",
    "State" : "active"
  } 
]
```

**使用实例元数据查看有关实例的已完成或已取消事件的事件历史记录**  
您可以使用实例元数据服务版本 2 或实例元数据服务版本 1 从[实例元数据](ec2-instance-metadata.md)中检索有关已完成或已取消的事件的信息。

**IMDSv2**

```
[ec2-user ~]$ TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` \
&& curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/events/maintenance/history
```

**IMDSv1**

```
[ec2-user ~]$ curl http://169.254.169.254/latest/meta-data/events/maintenance/history
```

以下是 JSON 格式的已取消和已完成系统重启事件相关信息的示例输出。

```
[ 
  {
    "NotBefore" : "21 Jan 2019 09:00:43 GMT",
    "Code" : "system-reboot",
    "Description" : "[Canceled] scheduled reboot",
    "EventId" : "instance-event-0d59937288b749b32",
    "NotAfter" : "21 Jan 2019 09:17:23 GMT",
    "State" : "canceled"
  }, 
  {
    "NotBefore" : "29 Jan 2019 09:00:43 GMT",
    "Code" : "system-reboot",
    "Description" : "[Completed] scheduled reboot",
    "EventId" : "instance-event-0d59937288b749b32",
    "NotAfter" : "29 Jan 2019 09:17:23 GMT",
    "State" : "completed"
  }
]
```

------
#### [ AWS Health ]

您可以使用 AWS Health Dashboard 了解可能影响您的实例的事件。Health Dashboard 会分三组管理问题：未处理问题、已计划更改和其他通知。已计划更改组包含正在进行或即将进行的项目。

有关更多信息，请参阅 *AWS Health 用户指南*中的[开始使用 AWS Health Dashboard](https://docs.aws.amazon.com/health/latest/ug/getting-started-health-dashboard.html)。

------

# 针对 EC2 实例自定义计划事件通知
<a name="customizing_scheduled_event_notifications"></a>

您可以自定义计划事件通知，以便在电子邮件通知中包含标签。这样就可以更轻松地识别受影响的资源（实例或 专用主机），并为即将到来的事件确定操作的优先级。

当您自定义事件通知以包含标签时，您可以选择包括：
+ 与受影响资源关联的所有标签
+ 仅限与受影响资源关联的特定标签

例如，假设您为所有实例分配 `application`、`costcenter`、`project` 和 `owner` 标签。您可以选择在事件通知中包含所有标签。或者，如果您只想在事件通知中查看 `owner` 和 `project` 标签，则可以选择仅包含这些标签。

选择要包含的标签后，事件通知将包含与受影响资源关联的资源 ID（实例 ID 或专用主机 ID）以及标签键/值对。

**Topics**
+ [在事件通知中包含标签](#register-tags)
+ [从事件通知中删除标签](#deregister-tags)
+ [查看要包含在事件通知中的标签](#view-tags)

## 在事件通知中包含标签
<a name="register-tags"></a>

您选择包含的标签将应用于选定区域中的所有资源（实例和 专用主机）。要自定义其他区域中的事件通知，请首先选择所需的区域，然后执行以下步骤。

------
#### [ Console ]

**在事件通知中包含标签**

1. 通过以下网址打开 Amazon EC2 控制台：[https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)。

1. 在导航窗格中，选择 **Events**。

1. 选择**操作**、**Manage event notifications (管理事件通知)**。

1. 启用**在事件通知中包含标签**。

1. 根据要包含在事件通知中的标签，执行以下操作之一：
   + 要包含与受影响实例或专属主机关联的所有标签，请选择**包含所有标签**。
   + 要选择要包含的标签，请选择**选择要包含的标签**，然后选择或输入标签键。

1. 选择**保存**。

------
#### [ AWS CLI ]

**在事件通知中包含所有标签**  
使用 [register-instance-event-notification-attributes](https://docs.aws.amazon.com/cli/latest/reference/ec2/register-instance-event-notification-attributes.html) 命令，并将 `IncludeAllTagsOfInstance` 参数设置为 `true`。

```
aws ec2 register-instance-event-notification-attributes \
    --instance-tag-attribute "IncludeAllTagsOfInstance=true"
```

**在事件通知中包含特定标签**  
使用 [register-instance-event-notification-attributes](https://docs.aws.amazon.com/cli/latest/reference/ec2/register-instance-event-notification-attributes.html) 命令，并使用 `InstanceTagKeys` 参数指定要包含的标签。

```
aws ec2 register-instance-event-notification-attributes \
    --instance-tag-attribute 'InstanceTagKeys=["tag_key_1", "tag_key_2", "tag_key_3"]'
```

------
#### [ PowerShell ]

**在事件通知中包含所有标签**  
使用 [Register-EC2InstanceEventNotificationAttribute](https://docs.aws.amazon.com/powershell/latest/reference/items/Register-EC2InstanceEventNotificationAttribute.html) cmdlet。

```
Register-EC2InstanceEventNotificationAttribute `
    -InstanceTagAttribute_IncludeAllTagsOfInstance $true
```

**在事件通知中包含特定标签**  
使用 [Register-EC2InstanceEventNotificationAttribute](https://docs.aws.amazon.com/powershell/latest/reference/items/Register-EC2InstanceEventNotificationAttribute.html) cmdlet。

```
Register-EC2InstanceEventNotificationAttribute `
    -InstanceTagAttribute_InstanceTagKey tag_key_1, tag_key_2, tag_key_3
```

------

## 从事件通知中删除标签
<a name="deregister-tags"></a>

您可以从事件通知中删除标签。

------
#### [ Console ]

**从事件通知中删除标签**

1. 通过以下网址打开 Amazon EC2 控制台：[https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)。

1. 在导航窗格中，选择 **Events**。

1. 选择**操作**、**Manage event notifications (管理事件通知)**。

1. 要从事件通知中删除所有标签，请关闭**在事件通知中包含标签**。

1. 要从事件通知中删除特定标签，请为相应的标签键选择 **X**)。

1. 选择**保存**。

------
#### [ AWS CLI ]

**从事件通知中删除所有标签**  
使用 [deregister-instance-event-notification-attributes](https://docs.aws.amazon.com/cli/latest/reference/ec2/deregister-instance-event-notification-attributes.html) 命令，并将 `IncludeAllTagsOfInstance` 参数设置为 `false`。

```
aws ec2 deregister-instance-event-notification-attributes \
    --instance-tag-attribute "IncludeAllTagsOfInstance=false"
```

**从事件通知中删除标签**  
使用 [deregister-instance-event-notification-attributes](https://docs.aws.amazon.com/cli/latest/reference/ec2/deregister-instance-event-notification-attributes.html) 命令，并使用 `InstanceTagKeys` 参数指定要删除的标签。

```
aws ec2 deregister-instance-event-notification-attributes \
    --instance-tag-attribute 'InstanceTagKeys=["tag_key_3"]'
```

------
#### [ PowerShell ]

**从事件通知中删除所有标签**  
使用 [Unregister-EC2InstanceEventNotificationAttribute](https://docs.aws.amazon.com/powershell/latest/reference/items/Unregister-EC2InstanceEventNotificationAttribute.html) cmdlet。

```
Unregister-EC2InstanceEventNotificationAttribute `
    -InstanceTagAttribute_IncludeAllTagsOfInstance $false
```

**从事件通知中删除标签**  
使用 [Unregister-EC2InstanceEventNotificationAttribute](https://docs.aws.amazon.com/powershell/latest/reference/items/Unregister-EC2InstanceEventNotificationAttribute.html) cmdlet。

```
Unregister-EC2InstanceEventNotificationAttribute `
    -InstanceTagAttribute_InstanceTagKey tag_key_3
```

------

## 查看要包含在事件通知中的标签
<a name="view-tags"></a>

您可以查看要包含在事件通知中的标签。

------
#### [ Console ]

**查看要包含在事件通知中的标签**

1. 通过以下网址打开 Amazon EC2 控制台：[https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)。

1. 在导航窗格中，选择 **Events**。

1. 选择**操作**、**Manage event notifications (管理事件通知)**。

------
#### [ AWS CLI ]

**查看要包含在事件通知中的标签**  
使用 [describe-instance-event-notification-attributes](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instance-event-notification-attributes.html) 命令。

```
aws ec2 describe-instance-event-notification-attributes
```

------
#### [ PowerShell ]

**查看要包含在事件通知中的标签**  
使用 [Get-EC2InstanceEventNotificationAttribute](https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2InstanceEventNotificationAttribute.html) cmdlet。

```
Get-EC2InstanceEventNotificationAttribute
```

------

# 重新计划 EC2 实例的计划事件
<a name="reschedule-event"></a>

您可以重新安排一个事件，以便它在适合您的特定日期和时间发生。重新计划事件后，可能需要一两分钟才能显示更新后的日期。

**限制**
+ 只有具有事件截止日期的事件才可以重新计划。可以将事件重新计划到事件截止日期之前的日期。**截止期限**列（控制台）和 `NotBeforeDeadline` 字段（AWS CLI）会指示事件的截止日期。
+ 只有尚未开始的事件才可以重新计划。**开始时间**列（控制台）和 `NotBefore` 字段（AWS CLI）会指示事件的开始时间。距离开始时间只剩 5 分钟的计划事件无法改期。
+ 新的事件开始时间离当前时间必须至少有 60 分钟。
+ 如果您使用控制台重新计划多个事件，则事件截止期限由具有最早的事件截止期限的事件决定。

------
#### [ Console ]

**重新安排事件**

1. 通过以下网址打开 Amazon EC2 控制台：[https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)。

1. 在导航窗格中，选择 **Events**。

1. 从筛选器列表中选择 **Resource type: instance (资源类型：实例)**。

1. 选择一个或多个实例，然后依次选择 **Actions (操作)**、**Schedule Event (计划事件)**。

   只有具有事件截止期限（由 **Deadline (截止期限)** 值指示）的事件才可以重新计划。如果某个选定的事件没有截止期限，则会禁用 **Actions (操作)** 下的 **Schedule Event (计划事件)**。

1. 对于 **New start time (新的开始时间)**，为事件输入新的日期和时间。新的日期和时间必须早于 **Event deadline (事件截止期限)**。

1. 选择**保存**。

   更新的事件开始时间可能需要一两分钟才会反映在控制台中。

------
#### [ AWS CLI ]

**重新安排事件**  
使用 [https://docs.aws.amazon.com/cli/latest/reference/ec2/modify-instance-event-start-time.html](https://docs.aws.amazon.com/cli/latest/reference/ec2/modify-instance-event-start-time.html) 命令。

```
aws ec2 modify-instance-event-start-time \
    --instance-id i-1234567890abcdef0 \
    --instance-event-id instance-event-0d59937288b749b32 \
    --not-before 2020-03-25T10:00:00.000
```

------
#### [ PowerShell ]

**重新安排事件**  
使用 [Edit-EC2InstanceEventStartTime](https://docs.aws.amazon.com/powershell/latest/reference/items/Edit-EC2InstanceEventStartTime.html) cmdlet。

```
Edit-EC2InstanceEventStartTime `
    -InstanceId i-1234567890abcdef0 `
    -InstanceEventId instance-event-0d59937288b749b32 `
    -NotBefore 2020-03-25T10:00:00.000
```

------

# 为会影响 Amazon EC2 实例的计划事件创建自定义事件窗口
<a name="event-windows"></a>

您可以为重新启动、停止或终止 Amazon EC2 实例的计划事件定义自定义事件窗口。您可以将一个或多个实例与事件窗口关联。如果计划了这些实例的计划事件，AWS 将在关联事件窗口中调度事件。

通过指定工作负载在非高峰期间发生的事件窗口，可以使用事件窗口最大化工作负载可用性。您还可以将事件窗口与内部维护计划保持一致。

您可以通过指定一组时间范围来定义事件窗口。最短持续时间为 2 小时。总计组合时间范围必须至少为 4 小时。

您可以使用实例 ID 或实例标签将一个或多个实例与事件窗口相关联。您还可以使用主机 ID 将专属主机与事件窗口相关联。

**警告**  
事件窗口仅适用于停止、重新启动或终止实例的计划事件。  
事件窗口不适用于：  
加速计划事件和网络维护事件。
[自动实例恢复](ec2-instance-recover.md)等非计划维护和非计划重启。

**Topics**
+ [注意事项](#event-windows-considerations)
+ [创建事件窗口](#create-event-windows)
+ [将目标与事件窗口关联](#associate-target-event-window)
+ [取消目标与事件窗口的关联](#disassociate-target-event-window)
+ [修改事件窗口](#modify-event-windows)
+ [删除事件窗口](#delete-event-windows)

## 注意事项
<a name="event-windows-considerations"></a>
+ 所有事件窗口均采用 UTC 时间。
+ 一个事件窗口可以包含多个时间范围。虽然每个范围必须至少为 2 小时，但所有范围的总持续时间必须至少为 4 小时。
+ 一个事件窗口只能关联一个目标类型（实例 ID、专属主机 ID 或实例标签）。
+ 一个目标（实例 ID、专属主机 ID 或实例标签）只能关联一个事件窗口。
+ 一个事件窗口最多可以关联 100 个实例 ID 或 50 个专属主机 ID 或 50 个实例标签。实例标签可以关联任意数量的实例。
+ 每个 AWS 区域最多可以创建 200 个事件窗口。
+ 与事件窗口关联的多个实例可能会同时发生计划的事件。
+ 如果 AWS 已计划事件，修改事件窗口不会更改计划事件的时间。如果事件有截止日期，您可以[重新计划事件](reschedule-event.md)。
+ 您可以在计划事件开始之前停止并启动实例。这会将实例迁移至新主机并清除该事件。

## 创建事件窗口
<a name="create-event-windows"></a>

您可以创建一个或多个事件窗口。对于每个事件窗口，您可以指定一个或多个时间块。例如，您可以创建一个带有时间块的事件窗口，发生于每天凌晨 4 点，持续 2 个小时。或者，您可以创建一个带有时间块的事件窗口，发生于星期日凌晨 2 点至凌晨 4 点以及星期三凌晨 3 点至凌晨 5 点。

事件窗口每周重复出现，直至将其删除。

------
#### [ Console ]

**创建事件窗口**

1. 通过以下网址打开 Amazon EC2 控制台：[https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)。

1. 在导航窗格中，选择 **Events**。

1. 选择 **Actions (操作)**，然后选择 **Manage event windows (管理事件窗口)**。

1. 选择 **Create instance event window (创建实例事件窗口)**。

1. 对于 **Event window name (事件窗口名称)**，输入事件窗口的描述性名称。

1. 对于 **Event window schedule (事件窗口时间表)**，选择通过使用 Cron schedule builder（Cron 计划生成器）或指定时间范围来指定事件窗口中的时间块。
   + 如果选择 **Cron schedule builder (Cron 计划生成器)**，请指定以下内容：

     1. 对于 **Days (UTC) (天数(UTC))**，请指定发生事件窗口的具体日期。

     1. 对于 **Start time (UTC) (开始时间(UTC))**，请指定事件窗口开始的时间。

     1. 对于 **Duration (持续时间)**，请指定事件窗口中时间块的持续时间。每个时间块的最短持续时间为 2 小时。事件窗口的最短持续时间总计必须等于或超过 4 小时。所有时间均采用 UTC。
   + 如果选择 **Time ranges (时间范围)**，选择 **Add new time range (添加新时间范围)**，并指定开始以及结束日期和时间。每个时间范围重复此操作。每个时间范围的最短持续时间为 2 小时。所有时间范围总计最小持续时间必须等于或超过 4 小时。

1. （可选）对于**目标详细信息**，将一个或多个实例关联到该事件时段。使用实例 ID 或实例标签来关联实例。使用主机 ID 来关联专属主机。当这些目标列入计划维护时，该事件将在此事件时段内出现。

   请注意，您可以在不将目标与窗口关联的情况下创建事件窗口。稍后，您可以修改窗口以关联一个或多个目标。

1. （可选）对于 **Event window tags (事件窗口标签)**，选择 **Add tag (添加标签)**，然后输入标签键和值。对每个标签重复此操作。

1. 选择 **Create event window (创建事件窗口)**。

------
#### [ AWS CLI ]

**创建有时间范围的事件窗口**  
使用 [create-instance-event-window](https://docs.aws.amazon.com/cli/latest/reference/ec2/create-instance-event-window.html) 命令。

```
aws ec2 create-instance-event-window \
    --time-range StartWeekDay=monday,StartHour=2,EndWeekDay=wednesday,EndHour=8 \
    --tag-specifications "ResourceType=instance-event-window,Tags=[{Key=K1,Value=V1}]" \
    --name myEventWindowName
```

下面是示例输出。

```
{
    "InstanceEventWindow": {
        "InstanceEventWindowId": "iew-0abcdef1234567890",
        "TimeRanges": [
            {
                "StartWeekDay": "monday",
                "StartHour": 2,
                "EndWeekDay": "wednesday",
                "EndHour": 8
            }
        ],
        "Name": "myEventWindowName",
        "State": "creating",
        "Tags": [
            {
                "Key": "K1",
                "Value": "V1"
            }
        ]
    }
}
```

**创建有 Cron 表达式的事件窗口**  
使用 [create-instance-event-window](https://docs.aws.amazon.com/cli/latest/reference/ec2/create-instance-event-window.html) 命令。

```
aws ec2 create-instance-event-window \
    --cron-expression "* 21-23 * * 2,3" \
    --tag-specifications "ResourceType=instance-event-window,Tags=[{Key=K1,Value=V1}]" \
    --name myEventWindowName
```

下面是示例输出。

```
{
    "InstanceEventWindow": {
        "InstanceEventWindowId": "iew-0abcdef1234567890",
        "Name": "myEventWindowName",
        "CronExpression": "* 21-23 * * 2,3",
        "State": "creating",
        "Tags": [
            {
                "Key": "K1",
                "Value": "V1"
            }
        ]
    }
}
```

------
#### [ PowerShell ]

**创建有时间范围的事件窗口**  
使用 [New-EC2InstanceEventWindow](https://docs.aws.amazon.com/powershell/latest/reference/items/New-EC2InstanceEventWindow.html) cmdlet。

```
$timeRange = New-Object Amazon.EC2.Model.InstanceEventWindowTimeRangeRequest
$timeRange.StartWeekDay = "monday"
$timeRange.EndWeekDay = "wednesday"
$timeRange.StartHour = 2
$timeRange.EndHour = 8
$tag = @{Key="key1"; Value="value1"}
$tagspec = New-Object Amazon.EC2.Model.TagSpecification
$tagspec.ResourceType = "instance-event-window"
$tagspec.Tags.Add($tag)
New-EC2InstanceEventWindow `
    -Name my-event-window `
    -TagSpecification $tagspec `
    -TimeRange @($timeRange)
```

下面是示例输出。

```
AssociationTarget     : 
CronExpression        : 
InstanceEventWindowId : iew-0abcdef1234567890
Name                  : my-event-window
State                 : creating
Tags                  : {key1}
TimeRanges            : {Amazon.EC2.Model.InstanceEventWindowTimeRange}
```

**创建有 Cron 表达式的事件窗口**  
使用 [New-EC2InstanceEventWindow](https://docs.aws.amazon.com/powershell/latest/reference/items/New-EC2InstanceEventWindow.html) cmdlet。

```
$tag = @{Key="key1"; Value="value1"}
$tagspec = New-Object Amazon.EC2.Model.TagSpecification
$tagspec.ResourceType = "instance-event-window"
$tagspec.Tags.Add($tag)
New-EC2InstanceEventWindow `
    -Name my-event-window `
    -TagSpecification $tagspec`
    -CronExpression "* 21-23 * * 2,3"
```

下面是示例输出。

```
AssociationTarget     : 
CronExpression        : * 21-23 * * 2,3
InstanceEventWindowId : iew-0abcdef1234567890
Name                  : my-event-window
State                 : creating
Tags                  : {key1}
TimeRanges            : {}
```

------

## 将目标与事件窗口关联
<a name="associate-target-event-window"></a>

创建事件窗口后，您可以将目标与事件窗口相关联。一个事件窗口只能关联一个目标类型。您可以指定实例 ID、专属主机 ID 或实例标签。

------
#### [ Console ]

**将目标与事件窗口相关联**

1. 通过以下网址打开 Amazon EC2 控制台：[https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)。

1. 在导航窗格中，选择 **Events**。

1. 选择要修改的事件窗口。

1. 选择**操作**、**修改实例事件窗口**。

------
#### [ AWS CLI ]

**将实例标签与事件窗口相关联**  
使用 [associate-instance-event-window](https://docs.aws.amazon.com/cli/latest/reference/ec2/associate-instance-event-window.html) 命令。

```
aws ec2 associate-instance-event-window \
    --instance-event-window-id iew-0abcdef1234567890 \
    --association-target "InstanceTags=[{Key=k2,Value=v2},{Key=k1,Value=v1}]"
```

下面是示例输出。

```
{
    "InstanceEventWindow": {
        "InstanceEventWindowId": "iew-0abcdef1234567890",
        "Name": "myEventWindowName",
        "CronExpression": "* 21-23 * * 2,3",
        "AssociationTarget": {
            "InstanceIds": [],
            "Tags": [
                {
                    "Key": "k2",
                    "Value": "v2"
                },
                {
                    "Key": "k1",
                    "Value": "v1"
                }
            ],
            "DedicatedHostIds": []
        },
        "State": "creating"
    }
}
```

**将实例 ID 与事件窗口相关联**  
使用 [associate-instance-event-window](https://docs.aws.amazon.com/cli/latest/reference/ec2/associate-instance-event-window.html) 命令。

```
aws ec2 associate-instance-event-window \
    --instance-event-window-id iew-0abcdef1234567890 \
    --association-target "InstanceIds=i-1234567890abcdef0,i-0598c7d356eba48d7"
```

下面是示例输出。

```
{
    "InstanceEventWindow": {
        "InstanceEventWindowId": "iew-0abcdef1234567890",
        "Name": "myEventWindowName",
        "CronExpression": "* 21-23 * * 2,3",
        "AssociationTarget": {
            "InstanceIds": [
                "i-1234567890abcdef0",
                "i-0598c7d356eba48d7"
            ],
            "Tags": [],
            "DedicatedHostIds": []
        },
        "State": "creating"
    }
}
```

**将专属主机与事件窗口相关联**  
使用 [associate-instance-event-window](https://docs.aws.amazon.com/cli/latest/reference/ec2/associate-instance-event-window.html) 命令。

```
aws ec2 associate-instance-event-window \
    --instance-event-window-id iew-0abcdef1234567890 \
    --association-target "DedicatedHostIds=h-029fa35a02b99801d"
```

下面是示例输出。

```
{
    "InstanceEventWindow": {
        "InstanceEventWindowId": "iew-0abcdef1234567890",
        "Name": "myEventWindowName",
        "CronExpression": "* 21-23 * * 2,3",
        "AssociationTarget": {
            "InstanceIds": [],
            "Tags": [],
            "DedicatedHostIds": [
                "h-029fa35a02b99801d"
            ]
        },
        "State": "creating"
    }
}
```

------
#### [ PowerShell ]

**将实例标签与事件窗口相关联**  
使用 [Register-EC2InstanceEventWindow](https://docs.aws.amazon.com/powershell/latest/reference/items/Register-EC2InstanceEventWindow.html) cmdlet。

```
$tag1 = @{Key="key1"; Value="value1"}
$tag2 = @{Key="key2"; Value="value2"}
Register-EC2InstanceEventWindow `
    -InstanceEventWindowId iew-0abcdef1234567890 `
    -AssociationTarget_InstanceTag @($tag1,$tag2)
```

**将实例 ID 与事件窗口相关联**  
使用 [Register-EC2InstanceEventWindow](https://docs.aws.amazon.com/powershell/latest/reference/items/Register-EC2InstanceEventWindow.html) cmdlet。

```
Register-EC2InstanceEventWindow `
    -InstanceEventWindowId iew-0abcdef1234567890 `
    -AssociationTarget_InstanceId i-1234567890abcdef0, i-0598c7d356eba48d7
```

**将专属主机与事件窗口相关联**  
使用 [Register-EC2InstanceEventWindow](https://docs.aws.amazon.com/powershell/latest/reference/items/Register-EC2InstanceEventWindow.html) cmdlet。

```
Register-EC2InstanceEventWindow `
    -InstanceEventWindowId iew-0abcdef1234567890 `
    -AssociationTarget_DedicatedHostId h-029fa35a02b99801d
```

------

## 取消目标与事件窗口的关联
<a name="disassociate-target-event-window"></a>

------
#### [ Console ]

**取消目标与事件窗口的关联**

1. 通过以下网址打开 Amazon EC2 控制台：[https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)。

1. 在导航窗格中，选择 **Events**。

1. 选择要修改的事件窗口。

1. 选择**操作**、**修改实例事件窗口**。

------
#### [ AWS CLI ]

**取消实例标签与事件窗口的关联**  
使用[disassociate-instance-event-window](https://docs.aws.amazon.com/cli/latest/reference//ec2/disassociate-instance-event-window.html) 命令。

```
aws ec2 disassociate-instance-event-window \
    --instance-event-window-id iew-0abcdef1234567890 \
    --association-target "InstanceTags=[{Key=k2,Value=v2},{Key=k1,Value=v1}]"
```

下面是示例输出。

```
{
    "InstanceEventWindow": {
        "InstanceEventWindowId": "iew-0abcdef1234567890",
        "Name": "myEventWindowName",
        "CronExpression": "* 21-23 * * 2,3",
        "AssociationTarget": {
            "InstanceIds": [],
            "Tags": [],
            "DedicatedHostIds": []
        },
        "State": "creating"
    }
}
```

**取消实例 ID 与事件窗口的关联**  
使用[disassociate-instance-event-window](https://docs.aws.amazon.com/cli/latest/reference/ec2/disassociate-instance-event-window.html) 命令。

```
aws ec2 disassociate-instance-event-window \
    --instance-event-window-id iew-0abcdef1234567890 \
    --association-target "InstanceIds=i-1234567890abcdef0,i-0598c7d356eba48d7"
```

下面是示例输出。

```
{
    "InstanceEventWindow": {
        "InstanceEventWindowId": "iew-0abcdef1234567890",
        "Name": "myEventWindowName",
        "CronExpression": "* 21-23 * * 2,3",
        "AssociationTarget": {
            "InstanceIds": [],
            "Tags": [],
            "DedicatedHostIds": []
        },
        "State": "creating"
    }
}
```

**取消专属主机与事件窗口的关联**  
使用[disassociate-instance-event-window](https://docs.aws.amazon.com/cli/latest/reference/ec2/disassociate-instance-event-window.html) 命令。

```
aws ec2 disassociate-instance-event-window \
    --instance-event-window-id iew-0abcdef1234567890 \
    --association-target DedicatedHostIds=h-029fa35a02b99801d
```

下面是示例输出。

```
{
    "InstanceEventWindow": {
        "InstanceEventWindowId": "iew-0abcdef1234567890",
        "Name": "myEventWindowName",
        "CronExpression": "* 21-23 * * 2,3",
        "AssociationTarget": {
            "InstanceIds": [],
            "Tags": [],
            "DedicatedHostIds": []
        },
        "State": "creating"
    }
}
```

------
#### [ PowerShell ]

**取消实例标签与事件窗口的关联**  
使用 [Unregister-EC2InstanceEventWindow](https://docs.aws.amazon.com/powershell/latest/reference/items/Unregister-EC2InstanceEventWindow.html) cmdlet。

```
$tag1 = @{Key="key1"; Value="value1"}
$tag2 = @{Key="key2"; Value="value2"}
Unregister-EC2InstanceEventWindow `
    -InstanceEventWindowId iew-0abcdef1234567890 `
    -AssociationTarget_InstanceTag @($tag1, $tag2)
```

**取消实例 ID 与事件窗口的关联**  
使用 [Unregister-EC2InstanceEventWindow](https://docs.aws.amazon.com/powershell/latest/reference/items/Unregister-EC2InstanceEventWindow.html) cmdlet。

```
Unregister-EC2InstanceEventWindow `
    -InstanceEventWindowId iew-0abcdef1234567890 `
    -AssociationTarget_InstanceId i-1234567890abcdef0, i-0598c7d356eba48d7
```

**取消专属主机与事件窗口的关联**  
使用 [Unregister-EC2InstanceEventWindow](https://docs.aws.amazon.com/powershell/latest/reference/items/Unregister-EC2InstanceEventWindow.html) cmdlet。

```
Unregister-EC2InstanceEventWindow `
    -InstanceEventWindowId iew-0abcdef1234567890 `
    -AssociationTarget_DedicatedHostId h-029fa35a02b99801d
```

------

## 修改事件窗口
<a name="modify-event-windows"></a>

您可以修改事件窗口中除 ID 之外的所有字段。例如，夏令时开始时，您可能需要修改事件窗口计划。对于现有事件窗口，您可能需要添加或移除目标。

您可以在修改事件窗口时修改时间范围或 Cron 表达式，但不能同时修改两者。

------
#### [ Console ]

**修改事件窗口**

1. 通过以下网址打开 Amazon EC2 控制台：[https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)。

1. 在导航窗格中，选择 **Events**。

1. 选择 **Actions (操作)**，然后选择 **Manage event windows (管理事件窗口)**。

1. 选择要修改的事件窗口，然后选择 **Actions (操作)**、**Modify instance event window (修改实例事件窗口)**。

1. 在事件窗口中修改字段，然后选择 **Modify event window (修改事件窗口)**。

------
#### [ AWS CLI ]

**修改事件窗口的时间范围**  
使用 [modify-instance-event-window](https://docs.aws.amazon.com/cli/latest/reference/ec2/modify-instance-event-window.html) 命令。

```
aws ec2 modify-instance-event-window 
    --instance-event-window-id iew-0abcdef1234567890 \
    --time-range StartWeekDay=monday,StartHour=2,EndWeekDay=wednesday,EndHour=8
```

下面是示例输出。

```
{
    "InstanceEventWindow": {
        "InstanceEventWindowId": "iew-0abcdef1234567890",
        "TimeRanges": [
            {
                "StartWeekDay": "monday",
                "StartHour": 2,
                "EndWeekDay": "wednesday",
                "EndHour": 8
            }
        ],
        "Name": "myEventWindowName",
        "AssociationTarget": {
            "InstanceIds": [
                "i-0abcdef1234567890",
                "i-0be35f9acb8ba01f0"
            ],
            "Tags": [],
            "DedicatedHostIds": []
        },
        "State": "creating",
        "Tags": [
            {
                "Key": "K1",
                "Value": "V1"
            }
        ]
    }
}
```

**修改事件窗口的一组时间范围**  
使用 [modify-instance-event-window](https://docs.aws.amazon.com/cli/latest/reference/ec2/modify-instance-event-window.html) 命令。

```
aws ec2 modify-instance-event-window 
    --instance-event-window-id iew-0abcdef1234567890 \
    --time-range '[{"StartWeekDay": "monday", "StartHour": 2, "EndWeekDay": "wednesday", "EndHour": 8},
	  {"StartWeekDay": "thursday", "StartHour": 2, "EndWeekDay": "friday", "EndHour": 8}]'
```

下面是示例输出。

```
{
    "InstanceEventWindow": {
        "InstanceEventWindowId": "iew-0abcdef1234567890",
        "TimeRanges": [
            {
                "StartWeekDay": "monday",
                "StartHour": 2,
                "EndWeekDay": "wednesday",
                "EndHour": 8
            },
            {
                "StartWeekDay": "thursday",
                "StartHour": 2,
                "EndWeekDay": "friday",
                "EndHour": 8
            }
        ],
        "Name": "myEventWindowName",
        "AssociationTarget": {
            "InstanceIds": [
                "i-0abcdef1234567890",
                "i-0be35f9acb8ba01f0"
            ],
            "Tags": [],
            "DedicatedHostIds": []
        },
        "State": "creating",
        "Tags": [
            {
                "Key": "K1",
                "Value": "V1"
            }
        ]
    }
}
```

**修改事件窗口的 Cron 表达式**  
使用 [modify-instance-event-window](https://docs.aws.amazon.com/cli/latest/reference/ec2/modify-instance-event-window.html) 命令。

```
aws ec2 modify-instance-event-window 
    --instance-event-window-id iew-0abcdef1234567890 \
    --cron-expression "* 21-23 * * 2,3"
```

下面是示例输出。

```
{
    "InstanceEventWindow": {
        "InstanceEventWindowId": "iew-0abcdef1234567890",
        "Name": "myEventWindowName",
        "CronExpression": "* 21-23 * * 2,3",
        "AssociationTarget": {
            "InstanceIds": [
                "i-0abcdef1234567890",
                "i-0be35f9acb8ba01f0"
            ],
            "Tags": [],
            "DedicatedHostIds": []
        },
        "State": "creating",
        "Tags": [
            {
                "Key": "K1",
                "Value": "V1"
            }
        ]
    }
}
```

------
#### [ PowerShell ]

**修改事件窗口的时间范围**  
使用 [Edit-EC2InstanceEventWindow](https://docs.aws.amazon.com/powershell/latest/reference/items/Edit-EC2InstanceEventWindow.html) cmdlet。

```
$timeRange1 = New-Object Amazon.EC2.Model.InstanceEventWindowTimeRangeRequest
$timeRange1.StartWeekDay = "monday"
$timeRange1.EndWeekDay = "wednesday"
$timeRange1.StartHour = 2
$timeRange1.EndHour = 8
$timeRange2 = New-Object Amazon.EC2.Model.InstanceEventWindowTimeRangeRequest
$timeRange2.StartWeekDay = "thursday"
$timeRange2.EndWeekDay = "friday"
$timeRange2.StartHour = 1
$timeRange2.EndHour = 6
Edit-EC2InstanceEventWindow `
    -InstanceEventWindowId iew-0abcdef1234567890 `
    -TimeRange @($timeRange1, $timeRange2)
```

**修改事件窗口的 Cron 表达式**  
使用 [Edit-EC2InstanceEventWindow](https://docs.aws.amazon.com/powershell/latest/reference/items/Edit-EC2InstanceEventWindow.html) cmdlet。

```
Edit-EC2InstanceEventWindow `
    -InstanceEventWindowId iew-0abcdef1234567890 `
    -CronExpression "* 21-23 * * 2,3"
```

------

## 删除事件窗口
<a name="delete-event-windows"></a>

您可以一次删除一个事件窗口。

------
#### [ Console ]

**删除事件窗口**

1. 通过以下网址打开 Amazon EC2 控制台：[https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)。

1. 在导航窗格中，选择 **Events**。

1. 选择 **Actions (操作)**，然后选择 **Manage event windows (管理事件窗口)**。

1. 选择要删除的事件窗口，然后选择 **Actions (操作)**、**Delete instance event window (删除实例事件窗口)**。

1. 当系统提示时，输入 **delete**，然后选择**删除**。

------
#### [ AWS CLI ]

**删除事件窗口**  
使用 [delete-instance-event-window](https://docs.aws.amazon.com/cli/latest/reference/ec2/delete-instance-event-window.html) 命令并指定要删除的事件窗口。

```
aws ec2 delete-instance-event-window \
    --instance-event-window-id iew-0abcdef1234567890
```

**强制删除事件时段**  
使用 `--force-delete` 参数（如果事件窗口当前与目标相关联）。

```
aws ec2 delete-instance-event-window \
    --instance-event-window-id iew-0abcdef1234567890 \
    --force-delete
```

------
#### [ PowerShell ]

**删除事件窗口**  
使用 [Remove-EC2InstanceEventWindow](https://docs.aws.amazon.com/powershell/latest/reference/items/Remove-EC2InstanceEventWindow.html) cmdlet。

```
Remove-EC2InstanceEventWindow `
    -InstanceEventWindowId iew-0abcdef1234567890
```

**强制删除事件时段**  
使用 [Remove-EC2InstanceEventWindow](https://docs.aws.amazon.com/powershell/latest/reference/items/Remove-EC2InstanceEventWindow.html) cmdlet。

```
Remove-EC2InstanceEventWindow `
    -InstanceEventWindowId iew-0abcdef1234567890 `
    -ForceDelete $true
```

------