

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在 AWS Organizations 中设置程序账户关闭警报
<a name="set-up-alerts-for-programmatic-account-closures-in-aws-organizations"></a>

*Richard Milner-Watts、Debojit Bhadra 和 Manav Yadav，Amazon Web Services*

## Summary
<a name="set-up-alerts-for-programmatic-account-closures-in-aws-organizations-summary"></a>

[AWS](https://aws.amazon.com/organizations/) Organizations 的 [CloseAccount API](https://docs.aws.amazon.com/organizations/latest/APIReference/API_CloseAccount.html) 使您能够以编程方式关闭组织内的成员账户，而不必使用根证书登录账户。[RemoveAccountFromOrganization API](https://docs.aws.amazon.com/organizations/latest/APIReference/API_RemoveAccountFromOrganization.html) 从 AWS Organizations 中的组织中提取一个账户，使其成为独立账户。

这 APIs 可能会增加可以关闭或移除 AWS 账户的操作员数量。所有通过 AWS Organizations 管理账户中的 AWS Identity and Access Management (IAM) 访问该组织的用户都可以拨打这些 APIs电话，因此访问权限不限于账户根电子邮件的所有者以及任何关联的多因素身份验证 (MFA) 设备。

此模式会在调用`CloseAccount`和`RemoveAccountFromOrganization` APIs 时发出警报，因此您可以监控这些活动。对于警报，它使用 [Amazon Simple Notiﬁcation Service (Amazon SNS)](https://aws.amazon.com/sns/) 主题。您还可通过 [webhook](https://api.slack.com/messaging/webhooks) 设置 Slack 通知。

## 先决条件和限制
<a name="set-up-alerts-for-programmatic-account-closures-in-aws-organizations-prereqs"></a>

**先决条件**
+ 一个有效的 Amazon Web Services account
+ AWS Organizations 中的组织
+ 访问组织根目录下的组织管理账户，以创建所需资源

**限制**
+ 如 [AWS Organizations API参考](https://docs.aws.amazon.com/organizations/latest/APIReference/API_CloseAccount.html)中所述，该 `CloseAccount`API 仅允许在连续30天内关闭10％的活跃成员账户。
+ 关闭 Amazon Web Services account 后，其状态将更改为已暂停。在此状态转换后的 90 天内，Amazon Web Services Support 可重新开放账户。90 天后，该账户将被永久删除。
+ 有权访问 AWS Organizations 管理账户并且 APIs 可能还有禁用这些提醒的权限的用户。如果主要关注的是恶意行为而不是意外删除，请考虑使用 [IAM 权限边界](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_boundaries.html) 保护由此模式创建的资源。
+ `CloseAccount ` 和 `RemoveAccountFromOrganization` 的 API 调用在美国东部（弗吉尼亚州北部）(`us-east-1`) 处理。因此，您必须在 `us-east-1` 中部署此解决方案才能观察事件。

## 架构
<a name="set-up-alerts-for-programmatic-account-closures-in-aws-organizations-architecture"></a>

**目标技术堆栈 **
+ AWS Organizations
+ AWS CloudTrail
+ Amazon EventBridge
+ AWS Lambda
+ Amazon SNS

**目标架构 **

下图显示此模式的解决方案架构。

 

![在 AWS Organizations 中设置账户关闭警报的架构](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/ba9d9db1-fab8-4e3b-a1bb-f0be91ade5c6/images/92caee55-2722-4ba2-bdd2-66f1af35dce5.png)


1. AWS Organizations 处理 `CloseAccount` 或 `RemoveAccountFromOrganization` 请求

1.  EventBridge Amazon 与 AWS 集成，可将这些事件传送 CloudTrail 到默认事件总线。

1. 自定义亚马逊 EventBridge 规则与 AWS Organizations 的请求相匹配，并调用 AWS Lambda 函数。

1. Lambda 函数向 SNS 主题发送消息，用户可以订阅该消息以接收电子邮件警报或进一步处理。

1. 如果启用 Slack 通知，Lambda 函数会向 Slack 网络挂钩发送一条消息。

## 工具
<a name="set-up-alerts-for-programmatic-account-closures-in-aws-organizations-tools"></a>

**AWS 服务**
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 提供了一种方法，通过将基础设施视为代码，对一组相关的 AWS 和第三方资源进行建模，快速一致地配置这些资源，并在它们的整个生命周期中对其进行管理。
+ [Amazon EventBridge](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html) 是一项无服务器事件总线服务，可用于将应用程序与来自各种来源的数据连接起来。 EventBridge 接收事件（环境变化的指示器），并应用规则将事件路由到目标。规则根据事件的结构（称为*事件模式*）或计划将事件与目标匹配。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一项计算服务，支持无需预置或管理服务器即可运行代码。只有在需要时 Lambda 才运行您的代码，并且能自动扩缩，从每天几个请求扩展到每秒数千个请求。您只需按使用的计算时间付费。代码不运行时不会产生任何费用。
+ [AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_introduction.html) 可帮助您在增长和扩缩您的 AWS 资源时集中管理和治理您的环境。使用 AWS Organizations，您可通过编程方式创建新的 Amazon Web Services account 并分配资源，对账户进行分组以组织您的工作流，将策略应用于账户或群组进行管理，并通过对所有账户使用单一付款方式来简化账单。
+ [AWS CloudTrail](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-user-guide.html) 监控和记录您的 AWS 基础设施中的账户活动，并允许您控制存储、分析和补救措施。
+ [亚马逊简单通知服务 (Amazon SNS) Simple Not](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) ification Service 是一项完全托管的消息服务，用于 (A2A) application-to-application 和 (A2P) application-to-person 通信。

**其他工具**
+ [适用于 Python 的 AWS Lambda Powertools 库](https://docs.powertools.aws.dev/lambda/python/latest/) 是一组实用程序，可为 Lambda 函数提供跟踪、日志、指标和事件处理功能。

**代码**

此模式的代码位于 GitHub [AWS 账户关闭通知器](https://github.com/aws-samples/aws-account-closure-notifier)存储库中。

该解决方案包括一个 CloudFormation 模板，用于部署此模式的架构。它使用 [AWS Lambda Powertools for Python 库](https://docs.powertools.aws.dev/lambda/python/latest/)来提供日志记录和跟踪。

## 操作说明
<a name="set-up-alerts-for-programmatic-account-closures-in-aws-organizations-epics"></a>

### 部署架构
<a name="deploy-the-architecture"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 启动解决方案堆栈的 CloudFormation 模板。 | 此模式的 CloudFormation 模板位于[GitHub 存储库的主分支中](https://github.com/aws-samples/aws-account-closure-notifier)。它部署 IAM 角色、 EventBridge 规则、Lambda 函数和 SNS 主题。<br />要启动模板：1. 克隆[GitHub 存储库](https://github.com/aws-samples/aws-account-closure-notifier)以获取解决方案代码的副本。<br />2. 打开 AWS Organizations 管理账户 AWS 管理控制台。<br />3. 选择美国东部（弗吉尼亚北部`us-east-1`）区域 ()，然后打开[CloudFormation 控制台](https://console.aws.amazon.com/cloudformation)。<br />4. 使用 `account-closure-notifier.yml` 模板并指定以下值来创建堆栈： 堆栈名称：`aws-account-closure-notifier-stack` `ResourcePrefix` 参数：`aws-account-closure-notifier``SlackNotification` 参数：如果需要 Slack 通知，请将设置更改为 `true`。`SlackWebhookEndpoint` 参数：如果需要 Slack 通知，请指定 webhook URL。<br />有关启动 CloudFormation 堆栈的更多信息，请参阅 [AWS 文档](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-create-stack.html)。 | AWS 管理员 | 
| 确认已成功启动解决方案。 | 1. 等待 CloudFormation 堆栈达到 CREATE\_COMPLETE 状态。<br />2. 在中打开[EventBridge 控制台](https://console.aws.amazon.com/events/)`us-east-1`。<br />3. 确认已使用 `aws-account-closure-notifier-event-rule` 名称创建了新规则。 | AWS 管理员 | 
| 订阅 SNS 主题。 | （可选）如果您想订阅 SNS 主题：1. 在 `us-east-1` 中打开 [Amazon SNS 控制台](https://console.aws.amazon.com/sns/v3/home)，找到名为 `aws-account-closure-notifier-sns-topic` 的主题。<br />2. 选择主题名称，然后选择**创建订阅**。<br />3. 对于**协议**，选择**电子邮件**。<br />4. 对于**端点**，指定应接收通知的电子邮件地址，然后选择**创建订阅**。<br />5. 查看您的电子邮件收件箱中是否包含来自 AWS 通知的消息。使用电子邮件中的链接确认订阅。<br />有关设置 SNS 通知的更多信息，请参阅 [Amazon SNS 文档](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/US_SetupSNS.html)。 | AWS 管理员 | 

### 验证解决方案
<a name="verify-the-solution"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 向设置事件总线发送测试事件。 | [GitHub 存储库](https://github.com/aws-samples/aws-account-closure-notifier)提供了一个示例事件，您可以将其发送到 EventBridge 默认事件总线进行测试。该 EventBridge 规则还会对使用自定义事件源`account.closure.notifier`的事件做出反应。您不能使用 CloudTrail 事件源发送此事件，因为无法将事件作为 AWS 服务发送。<br />要发送测试事件：1. 在中打开[EventBridge 控制台](https://console.aws.amazon.com/events/)`us-east-1`。<br />2. 在导航窗格的**总线**，选择**事件总线**，然后选择默认的事件总线。<br />3. 选择**发送事件**。<br />4. 在**事件源**中，输入 `account.closure.notifier`。<br />5. 在**详细信息类型**中，输入 `AWS API Call via CloudTrail`。<br />6. 要**了解事件详情**，请将 GitHub 存储库中的内容复制并粘贴到文本框中。`tests/dummy-event.json`<br />7. 选择**发送**以启动通知工作流。 | AWS 管理员 | 
| 确认已收到了电子邮件通知。 | 查看订阅 SNS 主题的邮箱，以获取通知。您应该会收到一封电子邮件，其中包含已关闭账户和执行 API 调用的主体的详细信息。 | AWS 管理员 | 
| 验证是否已收到了 Slack 通知。 | （可选）如果您在部署 CloudFormation 模板时为`SlackWebhookEndpoint`参数指定了 Webhook 网址，请检查映射到 webhook 的 Slack 频道。它应显示一条消息，其中包含已关闭账户和执行 API 调用的主体的详细信息。 | AWS 管理员 | 

## 相关资源
<a name="set-up-alerts-for-programmatic-account-closures-in-aws-organizations-resources"></a>
+ [CloseAccount 操作](https://docs.aws.amazon.com/organizations/latest/APIReference/API_CloseAccount.html)（AWS Organiations API 参考）
+ [RemoveAccountFromOrganization 操作](https://docs.aws.amazon.com/organizations/latest/APIReference/API_RemoveAccountFromOrganization.html)（AWS Organiations API 参考）
+ [AWS Lambda Powertools for Python](https://docs.powertools.aws.dev/lambda/python/latest/)