

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

# 使用 Terraform 设置企业级规模的集中式日志记录
<a name="set-up-centralized-logging-at-enterprise-scale-by-using-terraform"></a>

*Aarti Rajput、Yashwant Patel 和 Nishtha Yadav，Amazon Web Services*

## Summary
<a name="set-up-centralized-logging-at-enterprise-scale-by-using-terraform-summary"></a>

集中式日志记录对于组织的云基础设施至关重要，因为它能让企业清晰掌握自身的运营状况、安全态势及合规情况。随着您的组织跨多个账户扩展其 AWS 环境，结构化的日志管理策略成为运行安全操作、满足审计要求和实现卓越运营的基础。

这种模式提供了一个可扩展的安全框架，用于集中来自多个 AWS 账户 和服务的日志，从而在复杂 AWS 的部署中实现企业级日志管理。该解决方案使用Terraform实现自动化，Terraform是一种基础设施即代码（IaC）工具，可 HashiCorp 确保一致和可重复的部署，并最大限度地减少手动配置。通过结合亚马逊 CloudWatch 日志、Amazon Data Firehose 和亚马逊简单存储服务 (Amazon S3) Service，您可以实施一个强大的日志聚合和分析管道，该管道可提供：
+ 在整个组织中集中管理日志 AWS Organizations
+ 自动日志收集，内置安全控件
+ 可扩展的日志处理和持久存储功能
+ 简化的合规报告和审计跟踪记录
+ 实时运营洞察和监控

该解决方案通过日志收集来自亚马逊 Elastic Kubernetes Service（Amazon EKS） AWS Lambda 容器、函数和亚马逊关系数据库服务（Amazon RDS）数据库实例的日志。 CloudWatch 它使用 CloudWatch 订阅过滤器自动将这些日志转发到专用的日志帐户。Firehose 管理高吞吐量的日志流传输管道，将日志数据传输至 Amazon S3 进行长期存储。将 Amazon Simple Queue Service（Amazon SQS）配置为在创建对象后接收 Amazon S3 事件通知。这样可实现与分析服务的集成，包括：
+ 用于日志搜索、可视化和实时分析的 Amazon OpenSearch 服务
+ Amazon Athena，用于基于 SQL 的查询操作
+ Amazon EMR，用于大规模数据处理
+ Lambda，用于自定义转换
+ 适用于仪表板的 Amazon Quick

所有数据均使用 AWS Key Management Service (AWS KMS) 进行加密，整个基础架构通过使用 Terraform 进行部署，以实现跨环境的一致配置。

这种集中式日志记录方法使组织能够改善其安全状况，保持合规性要求，并优化其 AWS 基础架构的运营效率。

## 先决条件和限制
<a name="set-up-centralized-logging-at-enterprise-scale-by-using-terraform-prereqs"></a>

