

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 跨多個帳戶和區域自動清查 AWS 資源
<a name="automate-aws-resource-inventory"></a>

*Matej Macek，Amazon Web Services*

## 摘要
<a name="automate-aws-resource-inventory-summary"></a>

此模式概述自動化方法，以維護跨多個帳戶和 的完整 AWS 資源庫存 AWS 區域。它旨在協助基礎設施和安全工程師改善其資源管理實務。它使用 AWS Config 來追蹤資源變更、用於查詢的 Amazon Athena，以及用於互動式儀表板的 Amazon Quick Sight。您可以透過部署 AWS CloudFormation 堆疊來實作此解決方案。

此解決方案類似於[使用 Amazon Athena 和 Amazon Quick Sight 視覺化 AWS Config 資料中所呈現的解決方案 ](https://aws.amazon.com/blogs/mt/visualizing-aws-config-data-using-amazon-athena-and-amazon-quicksight/)(AWS 部落格文章）。此模式擴展了該解決方案，以解決下列常見需求，並提供下列主要優點：
+ 以**合規為重**心 – 此方法可協助您符合法規要求，例如 [PCI DSS](https://www.pcisecuritystandards.org/)、[NIST SP 800-53](https://csrc.nist.gov/pubs/sp/800/53/r5/upd1/final)、[ISO/IEC 27001](https://www.iso.org/standard/27001)、[HIPAA](https://www.hhs.gov/programs/hipaa/index.html)、[GDPR](https://gdpr.eu/) 和其他要求正確資產庫存的法規要求。
+ **自訂架構** – 它提供為各種 AWS 資源建立 Quick Sight 儀表板的基礎，讓您可以根據特定需求自訂解決方案。
+ **使用者驅動的增強**功能 – 此方法納入來自真實世界使用案例的意見回饋，並處理對更全面解決方案的請求。

基礎設施、安全和財務團隊通常會在動態、多帳戶或多區域環境中面臨可見性和協作挑戰。此解決方案旨在解決這些挑戰，並大幅減少建立和維護資源庫存所需的時間和精力。結果是資源的集中檢視，可協助您改善資源配置決策、識別和降低風險、最佳化成本，以及改善整體可見性和協同合作。此方法可彌補概念性解決方案與實際實作需求之間的差距，以達成安全性、合規性和營運目的。

## 先決條件和限制
<a name="automate-aws-resource-inventory-prereqs"></a>

**先決條件**
+ 下列作用中 AWS 帳戶：
  + *管理帳戶* - 用於計費、建立帳戶和控制整個組織的存取的集中式帳戶
  + *稽核帳戶* – 用於安全監控、合規檢查和偏離通知的集中式中樞
  + *日誌封存帳戶* – 用於儲存和分析收集的資料的集中式帳戶
+ 在稽核帳戶中， AWS Config [彙總工具](https://docs.aws.amazon.com/config/latest/developerguide/aggregate-data.html)會從目標帳戶和區域收集和彙總組態資料
+ 在日誌封存帳戶中，設定下列項目：
  + Amazon Simple Storage Service (Amazon S3) AWS Config 儲存[貯](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html)體，您可以在其中存放來自彙總工具的資料
  + Amazon Quick [訂閱](https://docs.aws.amazon.com/quicksight/latest/user/signing-up.html)
  + Quick Sight 與 Amazon Athena 之間的[授權連線](https://docs.aws.amazon.com/quicksight/latest/user/athena.html) 
  + 透過 Athena 查詢存取 Amazon S3 儲存貯體的[許可](https://docs.aws.amazon.com/athena/latest/ug/s3-permissions.html) 
+ AWS Command Line Interface (AWS CLI)，[已安裝](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)並[設定](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)
+ 部署佈建下列資源之 CloudFormation 堆疊的許可：
  +  AWS Lambda 函數
  + Amazon S3 通知組態
  + Athena 資料庫、資料表和檢視
  + Quick Sight 資料集和資料來源
+ 在 中執行自動化的許可 AWS Systems Manager
+ 快速存取的許可

**限制**
+ 解決方案倚賴 AWS Config。 AWS Config 通常在偵測到變更後立即記錄資源的組態變更，或依您指定的頻率記錄。不過，這需要盡最大努力，有時可能需要更長的時間。
+ 此解決方案只會追蹤 [AWS Config 支援的資源類型](https://docs.aws.amazon.com/config/latest/developerguide/resource-config-reference.html)。
+ 解決方案不會追蹤其他雲端提供者或內部部署環境的資源庫存。
+ 有些 AWS 服務 不適用於所有 AWS 區域。如需區域可用性，請參閱 AWS 文件中的[服務端點和配額](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)頁面，然後選擇服務的連結。

## Architecture
<a name="automate-aws-resource-inventory-architecture"></a>

下圖顯示簡化的流程，用於收集、組織、分析和視覺化 AWS 組織中多個帳戶的組態和合規資料。

![\[收集和視覺化整個組織的組態和合規資料。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/67a9667a-da19-4dcb-a2fe-62bc94a0541b/images/c9245de1-ac85-4a9e-a0c0-dbcc27a8bb5d.png)


該圖顯示以下工作流程：

1.  AWS Config 彙總器會定期收集目標帳戶和區域中資源的組態和合規資料，然後將資料交付至日誌封存帳戶中的 Amazon S3 儲存貯體。

1. 將新 AWS Config 資料新增至 Amazon S3 儲存貯體會叫用 AWS Lambda 函數。

1. Lambda 函數透過使用對應於每個快照檔案的區域和日期的值來設定索引鍵來分割資料。這有助於 AWS Glue 有效率地查詢和處理組態和合規資料。

1. Amazon Athena 使用 AWS Glue [結構描述](https://docs.aws.amazon.com/glue/latest/dg/schema-registry.html)，針對存放在 Amazon S3 儲存貯體中的資料執行 SQL 查詢。它利用來自 的結構描述中繼資料 AWS Glue 來了解資料的結構。

1. Athena [中的檢視](https://docs.aws.amazon.com/athena/latest/ug/views.html)會定義和擷取目標資料集。

1. Quick Sight 中的[儀表板](https://docs.aws.amazon.com/quicksight/latest/user/using-dashboards.html)可協助您視覺化和分析資料集。

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

**AWS 服務**
+ [Amazon Athena](https://docs.aws.amazon.com/athena/latest/ug/what-is.html) 是一種互動式查詢服務，可協助您使用標準 SQL 直接在 Amazon S3 中分析資料。
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 可協助您設定 AWS 資源、快速且一致地佈建資源，以及在整個 AWS 帳戶 和 生命週期中管理資源 AWS 區域。
+ [AWS Config](https://docs.aws.amazon.com/config/latest/developerguide/WhatIsConfig.html) 提供 中資源的詳細檢視 AWS 帳戶 及其設定方式。它可協助您識別資源彼此之間的關係，以及其組態如何隨著時間而改變。 AWS Config [彙整工具](https://docs.aws.amazon.com/config/latest/developerguide/aggregate-data.html)會從多個 和 區域收集 AWS Config 組態 AWS 帳戶 和合規資料。
+ [AWS Glue](https://docs.aws.amazon.com/glue/latest/dg/what-is-glue.html) 是一種全受管的擷取、轉換和載入 (ETL) 服務。它可協助您可靠地分類、清理、擴充和移動資料存放區和資料串流之間的資料。此模式使用 AWS Glue [資料目錄](https://docs.aws.amazon.com/glue/latest/dg/components-overview.html#data-catalog-intro)和[結構描述登錄](https://docs.aws.amazon.com/glue/latest/dg/schema-registry.html)檔。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一項運算服務，可協助您執行程式碼，無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。
+ [AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_introduction.html) 是一種帳戶管理服務，可協助您將多個 合併 AWS 帳戶 到您建立並集中管理的組織。
+ [Amazon Quick Sight](https://docs.aws.amazon.com/quicksuite/latest/userguide/quick-bi.html) 是一項商業智慧 (BI) 服務，可協助您透過互動式視覺化、儀表板和報告，將原始資料轉換為有意義的洞見。Quick Sight 是 Amazon Quick 的核心元件。
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 是一種雲端型物件儲存服務，可協助您儲存、保護和擷取任何數量的資料。
+ [AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html) 可協助您管理在 中執行的應用程式和基礎設施 AWS 雲端。它可簡化應用程式和資源管理、縮短偵測和解決操作問題的時間，並協助您大規模安全地管理 AWS 資源。[AWS Systems Manager 自動化](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-automation.html)可簡化許多 的常見維護、部署和修復任務 AWS 服務。

**程式碼儲存庫**

此模式的 AWS CloudFormation 範本可在[AWS Config 視覺化](https://github.com/aws-samples/aws-management-and-governance-samples/blob/master/AWSConfig/AWS-Config-Visualization/README.md) GitHub 儲存庫中使用。此 CloudFormation 範本會部署 AWS Systems Manager 自動化 Runbook，該 Runbook AWS Config 設定為與 Amazon Athena 搭配使用。此自動化 AWS Glue 準備與指定的 Amazon S3 儲存貯體連線、在 Amazon Athena 中建立檢視，以及設定儀表板視覺化的 Quick Sight。

## 最佳實務
<a name="automate-aws-resource-inventory-best-practices"></a>
+ 建議您遵循 AWS 規範指引中的[設定和管理安全、多帳戶 AWS 環境 AWS Control Tower](https://docs.aws.amazon.com/prescriptive-guidance/latest/migration-aws-environment/welcome.html)的最佳實務。
+ 我們建議您建立 AWS Config 彙總工具，以收集整個 AWS 組織的組態和合規資料。如需詳細資訊，請參閱 AWS Config 文件中的[多帳戶多區域資料彙總](https://docs.aws.amazon.com/config/latest/developerguide/aggregate-data.html)。
+ 部署此解決方案之前，建議您檢閱 [Amazon S3](https://aws.amazon.com/s3/pricing/)、、[AWS Config](https://aws.amazon.com/config/pricing/)[Athena](https://aws.amazon.com/athena/pricing/) 和 [Quick](https://aws.amazon.com/quicksight/pricing/) 的目前定價資訊。

## 史詩
<a name="automate-aws-resource-inventory-epics"></a>

### 部署 CloudFormation 堆疊
<a name="deploy-the-cfnshort-stack"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 下載 CloudFormation 範本。 | 下載 [Config-QuickSight-Visualization-SSM-Automation.yaml](https://github.com/aws-samples/aws-management-and-governance-samples/blob/master/AWSConfig/AWS-Config-Visualization/cft/Config-QuickSight-Visualization-SSM-Automation.yaml) CloudFormation 範本。 | AWS 管理員、雲端管理員、DevOps 工程師 | 
| 修改 CloudFormation 範本。 | 只有在您使用 [AWS Control Tower](https://aws.amazon.com/controltower/)並由 AWS Config 管理時，才完成此步驟 AWS Control Tower。您需要修改 CloudFormation 範本。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | DevOps 工程師、AWS 管理員 | 
| 建立 CloudFormation 堆疊。 | 遵循[從 CloudFormation 主控台建立堆疊](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-create-stack.html)中的指示。注意下列事項：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | AWS 管理員、雲端管理員、DevOps 工程師 | 

### 在 Systems Manager 中執行自動化
<a name="run-the-automation-in-sys"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 尋找您的快速使用者名稱。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | AWS 管理員、雲端管理員、DevOps 工程師 | 
| 尋找交付管道名稱和 Amazon S3 儲存貯體名稱。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | AWS 管理員、雲端管理員、DevOps 工程師 | 
| 在 Systems Manager 中執行自動化。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | AWS 管理員、雲端管理員、DevOps 工程師 | 

### 在 Quick Sight 中視覺化資料
<a name="visualize-data-in-qsight"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 重新整理資料。 | 若要根據您的特定需求排程資料集重新整理，請遵循[重新整理 SPICE 資料](https://docs.aws.amazon.com/quicksight/latest/user/refreshing-imported-data.html)中的指示。 | AWS 管理員、DevOps 工程師、雲端管理員 | 
| 建立 分析。 | 若要在 Quick Sight 中建立可協助您視覺化資源的儀表板，請遵循在 [Quick Sight 中開始分析](https://docs.aws.amazon.com/quicksuite/latest/userguide/creating-an-analysis.html)中的指示。 | Quick Suite 管理員 | 
| 建立儀表板。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | Quick Suite 管理員 | 

### (選用) 清除
<a name="optional-clean-up"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 刪除 Systems Manager 自動化建立的資源。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | AWS 管理員、雲端管理員、DevOps 工程師 | 
| 刪除 CloudFormation 堆疊。 | 若要刪除`Config-QuickSight-Visualization-SSM-Automation`堆疊中的資源，請遵循[從 CloudFormation 主控台刪除堆疊](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-delete-stack.html)中的指示。 | AWS 管理員、雲端管理員、DevOps 工程師 | 

## 疑難排解
<a name="automate-aws-resource-inventory-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| Amazon Quick 正在嘗試連線至 `us-east-1` AWS 區域，但不允許在該區域中建立資源。 | 服務控制政策限制您在此區域中訂閱 Amazon Quick。在服務控制政策中，手動指定目標 AWS 區域。`<REGION_ID>` 將 取代為適當的區域識別符：<pre>https://<REGION_ID>.quicksight.aws.amazon.com/sn/start/dashboards</pre>以下是範例：<pre>https://eu-central-1.quicksight.aws.amazon.com/sn/start/dashboards</pre> | 
| 在 Amazon Athena 中，您遇到下列訊息：`Before you run your first query, you need to set up a query result location in Amazon S3.` | 請確定您已準備好要存放 Amazon Athena 查詢結果的 Amazon S3 儲存貯體。 Amazon Athena 然後遵循[使用 Amazon Athena 主控台指定查詢結果位置](https://docs.aws.amazon.com/athena/latest/ug/query-results-specify-location-console.html)中的指示。 | 

## 相關資源
<a name="automate-aws-resource-inventory-resources"></a>

**AWS 文件**
+ [AWS Config 文件](https://docs.aws.amazon.com/config/latest/developerguide/WhatIsConfig.html)
+ [Amazon Quick 文件](https://docs.aws.amazon.com/quicksuite/latest/userguide/what-is.html)

**AWS 部落格文章**
+ [使用 自動化 AWS Config 資料視覺化 AWS Systems Manager](https://aws.amazon.com/blogs/mt/automate-aws-config-data-visualization-with-aws-systems-manager/)
+ [如何使用 定期記錄資源組態變更 AWS Config](https://aws.amazon.com/blogs/mt/how-to-record-resource-configuration-changes-periodically-with-aws-config/)

**其他資源**
+ [Amazon Quick Community Learning Center](https://community.amazonquicksight.com/c/learning-center/10/none)
+ [Amazon Quick Community Gallery](https://community.amazonquicksight.com/c/gallery/44)