

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

# 自动执行 AWS 资源评测
<a name="automate-aws-resource-assessment"></a>

*Naveen Suthar、Arun Bagal、Manish Garg 和 Sandeep Gawande，Amazon Web Services*

## Summary
<a name="automate-aws-resource-assessment-summary"></a>

此示例介绍了一种使用 [AWS Cloud Development Kit (AWS CDK)](https://docs.aws.amazon.com/cdk/v2/guide/home.html) 设置资源评测功能的自动方法。通过使用这种模式，运营团队可以自动收集资源审计详细信息，并在单个控制面板上查看部署在 AWS 账户中的所有资源的详细信息。这在以下用例中很有用：
+ 识别基础设施即代码 (IaC) 工具，隔离由不同 IaC 解决方案（例如 Ter [HashiCorp raform](https://www.terraform.io/)、AWS CloudFormation、AWS CDK 和 AW [S](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) [命令行](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html)界面 (AWS CLI)）创建的资源
+ 获取资源审计信息

该解决方案还将帮助领导团队通过单独的控制面板深入了解 AWS 账户中的资源和活动。


| 
| 
| 注意：[Amazon Quick Sight](https://docs.aws.amazon.com/quicksight/latest/user/welcome.html) 是一项付费服务。在运行它来分析数据和创建控制面板之前，请查看 [Amazon Quick Sight 的定价](https://aws.amazon.com/quicksight/pricing/)。 | 
| --- |

## 先决条件和限制
<a name="automate-aws-resource-assessment-prereqs"></a>

**先决条件**
+ 一个活跃的 AWS 账户。
+ AWS Identity and Acccess Management（IAM）角色和权限，具有预配置资源访问权限
+ [创建的 [Amazon Quick 账户](https://docs.aws.amazon.com/quicksight/latest/user/signing-up.html)，可以访问[亚马逊简单存储服务 (Amazon S3) 和亚马逊 A](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) thena](https://docs.aws.amazon.com/athena/latest/ug/what-is.html)
+ 已安装 2.55.1 或更高版本的 AWS CDK。
+ 已安装 3.9 或更高版本的 [Python](https://www.python.org/downloads/release/python-390/)。

**限制**
+ 此解决方案部署至单个 AWS 账户。
+ 除非已设置 AWS 并将数据存储在 S3 存储桶中，否则 CloudTrail 该解决方案不会跟踪部署前发生的事件。

**产品版本**
+ AWS CDK 版本 2.55.1 或更高版本
+ Python 版本 3.9 或更高版本

## 架构
<a name="automate-aws-resource-assessment-architecture"></a>

**目标技术堆栈**
+ Amazon Athena
+ AWS CloudTrail
+ AWS Glue
+ AWS Lambda
+ Amazon Quick Sight
+ Amazon S3

**目标架构**

AWS CDK 代码将部署所有资源，这些资源是在 AWS 账户中设置资源评测功能的必要条件。下图显示了向 AWS Glue、Amazon Athena 和 Quick Sight 发送 CloudTrail 日志的过程。

![\[AWS Glue、Amazon Athena 和 QuickSight 亚马逊的 AWS 资源评估分为六个步骤。\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/a504774e-db7a-4c36-a22c-ce56d252fb58/images/8f2b549d-33a8-4cbf-86fd-33244716b668.png)


1. CloudTrail 将日志发送到 S3 存储桶进行存储。

1. 事件通知会调用 Lambda 函数，以处理日志并生成经过筛选数据。

1. 筛选后的数据存储至另一个 S3 存储桶。

1. 在 S3 存储桶中的筛选数据上设置 AWS Glue 爬网程序，以便在 AWS Glue Data Catalog 表创建架构。

1. 筛选后的数据已就绪，可供 Amazon Athena 查询。

1. Quick Sight 可以访问查询的数据以进行可视化。

**自动化和扩展**
+ 如果 AWS Organizations 中存在组织范围的 CloudTrail 跟踪，则该解决方案可以从一个 AWS 账户扩展到多个 AWS 账户。通过在组织 CloudTrail 级别部署，您还可以使用此解决方案来获取所有所需资源的资源审计详细信息。
+ 此模式使用 AWS 无服务器资源部署解决方案。

## 工具
<a name="automate-aws-resource-assessment-tools"></a>

**Amazon Web Services**
+ [Amazon Athena](https://docs.aws.amazon.com/athena/latest/ug/what-is.html) 是一种交互式查询服务，可帮助您使用标准 SQL 直接在 Amazon S3 中分析数据。
+ [AWS Cloud Development Kit (AWS CDK)](https://docs.aws.amazon.com/cdk/latest/guide/home.html) 是一个软件开发框架，可帮助您在代码中定义和预调配 Amazon Web Services Cloud 基础设施。
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 可帮助您设置 AWS 资源，快速一致地配置这些资源，并在 AWS 账户和 AWS 区域的整个生命周期中对其进行管理。
+ [AWS CloudTrail](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-user-guide.html) 可帮助您审计 AWS 账户的治理、合规和运营风险。
+ [AWS Glue](https://docs.aws.amazon.com/glue/latest/dg/what-is-glue.html) 是一项完全托管的提取、转换、加载（ETL）服务。它可以帮助您在数据存储和数据流之间对数据进行可靠地分类、清理、扩充和移动。此模式使用 AWS Glue 爬网程序与 AWS Glue Data Catalog 表。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一项计算服务，可帮助您运行代码，而无需预置或管理服务器。它仅在需要时运行您的代码，并且能自动扩缩，因此您只需为使用的计算时间付费。
+ [Amazon Quick](https://docs.aws.amazon.com/quicksight/latest/user/welcome.html) 是一项云规模的商业智能 (BI) 服务，可帮助您在单个控制面板中可视化、分析和报告数据。
+ [Amazon Simple Storage Service（Amazon S3）](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html)是一项基于云的对象存储服务，可帮助您存储、保护和检索任意数量的数据。

**代码存储库 **

此模式的代码可在 GitHub [infrastructure-assessment-iac-automation](https://github.com/aws-samples/infrastructure-assessment-iac-automation)存储库中找到。

此代码存储库包含以下文件和文件夹：
+ `lib` 文件夹 — 用于创建 AWS 资源的 AWS CDK 构造 Python 文件
+ `src/lambda_code` — 在 Lambda 函数中运行的 Python 代码
+ `requirements.txt` — 必须安装的所有 Python 依赖项列表
+ `cdk.json` — 用于提供启动资源所需值的输入文件

## 最佳实践
<a name="automate-aws-resource-assessment-best-practices"></a>

为 Lambda 函数设置监控和警报。有关更多信息，请参阅 [Lambda 函数监控和故障排除](https://docs.aws.amazon.com/lambda/latest/dg/lambda-monitoring.html)。有关使用 Lambda 函数时的一般最佳实践标准，请参阅 [AWS 文档](https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html)

## 操作说明
<a name="automate-aws-resource-assessment-epics"></a>

### 设置您的环境
<a name="set-up-your-environment"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 在本地机器克隆存储库。 | 要克隆存储库，请运行 `git clone https://github.com/aws-samples/infrastructure-assessment-iac-automation.git`命令。 | AWS DevOps， DevOps 工程师 | 
| 设置 Python 虚拟环境以及安装所需依赖项。 | 要设置 Python 虚拟环境，请运行以下命令。<pre>cd infrastructure-assessment-iac-automation<br />python3 -m venv .venv<br />source .venv/bin/activate</pre>要设置所需依赖项，请运行命令 `pip install -r requirements.txt`。 | AWS DevOps， DevOps 工程师 | 
| 设置 AWS CDK 环境和合成 AWS CDK 代码。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-aws-resource-assessment.html) | AWS DevOps， DevOps 工程师 | 

### 在本地机器上设置 AWS 凭证
<a name="set-up-aws-credentials-on-your-local-machine"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 为待部署堆栈的账户和区域导出变量。 | 要使用环境变量为 AWS CDK 提供 AWS 凭证，请运行以下命令。<pre>export CDK_DEFAULT_ACCOUNT=<12 Digit AWS Account Number><br />export CDK_DEFAULT_REGION=<region></pre> | AWS DevOps， DevOps 工程师 | 
| 设置 AWS CLI 配置文件。 | 要为账户设置 AWS CLI 配置文件，请按 [AWS 文档](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/keys-profiles-credentials.html)中的说明进行操作。 | AWS DevOps， DevOps 工程师 | 

### 配置和部署资源评测工具
<a name="configure-and-deploy-the-resource-assessment-tool"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 在账户中部署资源。 | 若要使用 AWS CDK 在 AWS 账户中部署资源，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-aws-resource-assessment.html) | AWS DevOps | 
| 运行 AWS Glue 爬网程序，并创建数据目录表。 | [AWS Glue 爬网程序](https://docs.aws.amazon.com/glue/latest/dg/add-crawler.html)用于保持数据架构的动态性。该解决方案通过按照 AWS Glue 爬网程序调度程序的定义定期运行爬网程序来创建和更新 [AWS Glue Data Catalog](https://docs.aws.amazon.com/athena/latest/ug/querying-glue-catalog.html) 表中的分区。在输出 S3 存储桶中有数据后，使用以下步骤运行 AWS Glue 爬网程序，并创建用于测试的数据目录表架构：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-aws-resource-assessment.html)AWS CDK 代码将 AWS Glue 爬网程序配置为在特定时间运行，但您也可以按需运行。 | AWS DevOps， DevOps 工程师 | 
| 部署 Quick Sight 构造。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-aws-resource-assessment.html) | AWS DevOps， DevOps 工程师 | 
| 创建快速查看仪表板。 | 要创建示例 Quick Sight 仪表板和分析，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-aws-resource-assessment.html)有关更多信息，请参阅在 Amazon Qu [ick Sight 中开始分析和在 Amazon Q](https://docs.aws.amazon.com/quicksight/latest/user/creating-an-analysis.html) u [ick Sight 中启动视觉类型](https://docs.aws.amazon.com/quicksight/latest/user/working-with-visual-types.html)。 | AWS DevOps， DevOps 工程师 | 

### 清理解决方案中的所有 AWS 资源
<a name="clean-up-all-aws-resources-in-the-solution"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 移除 AWS 资源。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-aws-resource-assessment.html) | AWS DevOps， DevOps 工程师 | 

### 基于 AWS 资源评测工具自动化设置其他功能
<a name="set-up-additional-features-on-top-of-the-aws-resource-assessment-tool-automation"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 监控和清理手动创建资源。 | （可选）如果您的组织有使用 IaC 工具创建资源的合规性要求，则您可以使用 AWS 资源评测工具，自动获取手动配置的资源，从而实现合规。您也可以使用此工具将资源导入至 IaC 工具或重新创建这些资源。若要监控手动配置的资源，请执行以下概要任务：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-aws-resource-assessment.html) | AWS DevOps， DevOps 工程师 | 

## 问题排查
<a name="automate-aws-resource-assessment-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
| AWS CDK 会返回错误信息。 | 要获得有关 AWS CDK 问题的帮助，请参阅[常见 AWS CDK 问题疑难解答](https://docs.aws.amazon.com/cdk/v2/guide/troubleshooting.html)。 | 

## 相关资源
<a name="automate-aws-resource-assessment-resources"></a>
+ [使用 Python 构建 Lambda 函数](https://docs.aws.amazon.com/lambda/latest/dg/lambda-python.html)
+ [AWS CDK 入门](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html)
+ [在 Python 中使用 AWS CDK](https://docs.aws.amazon.com/cdk/v2/guide/work-with-cdk-python.html)
+ [创建 CloudTrail 日志跟踪](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-create-and-update-a-trail.html)
+ [开始使用 Amazon Quick Sight](https://aws.amazon.com/quicksight/getting-started/)

## 附加信息
<a name="automate-aws-resource-assessment-additional"></a>

**多个账户**

若要为多个账户设置 AWS CLI 凭证，请使用 AWS 配置文件。有关更多信息，请参阅[设置 AWS CLI](https://aws.amazon.com/getting-started/guides/setup-environment/module-three/) 中的*配置多项配置文件*部分。

**AWS CDK 命令**

使用 AWS CDK 时，切记以下有用的命令：
+ 列出应用程序的所有堆栈

  ```
  cdk ls
  ```
+ 发出合成的 AWS 模板 CloudFormation 

  ```
  cdk synth
  ```
+ 将堆栈部署至您的默认 AWS 账户和区域

  ```
  cdk deploy
  ```
+ 将已部署的堆栈与当前状态比较

  ```
  cdk diff
  ```
+ 打开 AWS CDK 文档

  ```
  cdk docs
  ```