**先决条件**
+ 通过使用 [AWS Control Tower](https://docs.aws.amazon.com/controltower/latest/userguide/getting-started-with-control-tower.html) 为组织构建的登录区
+ 已部署并配置了所需账户的 [Account Factory for Terraform（AFT）](https://docs.aws.amazon.com/controltower/latest/userguide/aft-getting-started.html)
+ 用于预调配基础设施的 [Terraform](https://developer.hashicorp.com/terraform/downloads)
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/getting-started.html) 跨账户访问的角色和策略

有关设置 AWS Control Tower、AFT 和应用程序账户的说明，请参阅 [Epics 部分](#set-up-centralized-logging-at-enterprise-scale-by-using-terraform-epics)。

**所需账户**

您的组织中 AWS Organizations 应包括以下账户：
+ **应用程序账户** — AWS 服务 （Amazon EKS、Lambda 和 Amazon RDS）运行和生成日志的一个或多个源账户
+ **日志存档账户** - 用于集中存储和管理日志的专用账户

**产品版本**
+ [AWS Control Tower 版本 3.1](https://docs.aws.amazon.com/controltower/latest/userguide/2023-all.html#lz-3-1) 或更高版本
+ [Terraform 版本 0.15.0](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli) 或更高版本

## 架构
<a name="set-up-centralized-logging-at-enterprise-scale-by-using-terraform-architecture"></a>

下图说明了一种 AWS 集中式日志架构，该架构提供了一种可扩展的解决方案，用于收集、处理来自多个应用程序帐户的日志，并将其存储到一个专用的日志存档帐户中。该架构可以有效地处理来自 AWS 服务 Amazon RDS、Amazon EKS 和 Lambda 的日志，并通过简化的流程将这些日志路由到日志存档账户中的区域 S3 存储桶。

![\[AWS 集中式日志记录架构，用于从多个应用程序账户收集日志。\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/9fc71a10-65d6-437b-9128-cc27bda11af4/images/2e916040-0f11-4712-a8dd-31c95194ce5d.png)


该工作流包括五个流程：

1. **日志流流程**
   + 日志流过程从应用程序账户开始，在那里 AWS 服务 生成各种类型的日志，例如来自 Amazon RDS 的常规日志、错误日志、审计日志、慢速查询日志、来自 Amazon EKS 的控制平面日志以及来自 Lambda 的函数执行和错误日志。
   + CloudWatch 用作初始收集点。它在每个应用程序账户中按日志组级别收集这些日志。
   + 在中 CloudWatch，[订阅筛选器](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/Subscriptions.html)决定应将哪些日志转发到中央账户。这些筛选条件支持对日志转发进行精细控制，因此您可指定确切的日志模式或完整的日志流，以实现日志的集中管理。

1. **跨账户日志传输**
   + 日志将移至日志存档帐户。 CloudWatch 订阅过滤器便于跨账户转账并保留区域背景。
   + 该架构建立了多个并行流，以高效地处理不同的日志源，从而确保实现最佳性能和可扩展性。

1. **日志存档账户中的日志处理**
   + 在日志存档账户中，Firehose 处理传入的日志流。
   + 每个区域均维护专用 Firehose 传输流，可以根据需要转换或丰富日志。
   + 这些 Firehose 传输流会将处理后的日志传输至日志存档账户中的 S3 存储桶，该账户与源应用程序账户位于同一区域（图中的区域 A），以满足数据主权要求。

1. **通知和其他工作流**
   + 当日志到达目标 S3 存储桶后，该架构会通过 Amazon SQS 实现通知系统。
   + 区域 SQS 队列支持异步处理，并且可以根据存储的日志触发其他工作流、分析或提醒系统。

1. **AWS KMS 为了安全**

    AWS KMS 为了安全起见，该架构包含在内。 AWS KMS 为 S3 存储桶提供加密密钥。这样可以确保所有存储的日志均实现静态加密，同时按区域进行加密以满足数据驻留要求。

## 工具
<a name="set-up-centralized-logging-at-enterprise-scale-by-using-terraform-tools"></a>

**AWS 服务**
+ [Amazon CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html) 是一项监控和可观察性服务，它以日志、指标和事件的形式收集监控和操作数据。它提供了在 AWS 和本地服务器上运行的 AWS 资源、应用程序和服务的统一视图。
+ [CloudWatch 日志订阅过滤器](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/SubscriptionFilters.html)是匹配传入日志事件中的模式并将匹配的日志事件传送到指定 AWS 资源以供进一步处理或分析的表达式。
+ AWS Control Tower Account F@@ [actory For Terraform (AFT) 设置了 Terraform 管道来帮助你在中配置和自定义账户](https://docs.aws.amazon.com/controltower/latest/userguide/aft-overview.html)。 AWS Control Tower AFT 提供基于 Terraform 的账户配置，同时允许您使用管理账户。 AWS Control Tower
+ [Amazon Data Firehos](https://docs.aws.amazon.com/firehose/latest/dev/what-is-this-service.html) e 向亚马逊 S3、亚马逊 Redshift 和亚马逊服务等目的地提供实时流数据。 OpenSearch 它可自动扩展以满足数据吞吐量要求，并且无需进行日常管理。
+ [Amazon Elastic Kubernetes Service（Amazon EKS）](https://docs.aws.amazon.com/eks/latest/userguide/what-is-eks.html)是一项托管式容器编排服务，可让您轻松地使用 Kubernetes 部署、管理和扩展容器化应用程序。它会自动管理 Kubernetes 控制面板节点的可用性和可扩展性。
+ [AWS Key Management Service (AWS KMS)](https://docs.aws.amazon.com/kms/latest/developerguide/overview.html) 创建和控制用于加密数据的加密密钥。 AWS KMS 与其他服务集成 AWS 服务 ，以帮助您保护使用这些服务存储的数据。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一项无服务器计算服务，使您无需预调配或管理服务器即可运行代码。它会通过响应每个触发器来运行代码，从而自动扩展应用程序，且仅按您使用的计算时间收费。
+ [Amazon Relational Database Service（Amazon RDS）](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Welcome.html)是一项托管式关系数据库服务，用户能够在云中轻松地设置、操作和扩展关系数据库。它提供经济高效且可调整的容量，同时自动执行耗时的管理任务。
+ [Amazon Simple Queue Service（Amazon SQS）](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html)是一种消息队列服务，可解耦和扩展微服务、分布式系统和无服务器应用程序。它消除了管理和操作面向消息的中间件的复杂性。
+ [Amazon Simple Storage Service（Amazon S3）](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html)是一种基于云的对象存储服务，可提供出色的可扩展性、数据可用性、安全性和性能。该服务可在 Web 上的任何位置存储和检索任意数量的数据。

**其他工具**
+ [Terraform](https://www.terraform.io/) 是一款基础设施即代码 (IaC) 工具 HashiCorp ，可帮助您创建和管理云和本地资源。

**代码**

此模式的代码可在 GitHub[集中式日志](https://github.com/aws-samples/sample-centralised-logging-at-enterprise-scale-using-terraform)存储库中找到。

## 最佳实践
<a name="set-up-centralized-logging-at-enterprise-scale-by-using-terraform-best-practices"></a>
+ [AWS 账户 在中的单个组织中使用多](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_accounts.html)个 AWS Organizations。此实践可实现跨账户的集中式管理和标准化日志记录。
+ [为 S3 存储桶配置版本控制、生命周期策略和跨区域复制](https://docs.aws.amazon.com/AmazonS3/latest/userguide/replication.html)。实施加密和访问日志记录，以确保安全性及合规性。
+ [通过使用带有标准时间戳和字段的 JSON 格式实施通用日志记录标准](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_AnalyzeLogData-discoverable-fields.html)。使用一致的前缀结构和相关性 IDs ，便于跟踪和分析。
+ 通过[AWS KMS 加密和最低权限访问启用安全控制](https://docs.aws.amazon.com/AmazonS3/latest/userguide/security-best-practices.html)。保持 AWS CloudTrail 监控和定期轮换密钥以增强安全性。
+ 设置配送跟踪的[CloudWatch 指标和提醒](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/working_with_metrics.html)。通过自动通知监控成本与性能。
+ 配置 [Amazon S3 保留策略](https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-lifecycle-mgmt.html)以满足合规性要求，并启用 Amazon S3 服务器访问日志记录，跟踪对 S3 存储桶发起的所有请求。保留 S3 存储桶策略和生命周期规则的相关文档。定期审查访问日志、存储桶权限和存储配置，以帮助确保合规性与[安全最佳实践](https://docs.aws.amazon.com/AmazonS3/latest/userguide/security-best-practices.html)。

## 操作说明
<a name="set-up-centralized-logging-at-enterprise-scale-by-using-terraform-epics"></a>

### 设置 AWS Control Tower、AFT 和应用程序账户
<a name="set-up-ctowerlong-aft-and-application-accounts"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 使用 AFT 设置 AWS Control Tower 环境。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-centralized-logging-at-enterprise-scale-by-using-terraform.html) | AWS 管理员 | 
| 为组织启用资源共享。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-centralized-logging-at-enterprise-scale-by-using-terraform.html) | AWS 管理员 | 
| 验证或预调配应用程序账户。 | 要为您的使用案例预调配新的应用程序账户，请通过 AFT 创建这些账户。有关更多信息，请参阅 AWS Control Tower 文档中的[向 AFT 开通新账户](https://docs.aws.amazon.com/controltower/latest/userguide/aft-provision-account.html)。 | AWS 管理员 | 

### 为应用程序账户设置配置文件
<a name="set-up-configuration-files-for-application-accounts"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 将 `Application_account` 文件夹内容复制到 `aft-account-customizations` 存储库中。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-centralized-logging-at-enterprise-scale-by-using-terraform.html) | DevOps 工程师 | 
| 查看并编辑用于设置应用程序账户的输入参数。 | 在此步骤中，您将设置用于在应用程序账户中创建资源的配置文件，包括 CloudWatch 日志组、 CloudWatch 订阅筛选条件、IAM 角色和策略以及 Amazon RDS、Amazon EKS 和 Lambda 函数的配置详细信息。在 `aft-account-customizations` 存储库的 `Application_account` 文件夹中，根据组织的要求配置 `terraform.tfvars` 文件中的输入参数：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-centralized-logging-at-enterprise-scale-by-using-terraform.html) | DevOps 工程师 | 

### 为日志存档账户设置配置文件
<a name="set-up-configuration-files-for-the-log-archive-account"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 将 `Log_archive_account` 文件夹内容复制到 `aft-account-customizations` 存储库中。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-centralized-logging-at-enterprise-scale-by-using-terraform.html) | DevOps 工程师 | 
| 查看并编辑用于设置日志存档账户的输入参数。 | 在此步骤中，您需要设置用于在日志存档账户中创建资源的配置文件，包括 Firehose 传输流、S3 存储桶、SQS 队列以及 IAM 角色和策略。在 `aft-account-customizations` 存储库的 `Log_archive_account` 文件夹中，根据组织的要求配置 `terraform.tfvars` 文件中的输入参数：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-centralized-logging-at-enterprise-scale-by-using-terraform.html) | DevOps 工程师 | 

### 运行 Terraform 命令预调配资源
<a name="run-terraform-commands-to-provision-resources"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 选项 1 - 从 AFT 部署 Terraform 配置文件。 | 在 AFT 中，AFT 管道是在您将包含配置更改的代码推送到 GitHub `aft-account-customizations`存储库后触发的。AFT 会自动检测更改并启动账户自定义流程。对 Terraform（`terraform.tfvars`）文件进行更改后，提交更改并将其推送到 `aft-account-customizations` 存储库：<pre>$ git add *<br />$ git commit -m "update message"<br />$ git push origin main</pre>如果您使用的是其他分支（例如 `dev`），请将 `main` 替换为分支名称。 | DevOps 工程师 | 
| 选项 2 - 手动部署 Terraform 配置文件。 | 如果您不使用 AFT 或想要手动部署解决方案，则可以使用 `Application_account` 和 `Log_archive_account` 文件夹中的以下 Terraform 命令：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-centralized-logging-at-enterprise-scale-by-using-terraform.html) | DevOps 工程师 | 

### 验证资源
<a name="validate-resources"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 验证订阅筛选条件。 | 要验证订阅筛选条件是否正确地将日志从应用程序账户日志组转发到日志存档账户，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-centralized-logging-at-enterprise-scale-by-using-terraform.html) | DevOps 工程师 | 
| 验证 Firehose 流。 | 要验证日志存档账户中的 Firehose 流是否成功处理应用程序日志，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-centralized-logging-at-enterprise-scale-by-using-terraform.html) | DevOps 工程师 | 
| 验证集中式 S3 存储桶。 | 要验证集中式 S3 存储桶是否正确接收并整理日志，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-centralized-logging-at-enterprise-scale-by-using-terraform.html) | DevOps 工程师 | 
| 验证 SQS 队列。 | 要验证 SQS 队列是否收到新日志文件的通知，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-centralized-logging-at-enterprise-scale-by-using-terraform.html) | DevOps 工程师 | 

### 清理 资源
<a name="clean-up-resources"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 选项 1 - 从 AFT 停用 Terraform 配置文件。 | 当您删除 Terraform 配置文件并推送更改时，AFT 会自动启动资源删除流程。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-centralized-logging-at-enterprise-scale-by-using-terraform.html) | DevOps 工程师 | 
| 选项 2 – 手动清理 Terraform 资源。 | 如果您不使用 AFT 或想要手动清理资源，则可以使用 `Application_account` 和 `Log_archive_account` 文件夹中的以下 Terraform 命令：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-centralized-logging-at-enterprise-scale-by-using-terraform.html) | DevOps 工程师 | 

## 问题排查
<a name="set-up-centralized-logging-at-enterprise-scale-by-using-terraform-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
|  CloudWatch 日志目标未创建或处于非活动状态。 | 请验证以下内容：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-centralized-logging-at-enterprise-scale-by-using-terraform.html) | 
| 订阅筛选条件失败或停留在待处理状态。 | 请检查以下事项：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-centralized-logging-at-enterprise-scale-by-using-terraform.html) | 
| Firehose 传输流未显示任何传入记录。 | 请验证以下内容：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-centralized-logging-at-enterprise-scale-by-using-terraform.html) | 

## 相关资源
<a name="set-up-centralized-logging-at-enterprise-scale-by-using-terraform-resources"></a>
+ [Terraform 基础设施设置](https://developer.hashicorp.com/terraform/tutorials/aws-get-started)（Terraform 文档）
+ [部署 Terraform 版 Ac AWS Control Tower count Factory (AFT)（文档AWS Control Tower ）](https://docs.aws.amazon.com/controltower/latest/userguide/aft-getting-started.html)
+ [IAM 教程： AWS 账户 使用 IAM 角色委派访问权限](https://docs.aws.amazon.com/IAM/latest/UserGuide/tutorial_cross-account-with-roles.html) (IAMdocumentation)