

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

# 监控 IAM 根用户活动
<a name="monitor-iam-root-user-activity"></a>

*JJ Sung 和 Mostefa Brougui，Amazon Web Services*

## Summary
<a name="monitor-iam-root-user-activity-summary"></a>

每个 Amazon Web Services (AWS) 账户都有一个根用户。作为 AWS Identity and Access Management (IAM) 的[安全最佳实践](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)，我们建议您使用根用户来完成只有根用户才能执行的任务。有关完整列表，请参阅*《Amazon Web Services account 管理参考指南》*中的[需要根用户凭证的任务](https://docs.aws.amazon.com/accounts/latest/reference/root-user-tasks.html)。由于根用户对您的所有 AWS 资源和账单信息具有完全访问权限，因此我们建议您不要使用此账户并监控其是否有任何活动，这可能表明根用户凭证已泄露。

使用此模式，您可以设置一个[事件驱动的架构](https://aws.amazon.com/event-driven-architecture/)来监控 IAM 根用户。这种模式建立了一个 hub-and-spoke解决方案，该解决方案可以监控多个 AWS 账户、分*支*账户，并将管理和报告集中到一个账户（*中心*账户）中。

使用 IAM 根用户证书时，Amazon CloudWatch 和 AWS 会分别在日志和跟踪中 CloudTrail 记录活动。在分支账户中，Amazon EventBridge 规则将事件发送到中心账户中的中央[事件总线](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-bus.html)。在中心账户中， EventBridge 规则将事件发送到 AWS Lambda 函数。该函数使用 Amazon Simple Notiﬁcation Service (Amazon SNS) 主题来通知您根用户活动。

在此模式中，您可以使用 AWS CloudFormation 模板在分支账户中部署监控和事件处理服务。您可以使用 T HashiCorp erraform 模板在中心账户中部署事件管理和通知服务。

## 先决条件和限制
<a name="monitor-iam-root-user-activity-prereqs"></a>

**先决条件**

1. 在您的 AWS 环境中部署 AWS 资源的权限。

1. 部署 CloudFormation 堆栈集的权限。有关更多信息，请参阅[堆栈集操作的先决条件](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacksets-prereqs.html)（CloudFormation 文档）。

1. Terraform 已安装并可供使用。有关更多信息，请参阅[入门 – AWS](https://learn.hashicorp.com/collections/terraform/aws-get-started)（Terraform 文档）。

1. 每个分支账户中的现有跟踪。有关更多信息，请参阅 [AWS 入门 CloudTrail](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-tutorial.html)（CloudTrail 文档）。

1. 该跟踪配置为将事件发送到 CloudWatch 日志。有关更多信息，请参阅[向 CloudWatch 日志发送事件](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/send-cloudtrail-events-to-cloudwatch-logs.html)（CloudTrail 文档）。

1. 您的中心和分支账户必须由 AWS Organizations 管理。

## 架构
<a name="monitor-iam-root-user-activity-architecture"></a>

下图说明了实现的构建基块。

![分支账户中的事件，在中心账户中创建电子邮件通知](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/175f356b-f9df-4d33-82fc-fe33b2c88b05/images/6147e5b5-616e-49a4-b330-dbb7e3381fe7.png)


1. 使用 IAM 根用户证书时， CloudWatch 分别在日志和跟踪中 CloudTrail 记录活动。

1. 在分支账户中， EventBridge 规则将事件发送到中心账户中的中央[事件总线](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-bus.html)。

1. 在中心账户中， EventBridge 规则将事件发送到 Lambda 函数。

1. Lambda 函数使用一个 Amazon SNS 主题来通知您根用户活动。

## 工具
<a name="monitor-iam-root-user-activity-tools"></a>

**AWS 服务**
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 可帮助您设置 AWS 资源，快速一致地配置这些资源，并在 AWS 账户和区域的整个生命周期中对其进行管理。
+ [AWS CloudTrail](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-user-guide.html) 可帮助您审计 AWS 账户的治理、合规和运营风险。
+ [Amazon CloudWatch Lo](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/WhatIsCloudWatchLogs.html) gs 可帮助您集中管理来自所有系统、应用程序和 AWS 服务的日志，以便您可以监控它们并安全地将其存档。
+ [Amazon EventBridge](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html) 是一项无服务器事件总线服务，可帮助您将应用程序与来自各种来源的实时数据连接起来。例如，AWS Lambda 函数、使用 API 目标的 HTTP 调用端点或其他 Amazon Web Services account 中的事件总线。
+ [AWS Identity and Access Management (AWS IAM) ](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html)通过控制验证和授权使用您 AWS 资源的用户，帮助您安全地管理对您 AWS 资源的访问。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一项计算服务，可帮助您运行代码，而无需预置或管理服务器。它仅在需要时运行您的代码，并且能自动扩缩，因此您只需为使用的计算时间付费。
+ [Amazon Simple Notiﬁcation Service (Amazon SNS)](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) 可帮助您协调和管理发布者与客户端（包括 Web 服务器和电子邮件地址）之间的消息交换。

**其他工具和服务**
+ [Terraform](https://www.terraform.io/docs) 是一个 CLI 应用程序，用于使用配置文件形式的代码来配置和管理云基础架构和资源。

**代码存储库**

此模式的源代码和模板可在[GitHub 存储库](https://github.com/aws-samples/aws-iam-root-user-activity-monitor)中找到。此模式提供两个模板：
+ 一个 Terraform 模板，其中包含您在中心账户中部署的资源
+ 在分支账户中作为堆栈集实例部署的 CloudFormation 模板

该库的总体结构如下。

```
.
 |__README.md
 |__spoke-stackset.yaml
 |__hub.tf
 |__root-activity-monitor-module
     |__main.tf  # contains Terraform code to deploy resources in the Hub account
     |__iam      # contains IAM policies JSON files
         |__ lambda-assume-policy.json          # contains trust policy of the IAM role used by the Lambda function
         |__ lambda-policy.json                 # contains the IAM policy attached to the IAM role used by the Lambda function
     |__outputs  # contains Lambda function zip code
```

*Epics* 部分提供了部署模板的 step-by-step说明。

## 操作说明
<a name="monitor-iam-root-user-activity-epics"></a>

### 将资源部署到中心账户
<a name="deploy-resources-to-the-hub-account"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 克隆示例代码存储库。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/monitor-iam-root-user-activity.html) | 常规 AWS | 
| 更新 Terraform 模板。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/monitor-iam-root-user-activity.html) | 常规 AWS | 
| 将资源部署到 AWS 中心账户。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/monitor-iam-root-user-activity.html) | 常规 AWS | 

### 将资源部署到分支账户
<a name="deploy-resources-to-your-spoke-accounts"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 部署 CloudFormation 模板。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/monitor-iam-root-user-activity.html)有关更多信息和说明，请参阅[创建堆栈集](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacksets-getting-started-create.html)（CloudFormation 文档）。 | 常规 AWS | 

### （可选）测试通知
<a name="optional-test-the-notifications"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 使用根用户凭证。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/monitor-iam-root-user-activity.html) | 常规 AWS | 

## 相关资源
<a name="monitor-iam-root-user-activity-resources"></a>
+ [安全最佳实践](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)（IAM 文档）
+ [使用 StackSets](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/what-is-cfnstacksets.html)（CloudFormation 文档）
+ [入门](https://learn.hashicorp.com/collections/terraform/aws-get-started)（Terraform 文档）

## 附加信息
<a name="monitor-iam-root-user-activity-additional"></a>

[Amazon GuardDuty](https://docs.aws.amazon.com/guardduty/latest/ug/what-is-guardduty.html) 是一项持续的安全监控服务，可分析和处理日志，以识别您的 AWS 环境中意外和可能未经授权的活动。作为此解决方案的替代方案，如果您已启用 GuardDuty，它可以在使用根用户凭据时提醒您。 GuardDuty 结果为`Policy:IAMUser/RootCredentialUsage`，默认严重性为 “**低**”。有关更多信息，请参阅[管理 Amazon GuardDuty 调查结果](https://docs.aws.amazon.com/guardduty/latest/ug/findings_management.html)。