

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

# 管理
<a name="management-pattern-list"></a>

**Topics**
+ [成本管理](costmanagement-pattern-list.md)
+ [最終使用者運算](endusercomputing-pattern-list.md)
+ [高效能運算](highperformancecomputing-pattern-list.md)
+ [混合雲端](hybrid-pattern-list.md)
+ [管理與治理](governance-pattern-list.md)
+ [訊息與通訊](messagingandcommunications-pattern-list.md)
+ [多帳戶策略](multiaccountstrategy-pattern-list.md)

# 成本管理
<a name="costmanagement-pattern-list"></a>

**Topics**
+ [使用 AWS Cost Explorer 建立 AWS Glue 任務的詳細成本和用量報告 Cost Explorer](create-detailed-cost-and-usage-reports-for-aws-glue-jobs-by-using-aws-cost-explorer.md)
+ [使用 AWS Cost Explorer 建立 Amazon EMR 叢集的詳細成本和用量報告](create-detailed-cost-and-usage-reports-for-amazon-emr-clusters-by-using-aws-cost-explorer.md)
+ [更多模式](costmanagement-more-patterns-pattern-list.md)

# 使用 AWS Cost Explorer 建立 AWS Glue 任務的詳細成本和用量報告 Cost Explorer
<a name="create-detailed-cost-and-usage-reports-for-aws-glue-jobs-by-using-aws-cost-explorer"></a>

*Parijat Bhide 和 Aromal Raj Jayarajan，Amazon Web Services*

## 總結
<a name="create-detailed-cost-and-usage-reports-for-aws-glue-jobs-by-using-aws-cost-explorer-summary"></a>

此模式說明如何透過設定使用者定義的成本分配標籤來追蹤 AWS Glue 資料整合任務的使用成本。 [https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/custom-tags.html](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/custom-tags.html)您可以使用這些標籤，在 AWS Cost Explorer 中為多個維度的任務建立詳細的成本和用量報告。例如，您可以在團隊、專案或成本中心層級追蹤用量成本。

## 先決條件和限制
<a name="create-detailed-cost-and-usage-reports-for-aws-glue-jobs-by-using-aws-cost-explorer-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ 一或多個已啟用使用者定義標籤的 [AWS Glue 任務](https://docs.aws.amazon.com/glue/latest/dg/how-it-works.html) 

## Architecture
<a name="create-detailed-cost-and-usage-reports-for-aws-glue-jobs-by-using-aws-cost-explorer-architecture"></a>

**目標技術堆疊**
+ AWS Glue
+ AWS Cost Explorer

下圖顯示如何套用標籤來追蹤 AWS Glue 任務的使用成本。

![\[在 AWS Glue 任務中建立和套用標籤，以追蹤 AWS Cost Explorer 中的使用成本。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/e0ae6643-713d-423a-9013-b41b30638053/images/f2b74ef1-494d-439b-9aec-5a9d601126a6.png)


該圖顯示以下工作流程：

1. 資料工程師或 AWS 管理員會為 AWS Glue 任務建立使用者定義的成本分配標籤。

1. AWS 管理員會啟用標籤。

1. 這些標籤會向 AWS Cost Explorer 報告中繼資料。

## 工具
<a name="create-detailed-cost-and-usage-reports-for-aws-glue-jobs-by-using-aws-cost-explorer-tools"></a>
+ [AWS Glue](https://docs.aws.amazon.com/glue/latest/dg/what-is-glue.html) 是全受管的擷取、轉換和載入 (ETL) 服務。它可協助您可靠地分類、清理、擴充和移動資料存放區和資料串流之間的資料。
+ [AWS Cost Explorer](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/ce-what-is.html) 可協助您檢視和分析 AWS 成本和用量。

## 史詩
<a name="create-detailed-cost-and-usage-reports-for-aws-glue-jobs-by-using-aws-cost-explorer-epics"></a>

### 為您的 AWS Glue 任務建立和啟用標籤
<a name="create-and-activate-tags-for-your-aws-glue-jobs"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 為您的 AWS Glue 任務建立使用者定義的成本分配標籤。 | **將標籤新增至現有的 AWS Glue 任務**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-detailed-cost-and-usage-reports-for-aws-glue-jobs-by-using-aws-cost-explorer.html)**將標籤新增至新的 AWS Glue 任務**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-detailed-cost-and-usage-reports-for-aws-glue-jobs-by-using-aws-cost-explorer.html)如需詳細資訊，請參閱[《AWS Glue 開發人員指南》中的](https://docs.aws.amazon.com/glue/latest/dg/monitor-tags.html) *AWS Glue 標籤。* | 資料工程師 | 
| 啟用使用者定義的成本分配標籤。 | 遵循 *AWS Billing 使用者指南*中[啟用使用者定義的成本分配標籤](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/activating-tags.html)中的指示。 | AWS 管理員 | 

### 為您的 AWS Glue 任務建立成本和用量報告
<a name="create-cost-and-usage-reports-for-your-aws-glue-jobs"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 使用 AWS Cost Explorer 中的標籤篩選條件，為您的 AWS Glue 任務建立成本和用量報告。 Cost Explorer | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-detailed-cost-and-usage-reports-for-aws-glue-jobs-by-using-aws-cost-explorer.html)如需詳細資訊，請參閱《*AWS Cost Management 使用者指南*》中的[使用 Cost Explorer 探索您的資料](https://docs.aws.amazon.com/cost-management/latest/userguide/ce-exploring-data.html)。 | 一般 AWS、AWS 管理員 | 

# 使用 AWS Cost Explorer 建立 Amazon EMR 叢集的詳細成本和用量報告
<a name="create-detailed-cost-and-usage-reports-for-amazon-emr-clusters-by-using-aws-cost-explorer"></a>

*Parijat Bhide 和 Aromal Raj Jayarajan，Amazon Web Services*

## 總結
<a name="create-detailed-cost-and-usage-reports-for-amazon-emr-clusters-by-using-aws-cost-explorer-summary"></a>

此模式說明如何透過設定[使用者定義的成本分配標籤來追蹤 Amazon EMR 叢集的使用成本](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/custom-tags.html)。您可以使用這些標籤，在 AWS Cost Explorer 中為多個維度的叢集建立詳細的成本和用量報告。例如，您可以在團隊、專案或成本中心層級追蹤用量成本。

## 先決條件和限制
<a name="create-detailed-cost-and-usage-reports-for-amazon-emr-clusters-by-using-aws-cost-explorer-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ 一或多個已啟用使用者定義標籤[的 EMR 叢集](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-gs.html) 

## Architecture
<a name="create-detailed-cost-and-usage-reports-for-amazon-emr-clusters-by-using-aws-cost-explorer-architecture"></a>

**目標技術堆疊**
+ Amazon EMR
+ AWS Cost Explorer

**目標架構**

下圖顯示如何套用標籤來追蹤特定 Amazon EMR 叢集的使用成本。

![\[使用 Amazon EMR 叢集的成本分配標籤。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/3e470077-e3b1-43cf-8cb9-0895fe39e664/images/fb6b78cb-47bb-4ba1-848a-98dba02bdbb2.png)


該圖顯示以下工作流程：

1. 資料工程師或 AWS 管理員會為 Amazon EMR 叢集建立使用者定義的成本分配標籤。

1. AWS 管理員會啟用標籤。

1. 這些標籤會向 AWS Cost Explorer 報告中繼資料。

## 工具
<a name="create-detailed-cost-and-usage-reports-for-amazon-emr-clusters-by-using-aws-cost-explorer-tools"></a>

**工具**
+ [Amazon EMR](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-what-is-emr.html) 是受管叢集平台，可簡化在 AWS 上執行大數據架構，以處理和分析大量資料。
+ [AWS Cost Explorer](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/ce-what-is.html) 可協助您檢視和分析 AWS 成本和用量。

## 史詩
<a name="create-detailed-cost-and-usage-reports-for-amazon-emr-clusters-by-using-aws-cost-explorer-epics"></a>

### 為您的 Amazon EMR 叢集建立和啟用標籤
<a name="create-and-activate-tags-for-your-amazon-emr-clusters"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 為您的 Amazon EMR 叢集建立使用者定義的成本分配標籤。 | **將標籤新增至現有的 Amazon EMR 叢集**請遵循 *Amazon EMR 管理指南*中[將標籤新增至現有叢集](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-plan-tags-add.html)中的指示。**將標籤新增至新的 Amazon EMR 叢集**請遵循《*Amazon EMR 管理指南*》中[將標籤新增至新叢集](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-plan-tags-add-new.html)中的指示。如需如何設定 Amazon EMR 叢集的詳細資訊，請參閱《*Amazon EMR 管理指南*》中的[規劃和設定叢集](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-plan.html)。 | 資料工程師 | 
| 啟用使用者定義的成本分配標籤。 | 請遵循 *AWS Billing 使用者指南*中[啟用使用者定義的成本分配標籤](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/activating-tags.html)中的指示。 | AWS 管理員 | 

### 為您的 Amazon EMR 叢集建立成本和用量報告
<a name="create-cost-and-usage-reports-for-your-amazon-emr-clusters"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 使用 AWS Cost Explorer 中的標籤篩選條件，為您的 Amazon EMR 叢集建立成本和用量報告。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-detailed-cost-and-usage-reports-for-amazon-emr-clusters-by-using-aws-cost-explorer.html)如需詳細資訊，請參閱《*AWS Cost Management 使用者指南*》中的[使用 Cost Explorer 探索您的資料](https://docs.aws.amazon.com/cost-management/latest/userguide/ce-exploring-data.html)。 | 一般 AWS、AWS 管理員 | 

# 更多模式
<a name="costmanagement-more-patterns-pattern-list"></a>

**Topics**
+ [使用 Amazon Bedrock 自動化 AWS 基礎設施操作](automate-aws-infrastructure-operations-by-using-amazon-bedrock.md)
+ [跨多個帳戶和區域自動清查 AWS 資源](automate-aws-resource-inventory.md)
+ [使用 自動建立 Amazon WorkSpaces 應用程式資源 AWS CloudFormation](automate-the-creation-of-appstream-2-0-resources-using-aws-cloudformation.md)
+ [使用 DynamoDB TTL 自動將項目封存至 Amazon S3](automatically-archive-items-to-amazon-s3-using-dynamodb-ttl.md)
+ [使用 AWS Systems Manager 維護 Windows 自動停止和啟動 Amazon RDS 資料庫執行個體](automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows.md)
+ [建立 Amazon RDS 和 Amazon Aurora 的詳細成本和用量報告](create-detailed-cost-and-usage-reports-for-amazon-rds-and-amazon-aurora.md)
+ [Amazon DynamoDB 資料表的預估儲存成本](estimate-storage-costs-for-an-amazon-dynamodb-table.md)
+ [預估 DynamoDB 資料表的隨需容量成本](estimate-the-cost-of-a-dynamodb-table-for-on-demand-capacity.md)
+ [使用 Amazon EKS Pod Identity 和 KEDA 在 Amazon EKS 中設定事件驅動的自動擴展](event-driven-auto-scaling-with-eks-pod-identity-and-keda.md)
+ [使用 AWS Fargate WaitCondition 勾點建構來協調資源相依性和任務執行](use-the-aws-fargate-waitcondition-hook-construct.md)

# 最終使用者運算
<a name="endusercomputing-pattern-list"></a>

**Topics**
+ [使用 Auth0 和 實作 Amazon WorkSpaces 的 SAML 2.Auth0驗證 AWS Managed Microsoft AD](implement-saml-authentication-for-amazon-workspaces-by-using-auth0-and-aws-managed-microsoft-ad.md)
+ [更多模式](endusercomputing-more-patterns-pattern-list.md)

# 使用 Auth0 和 實作 Amazon WorkSpaces 的 SAML 2.Auth0驗證 AWS Managed Microsoft AD
<a name="implement-saml-authentication-for-amazon-workspaces-by-using-auth0-and-aws-managed-microsoft-ad"></a>

*Siva Vinnakota 和 Shantanu Padhye，Amazon Web Services*

## 總結
<a name="implement-saml-authentication-for-amazon-workspaces-by-using-auth0-and-aws-managed-microsoft-ad-summary"></a>

此模式會探索如何將 Auth0 與 整合 AWS Directory Service for Microsoft Active Directory ，為您的 Amazon WorkSpaces 環境建立強大的 SAML 2.0 身分驗證解決方案。它說明如何在這些項目之間建立聯合 AWS 服務 ，以啟用多重要素驗證 (MFA) 和自訂登入流程等進階功能，同時保留無縫桌面存取 AWS Managed Microsoft AD。無論您是只管理少數使用者還是數千個使用者，此整合都有助於為您的組織提供靈活性和安全性。此模式提供設定程序的步驟，讓您可以在自己的環境中實作此解決方案。

## 先決條件和限制
<a name="implement-saml-authentication-for-amazon-workspaces-by-using-auth0-and-aws-managed-microsoft-ad-prereqs"></a>

**先決條件 **
+ 作用中 AWS 帳戶
+ AWS Managed Microsoft AD
+ Amazon WorkSpaces Personal 中與 相關聯的佈建桌面 AWS Managed Microsoft AD
+ Amazon Elastic Compute Cloud (Amazon EC2) 執行個體
+ Auth0 帳戶

**限制**

有些 AWS 服務 完全無法使用 AWS 區域。如需區域可用性，請參閱[AWS 服務 依區域](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。如需特定端點，請參閱[服務端點和配額](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)頁面，然後選擇服務的連結。

## Architecture
<a name="implement-saml-authentication-for-amazon-workspaces-by-using-auth0-and-aws-managed-microsoft-ad-architecture"></a>

WorkSpaces 用戶端應用程式的 SAML 2.0 身分驗證程序包含五個步驟，如下圖所示。這些步驟代表用於登入的典型工作流程。您可以在遵循此模式中的指示後，使用此分散式方法進行身分驗證，以協助提供結構化且安全的使用者存取方法。

![\[WorkSpaces 用戶端應用程式的 SAML 2.0 身分驗證程序工作流程。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/5a0f227c-c111-495b-9fde-98ae7832bb10/images/957b2a11-e898-4c4f-ae4e-c2e85bfa93a0.png)


 工作流程：

1. **註冊**。使用者啟動 WorkSpaces 的用戶端應用程式，並為其已啟用 SAML 的 WorkSpaces 目錄輸入 WorkSpaces 註冊碼。WorkSpaces 會將 Auth0 身分提供者 (IdP) URL 傳回至用戶端應用程式。

1. **登入。 **WorkSpaces 用戶端會使用 Auth0 URL 重新導向至使用者的 Web 瀏覽器。 使用者使用其使用者名稱和密碼進行身分驗證。Auth0 會將 SAML 聲明傳回至用戶端瀏覽器。SAML 聲明是可宣告使用者身分的加密字符。

1. **驗證**。用戶端瀏覽器會將 SAML 聲明發佈至 AWS 登入 端點以進行驗證。 AWS 登入 允許發起人擔任 AWS Identity and Access Management (IAM) 角色。這會傳回包含 IAM 角色臨時登入資料的字符。

1. **WorkSpaces 登入**。WorkSpaces 用戶端會將權杖呈現給 WorkSpaces 服務端點。WorkSpaces 會交換工作階段字符，並使用登入 URL 將工作階段字符傳回給 WorkSpaces 用戶端。當 WorkSpaces 用戶端載入登入頁面時，使用者名稱值會填入在 SAML 回應中傳遞`NameId`的值。

1. **串流**。使用者輸入其密碼並對 WorkSpaces 目錄進行身分驗證。身分驗證之後，WorkSpaces 會將權杖傳回給用戶端。用戶端會重新導向回 WorkSpaces 服務，並呈現權杖。這會代理 WorkSpaces 用戶端和 WorkSpace 之間的串流工作階段。

**注意**  
若要設定不需要密碼提示的無縫單一登入體驗，請參閱 [ WorkSpaces 文件中的憑證型身分驗證和 WorkSpaces Personal](https://docs.aws.amazon.com/workspaces/latest/adminguide/certificate-based-authentication.html)。 WorkSpaces 

## 工具
<a name="implement-saml-authentication-for-amazon-workspaces-by-using-auth0-and-aws-managed-microsoft-ad-tools"></a>

**AWS 服務**
+ [Amazon WorkSpaces](https://docs.aws.amazon.com/workspaces/latest/adminguide/amazon-workspaces.html) 是一項全受管的虛擬桌面基礎設施 (VDI) 服務，為使用者提供雲端型桌面，而無需購買和部署硬體或安裝複雜的軟體。
+ [AWS Directory Service for Microsoft Active Directory](https://docs.aws.amazon.com/directoryservice/latest/admin-guide/directory_microsoft_ad.html) 可讓您的目錄感知工作負載 AWS 和資源在 中使用 Microsoft Active Directory AWS 雲端。

**其他工具**
+ [Auth0](https://auth0.com/) 是一種身分驗證和授權平台，可協助您管理對應用程式的存取。

## 史詩
<a name="implement-saml-authentication-for-amazon-workspaces-by-using-auth0-and-aws-managed-microsoft-ad-epics"></a>

### 在 Auth0 中設定 Active Directory LDAP 連接器
<a name="configure-the-active-directory-ldap-connector-in-auth0"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 使用 在 Auth0 中安裝 Active Directory LDAP 連接器 AWS Managed Microsoft AD。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-saml-authentication-for-amazon-workspaces-by-using-auth0-and-aws-managed-microsoft-ad.html) | 雲端管理員、雲端架構師 | 
| 在 Auth0 中建立應用程式，以產生 SAML 中繼資料清單檔案。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-saml-authentication-for-amazon-workspaces-by-using-auth0-and-aws-managed-microsoft-ad.html) | 雲端管理員、雲端架構師 | 

### 在 IAM 中設定 SAML 2.0 的 IdP、角色和政策
<a name="set-up-idp-role-and-policy-for-saml-2-0-in-iam"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在 IAM 中建立 SAML 2.0 IdP。 | 若要將 SAML 2.0 設定為 IdP，請遵循 IAM 文件中在 [IAM 中建立 SAML 身分提供者](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_saml.html)中所述的步驟。 | 雲端管理員 | 
| 建立 SAML 2.0 聯合的 IAM 角色和政策。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-saml-authentication-for-amazon-workspaces-by-using-auth0-and-aws-managed-microsoft-ad.html) | 雲端管理員 | 

### 在 Auth0 中設定宣告
<a name="configure-assertions-in-auth0"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定 Auth0 和 SAML 聲明。 | 您可以使用 Auth0 動作在 SAML 2.0 回應中設定宣告。SAML 聲明是可宣告使用者身分的加密字符。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-saml-authentication-for-amazon-workspaces-by-using-auth0-and-aws-managed-microsoft-ad.html)如此即完成 WorkSpaces Personal 桌面的 SAML 2.0 身分驗證設定。[架構](#implement-saml-authentication-for-amazon-workspaces-by-using-auth0-and-aws-managed-microsoft-ad-architecture)區段說明設定後的身分驗證程序。 | 雲端管理員 | 

## 疑難排解
<a name="implement-saml-authentication-for-amazon-workspaces-by-using-auth0-and-aws-managed-microsoft-ad-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| WorkSpaces 中的 SAML 2.0 身分驗證問題** ** | 如果您在實作 WorkSpaces Personal 的 SAML 2.0 身分驗證時遇到任何問題，請遵循 [AWS re：Post 文章](https://repost.aws/knowledge-center/workspaces-saml-authentication-issues)中關於對 SAML 2.0 身分驗證進行疑難排解的步驟和連結。如需有關在存取 WorkSpaces 時調查 SAML 2.0 錯誤的其他資訊，請參閱：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-saml-authentication-for-amazon-workspaces-by-using-auth0-and-aws-managed-microsoft-ad.html) | 

## 相關資源
<a name="implement-saml-authentication-for-amazon-workspaces-by-using-auth0-and-aws-managed-microsoft-ad-resources"></a>
+ [為 WorkSpaces Personal 設定 SAML 2.0](https://docs.aws.amazon.com/workspaces/latest/adminguide/setting-up-saml.html) (WorkSpaces 文件）
+ [Auth0 文件](https://auth0.com/docs)

# 更多模式
<a name="endusercomputing-more-patterns-pattern-list"></a>

**Topics**
+ [使用 自動建立 Amazon WorkSpaces 應用程式資源 AWS CloudFormation](automate-the-creation-of-appstream-2-0-resources-using-aws-cloudformation.md)
+ [改善 Amazon Connect 聯絡中心中客服人員工作站的通話品質](improve-call-quality-on-agent-workstations-in-amazon-connect-contact-centers.md)
+ [從 AWS Step Functions 同步執行 AWS Systems Manager 自動化任務 AWS Step Functions](run-aws-systems-manager-automation-tasks-synchronously-from-aws-step-functions.md)

# 高效能運算
<a name="highperformancecomputing-pattern-list"></a>

**Topics**
+ [使用 Terraform 和 DRA 部署 Lustre 檔案系統以進行高效能資料處理](deploy-lustre-file-system-for-high-performance-data-processing-with-terraform-dra.md)
+ [設定 AWS ParallelCluster 的 Grafana 監控儀表板](set-up-a-grafana-monitoring-dashboard-for-aws-parallelcluster.md)
+ [更多模式](highperformancecomputing-more-patterns-pattern-list.md)

# 使用 Terraform 和 DRA 部署 Lustre 檔案系統以進行高效能資料處理
<a name="deploy-lustre-file-system-for-high-performance-data-processing-with-terraform-dra"></a>

*Arun Bagal 和 Ishwar Chauthaiwale，Amazon Web Services*

## 總結
<a name="deploy-lustre-file-system-for-high-performance-data-processing-with-terraform-dra-summary"></a>

此模式會自動在 上部署 Lustre 檔案系統， AWS 並將其與 Amazon Elastic Compute Cloud (Amazon EC2) 和 Amazon Simple Storage Service (Amazon S3) 整合。

此解決方案可協助您快速設定具有整合式儲存、運算資源和 Amazon S3 資料存取的高效能運算 (HPC) 環境。它結合了 Lustre 的儲存功能與 Amazon EC2 提供的彈性運算選項，以及 Amazon S3 中可擴展的物件儲存，因此您可以在機器學習、HPC 和大數據分析中處理資料密集型工作負載。

模式使用 HashiCorp Terraform 模組和 Amazon FSx for Lustre 來簡化下列程序：
+ 佈建 Lustre 檔案系統
+ 在 FSx for Lustre 和 S3 儲存貯體之間建立資料儲存庫關聯 (DRA)，以將 Lustre 檔案系統與 Amazon S3 物件連結
+ 建立 EC2 執行個體
+ 在 EC2 執行個體上使用 Amazon S3 連結 DRA 掛載 Lustre 檔案系統

此解決方案的優點包括：
+ 模組化設計。您可以輕鬆維護和更新此解決方案的個別元件。
+ 延展性。​ 您可以跨 AWS 帳戶 或 區域快速部署一致的環境。
+ 彈性。您可以自訂部署以符合您的特定需求。
+ 最佳實務。此模式使用遵循 AWS 最佳實務的預先設定模組。

如需 Lustre 檔案系統的詳細資訊，請參閱 [Lustre 網站](https://www.lustre.org/)。

## 先決條件和限制
<a name="deploy-lustre-file-system-for-high-performance-data-processing-with-terraform-dra-prereqs"></a>

**先決條件**
+ 作用中 AWS 帳戶
+ 最低權限 AWS Identity and Access Management (IAM) 政策 （請參閱[說明](https://aws.amazon.com/blogs/security/techniques-for-writing-least-privilege-iam-policies/))

**限制**

FSx for Lustre 會將 Lustre 檔案系統限制在單一可用區域，如果您有高可用性需求，這可能會令人擔憂。如果包含檔案系統的可用區域失敗，則會失去對檔案系統的存取，直到復原為止。若要實現高可用性，您可以使用 DRA 將 Lustre 檔案系統與 Amazon S3 連結，並在可用區域之間傳輸資料。

**產品版本**
+ [Terraform 1.9.3 版或更新版本](https://developer.hashicorp.com/terraform/install?product_intent=terraform)
+ [HashiCorp AWS 提供者 4.0.0 版或更新版本](https://registry.terraform.io/providers/hashicorp/aws/latest)

## Architecture
<a name="deploy-lustre-file-system-for-high-performance-data-processing-with-terraform-dra-architecture"></a>

下圖顯示 FSx for Lustre 和 AWS 服務 中互補的架構 AWS 雲端。

![\[FSx for Lustre 部署搭配 AWS KMS、Amazon EC2、Amazon CloudWatch Logs 和 Amazon S3。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/51d38589-e752-42cd-9f46-59c3c8d0bfd3/images/c1c21952-fd6f-4b1d-9bf8-09b2f4f4459f.png)


架構包含下列項目：
+ S3 儲存貯體可做為資料耐用、可擴展且符合成本效益的儲存位置。FSx for Lustre 和 Amazon S3 之間的整合提供與 Amazon S3 無縫連結的高效能檔案系統。
+ FSx for Lustre 會執行和管理 Lustre 檔案系統。
+ Amazon CloudWatch Logs 會從檔案系統收集和監控日誌資料。這些日誌可讓您深入了解 Lustre 檔案系統的效能、運作狀態和活動。
+ Amazon EC2 用於使用開放原始碼 Lustre 用戶端存取 Lustre 檔案系統。EC2 執行個體可以從相同虛擬私有雲端 (VPC) 中的其他可用區域存取檔案系統。網路組態允許在 VPC 內的子網路之間存取 。在執行個體上掛載 Lustre 檔案系統之後，您可以使用其檔案和目錄，就像使用本機檔案系統一樣。
+ AWS Key Management Service (AWS KMS) 透過提供靜態資料的加密，增強檔案系統的安全性。

**自動化和擴展**

Terraform 可讓您更輕鬆地跨多個環境部署、管理和擴展 Lustre 檔案系統。在 FSx for Lustre 中，單一檔案系統具有大小限制，因此您可能需要建立多個檔案系統來水平擴展。您可以使用 Terraform 根據您的工作負載需求佈建多個 Lustre 檔案系統。

## 工具
<a name="deploy-lustre-file-system-for-high-performance-data-processing-with-terraform-dra-tools"></a>

**AWS 服務**
+ [Amazon CloudWatch Logs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/WhatIsCloudWatchLogs.html) 可協助您集中所有系統、應用程式和 的日誌， AWS 服務 以便您可以監控日誌並將其安全地存檔。
+ [Amazon Elastic Compute Cloud (Amazon EC2)](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html) 在 AWS 雲端中提供可擴展的運算容量。您可以視需要啟動任意數量的虛擬伺服器，，並快速進行擴展或縮減。
+ [Amazon FSx for Lustre](https://docs.aws.amazon.com/fsx/latest/LustreGuide/what-is.html) 可讓您輕鬆且經濟實惠地啟動、執行和擴展高效能 Lustre 檔案系統。
+ [AWS Key Management Service (AWS KMS)](https://docs.aws.amazon.com/kms/latest/developerguide/overview.html) 可協助您建立和控制密碼編譯金鑰，以協助保護您的資料。
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 是一種雲端型物件儲存服務，可協助您儲存、保護和擷取任何數量的資料。

**程式碼儲存庫**

此模式的程式碼可在 GitHub [Provision FSx for Lustre Filesystem 中使用 Terraform](https://github.com/aws-samples/provision-fsx-lustre-with-terraform) 儲存庫。

## 最佳實務
<a name="deploy-lustre-file-system-for-high-performance-data-processing-with-terraform-dra-best-practices"></a>
+ 下列變數定義 Lustre 檔案系統。請務必根據您的環境正確設定這些項目，如 [Epics](#deploy-lustre-file-system-for-high-performance-data-processing-with-terraform-dra-epics) 一節所述。
  + `storage_capacity` – Lustre 檔案系統的儲存容量，以 GiBs為單位。最小和預設設定為 1200 GiB。
  + `deployment_type` – Lustre 檔案系統的部署類型。如需兩個選項 `PERSISTENT_1`和 `PERSISTENT_2`（預設） 的說明，請參閱 [FSx for Lustre 文件](https://docs.aws.amazon.com/fsx/latest/LustreGuide/using-fsx-lustre.html#persistent-file-system)。
  + `per_unit_storage_throughput` – 讀取和寫入輸送量，以每秒每 TiB MBs為單位。 
  + `subnet_id` – 您要部署 FSx for Lustre 的私有子網路 ID。
  + `vpc_id` – 您想要在 AWS 其中部署 FSx for Lustre 的虛擬私有雲端 ID。
  + `data_repository_path` – 將連結至 Lustre 檔案系統的 S3 儲存貯體路徑。
  + `iam_instance_profile` – 用來啟動 EC2 執行個體的 IAM 執行個體描述檔。
  + `kms_key_id` – 將用於資料加密之 AWS KMS 金鑰的 Amazon Resource Name (ARN)。
+ 使用 和 `vpc_id`變數，確保 VPC 內的適當網路存取`security_group`和放置。
+ 如 [Epics](#deploy-lustre-file-system-for-high-performance-data-processing-with-terraform-dra-epics) 章節所述執行 `terraform plan`命令，以在套用變更之前預覽和驗證變更。這有助於發現潛在問題，並確保您知道要部署的內容。
+ 如 [Epics](#deploy-lustre-file-system-for-high-performance-data-processing-with-terraform-dra-epics) 一節所述使用 `terraform validate`命令來檢查語法錯誤，並確認組態是否正確。

## 史詩
<a name="deploy-lustre-file-system-for-high-performance-data-processing-with-terraform-dra-epics"></a>

### 設定您的環境
<a name="set-up-your-environment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 安裝 Terraform。 | 若要在本機電腦上安裝 Terraform，請遵循 [Terraform 文件](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli)中的指示。 | AWS DevOps，DevOps 工程師 | 
| 設定 AWS 登入資料。 | 若要設定帳戶的 AWS Command Line Interface (AWS CLI) 設定檔，請遵循 [AWS 文件](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html)中的指示。 | AWS DevOps，DevOps 工程師 | 
| 複製 GitHub 儲存庫。 | 若要複製 GitHub 儲存庫，請執行 命令：<pre>git clone https://github.com/aws-samples/provision-fsx-lustre-with-terraform.git</pre> | AWS DevOps，DevOps 工程師 | 

### 設定和部署 FSx for Lustre
<a name="configure-and-deploy-fsxlustre"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 更新部署組態。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-lustre-file-system-for-high-performance-data-processing-with-terraform-dra.html) | AWS DevOps，DevOps 工程師 | 
| 初始化 Terraform 環境。 | 若要初始化您的環境以執行 Terraform `fsx_deployment`模組，請執行：<pre>terraform init</pre> | AWS DevOps，DevOps 工程師 | 
| 驗證 Terraform 語法。 | 若要檢查語法錯誤並確認組態是否正確，請執行：<pre>terraform validate </pre> | AWS DevOps，DevOps 工程師 | 
| 驗證 Terraform 組態。 | 若要建立 Terraform 執行計劃並預覽部署，請執行：<pre>terraform plan -var-file terraform.tfvars</pre> | AWS DevOps，DevOps 工程師 | 
| 部署 Terraform 模組。 | 若要部署 FSx for Lustre 資源，請執行：<pre>terraform apply -var-file terraform.tfvars</pre> | AWS DevOps，DevOps 工程師 | 

### 清除 AWS 資源
<a name="clean-up-aws-resources"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 移除 AWS 資源。 | 完成使用 FSx for Lustre 環境後，您可以移除 Terraform 部署 AWS 的資源，以避免產生不必要的費用。程式碼儲存庫中提供的 Terraform 模組會自動執行此清除。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-lustre-file-system-for-high-performance-data-processing-with-terraform-dra.html) | AWS DevOps，DevOps 工程師 | 

## 疑難排解
<a name="deploy-lustre-file-system-for-high-performance-data-processing-with-terraform-dra-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| FSx for Lustre 傳回錯誤。 | 如需 FSx for Lustre 問題的協助，請參閱 [FSx for Lustre 文件中的疑難排解 Amazon](https://docs.aws.amazon.com/fsx/latest/LustreGuide/troubleshooting.html) FSx for Lustre。 | 

## 相關資源
<a name="deploy-lustre-file-system-for-high-performance-data-processing-with-terraform-dra-resources"></a>
+ [使用 Terraform 建置 Amazon FSx for Lustre](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/fsx_lustre_file_system) (Terraform 文件中的AWS 提供者參考）
+ [Amazon FSx for Lustre 入門](https://docs.aws.amazon.com/fsx/latest/LustreGuide/getting-started.html) (FSx for Lustre 文件）
+ [AWS 有關 Amazon FSx for Lustre 的部落格文章](https://aws.amazon.com/blogs/storage/tag/amazon-fsx-for-lustre/)

# 設定 AWS ParallelCluster 的 Grafana 監控儀表板
<a name="set-up-a-grafana-monitoring-dashboard-for-aws-parallelcluster"></a>

*Dario La Porta 和 William Lu，Amazon Web Services*

## 總結
<a name="set-up-a-grafana-monitoring-dashboard-for-aws-parallelcluster-summary"></a>

AWS ParallelCluster 可協助您部署和管理高效能運算 (HPC) 叢集。它支援 AWS Batch 和 Slurm 開放原始碼任務排程器。雖然 AWS ParallelCluster 已與 Amazon CloudWatch 整合，用於記錄和指標，但不會為工作負載提供監控儀表板。

[AWS ParallelCluster (GitHub) 的 Grafana 儀表板](https://github.com/aws-samples/aws-parallelcluster-monitoring)是 AWS ParallelCluster 的監控儀表板。 GitHub 它在作業系統 (OS) 層級提供任務排程器洞察和詳細監控指標。如需此解決方案中包含之儀表板的詳細資訊，請參閱 GitHub 儲存庫中[的範例儀表板](https://github.com/aws-samples/aws-parallelcluster-monitoring#example-dashboards)。這些指標可協助您進一步了解 HPC 工作負載及其效能。不過，最新版本的 AWS ParallelCluster 或解決方案中使用的開放原始碼套件不會更新儀表板程式碼。此模式可增強解決方案，以提供下列優點：
+ 支援 AWS ParallelCluster v3
+ 使用最新版本的開放原始碼套件，包括 Prometheus、Grafana、Prometheus Slurm Exporter 和 NVIDIA DCGM-Exporter
+ 增加 Slurm 任務使用的 CPU 核心和 GPUs 數量
+ 新增任務監控儀表板
+ 為具有 4 或 8 個圖形處理單元 (GPUs) 的節點增強 GPU 節點監控儀表板

此增強型解決方案版本已在 AWS 客戶的 HPC 生產環境中實作和驗證。

## 先決條件和限制
<a name="set-up-a-grafana-monitoring-dashboard-for-aws-parallelcluster-prereqs"></a>

**先決條件**
+ [AWS ParallelCluster CLI](https://docs.aws.amazon.com/parallelcluster/latest/ug/pcluster-v3.html)，已安裝並設定。
+ AWS ParallelCluster 支援[的網路組態](https://docs.aws.amazon.com/parallelcluster/latest/ug/iam-roles-in-parallelcluster-v3.html)。此模式使用 [AWS ParallelCluster 使用兩個子網路](https://docs.aws.amazon.com/parallelcluster/latest/ug/network-configuration-v3.html#network-configuration-v3-two-subnets)組態，需要公有子網路、私有子網路、網際網路閘道和 NAT 閘道。
+ 所有 AWS ParallelCluster 叢集節點都必須具有網際網路存取權。這是必要的，以便安裝指令碼可以下載開放原始碼軟體和 Docker 映像。
+ Amazon Elastic Compute Cloud (Amazon EC2) 中的[金鑰對](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html)。具有此金鑰對的資源具有前端節點的安全殼層 (SSH) 存取權。

**限制**
+ 此模式旨在支援 Ubuntu 20.04 LTS。如果您使用的是不同版本的 Ubuntu，或是使用 Amazon Linux 或 CentOS，則需要修改此解決方案隨附的指令碼。這些修改不包含在此模式中。

**產品版本**
+ Ubuntu 20.04 LTS
+ ParallelCluster 3.X

**帳單和成本考量**
+ 免費方案不會涵蓋在此模式中部署的解決方案。Amazon EC2、Amazon FSx for Lustre、Amazon VPC 中的 NAT 閘道和 Amazon Route 53 需支付費用。

## Architecture
<a name="set-up-a-grafana-monitoring-dashboard-for-aws-parallelcluster-architecture"></a>

**目標架構**

下圖顯示使用者如何存取前端節點上 AWS ParallelCluster 的監控儀表板。前端節點執行 NICE DCV、Prometheus、Grafana、Prometheus Slurm Exporter、Prometheus Node Exporter 和 NGINX Open Source。運算節點會執行 Prometheus Node Exporter，如果節點包含 GPUs，也會執行 NVIDIA DCGM-Exporter。前端節點會從運算節點擷取資訊，並在 Grafana 儀表板中顯示該資料。

![\[存取前端節點上 AWS ParallelCluster 的監控儀表板。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/a2132c94-98e0-4b90-8be0-99ebfa546442/images/d2255792-f66a-4ef2-8f04-cc3d5482db5f.png)


在大多數情況下，前端節點不會大量載入，因為任務排程器不需要大量的 CPU 或記憶體。使用者在連接埠 443 上使用 SSL 存取前端節點上的儀表板。

所有授權檢視者都可以匿名檢視監控儀表板。只有 Grafana 管理員可以修改儀表板。您可以在 `aws-parallelcluster-monitoring/docker-compose/docker-compose.head.yml` 檔案中設定 Grafana 管理員的密碼。

## 工具
<a name="set-up-a-grafana-monitoring-dashboard-for-aws-parallelcluster-tools"></a>

**AWS 服務**
+ [NICE DCV](https://docs.aws.amazon.com/dcv/#nice-dcv) 是一種高效能遠端顯示通訊協定，可協助您在不同的網路條件下，將遠端桌面和應用程式串流從任何雲端或資料中心交付到任何裝置。
+ [AWS ParallelCluster](https://docs.aws.amazon.com/parallelcluster/latest/ug/what-is-aws-parallelcluster.html) 可協助您部署和管理高效能運算 (HPC) 叢集。它支援 AWS Batch 和 Slurm 開放原始碼任務排程器。
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 是一種雲端型物件儲存服務，可協助您儲存、保護和擷取任何數量的資料。
+ [Amazon Virtual Private Cloud (Amazon VPC)](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) 可協助您在已定義的虛擬網路中啟動 AWS 資源。

**其他工具**
+ [Docker](https://www.docker.com/) 是一組平台即服務 (PaaS) 產品，可在作業系統層級使用虛擬化在容器中交付軟體。
+ [Grafana](https://grafana.com/docs/grafana/latest/introduction/) 是一種開放原始碼軟體，可協助您查詢、視覺化、提醒和探索指標、日誌和追蹤。
+ [NGINX 開放原始碼](https://nginx.org/en/docs/?_ga=2.187509224.1322712425.1699399865-405102969.1699399865)是開放原始碼 Web 伺服器和反向代理。
+ [NVIDIA Data Center GPU Manager (DCGM)](https://docs.nvidia.com/data-center-gpu-manager-dcgm/index.html) 是一組工具，可用於管理和監控叢集環境中的 NVIDIA 資料中心圖形處理單元 (GPUs)。在此模式中，您會使用 [DCGM-Exporter](https://github.com/NVIDIA/dcgm-exporter)，這可協助您從 Prometheus 匯出 GPU 指標。
+ [Prometheus](https://prometheus.io/docs/introduction/overview/) 是一種開放原始碼系統監控工具組，可收集並儲存其指標，做為具有相關鍵值對的時間序列資料，稱為*標籤*。在此模式中，您也使用 [Prometheus Slurm Exporter](https://github.com/vpenso/prometheus-slurm-exporter) 收集和匯出指標，並使用 [Prometheus Node Exporter](https://github.com/prometheus/node_exporter) 從運算節點匯出指標。
+ [Ubuntu](https://help.ubuntu.com/) 是一種開放原始碼的 Linux 作業系統，專為企業伺服器、桌面、雲端環境和 IoT 而設計。

**程式碼儲存庫**

此模式的程式碼可在 GitHub [pcluster-monitoring-dashboard](https://github.com/aws-samples/parallelcluster-monitoring-dashboard) 儲存庫中使用。

## 史詩
<a name="set-up-a-grafana-monitoring-dashboard-for-aws-parallelcluster-epics"></a>

### 建立必要的資源
<a name="create-the-required-resources"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 S3 儲存貯體。 | 建立 Amazon S3 儲存貯體。您可以使用此儲存貯體來存放組態指令碼。如需說明，請參閱 Amazon S3 文件中的[建立儲存貯](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html)體。 | 一般 AWS | 
| 複製儲存庫。 | 執行下列命令，複製 GitHub [pcluster-monitoring-dashboard](https://github.com/aws-samples/parallelcluster-monitoring-dashboard/tree/main/aws-parallelcluster-monitoring) 儲存庫。<pre>git clone https://github.com/aws-samples/parallelcluster-monitoring-dashboard.git</pre> | DevOps 工程師 | 
| 建立管理員密碼。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-a-grafana-monitoring-dashboard-for-aws-parallelcluster.html) | Linux Shell 指令碼 | 
| 將必要的檔案複製到 S3 儲存貯體。 | 將 [post\$1install.sh](https://github.com/aws-samples/parallelcluster-monitoring-dashboard/blob/main/post_install.sh) 指令碼和 [aws-parallelcluster-monitoring](https://github.com/aws-samples/parallelcluster-monitoring-dashboard/tree/main/aws-parallelcluster-monitoring) 資料夾複製到您建立的 S3 儲存貯體。如需說明，請參閱 [Amazon S3 文件中的上傳物件](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html)。 Amazon S3  | 一般 AWS | 
| 為前端節點設定額外的安全群組。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-a-grafana-monitoring-dashboard-for-aws-parallelcluster.html) | AWS 管理員 | 
| 設定前端節點的 IAM 政策。 | 為前端節點建立身分型政策。此政策允許節點從 Amazon CloudWatch 擷取指標資料。GitHub 儲存庫包含範例[政策](https://github.com/aws-samples/parallelcluster-monitoring-dashboard/blob/main/policies/head_node.json)。如需說明，請參閱 AWS Identity and Access Management [(IAM) 文件中的建立 IAM 政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html)。 | AWS 管理員 | 
| 設定運算節點的 IAM 政策。 | 為運算節點建立身分型政策。此政策允許節點建立包含任務 ID 和任務擁有者的標籤。GitHub 儲存庫包含範例[政策](https://github.com/aws-samples/parallelcluster-monitoring-dashboard/blob/main/policies/compute_node.json)。如需說明，請參閱 [IAM 文件中的建立 IAM 政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html)。如果您使用提供的範例檔案，請取代下列值：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-a-grafana-monitoring-dashboard-for-aws-parallelcluster.html) | AWS 管理員 | 

### 建立叢集
<a name="create-the-cluster"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 修改提供的叢集範本檔案。 | 建立 AWS ParallelCluster 叢集。使用提供的 [cluster.yaml](https://github.com/aws-samples/parallelcluster-monitoring-dashboard/blob/main/cluster.yaml) AWS CloudFormation 範本檔案作為建立叢集的起點。在提供的範本中取代下列值：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-a-grafana-monitoring-dashboard-for-aws-parallelcluster.html) | AWS 管理員 | 
| 建立 叢集 | 在 AWS ParallelCluster CLI 中，輸入下列命令。這會部署 CloudFormation 範本並建立叢集。如需此命令的詳細資訊，請參閱 AWS [ParallelCluster 文件中的 pcluster create-cluster](https://docs.aws.amazon.com/parallelcluster/latest/ug/pcluster.create-cluster-v3.html)。 ParallelCluster <pre>pcluster create-cluster -n <cluster_name> -c cluster.yaml</pre> | AWS 管理員 | 
| 監控叢集建立。 | 輸入下列命令來監控叢集建立。如需此命令的詳細資訊，請參閱 AWS [ParallelCluster 文件中的 pcluster describe-cluster](https://docs.aws.amazon.com/parallelcluster/latest/ug/pcluster.describe-cluster-v3.html)。 ParallelCluster <pre>pcluster describe-cluster -n <cluster_name></pre> | AWS 管理員 | 

### 使用 Grafana 儀表板
<a name="using-the-grafana-dashboards"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 存取 Grafana 入口網站。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-a-grafana-monitoring-dashboard-for-aws-parallelcluster.html) | AWS 管理員 | 

### 清除解決方案以停止產生相關聯的成本
<a name="clean-up-the-solution-to-stop-incurring-associated-costs"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 刪除叢集。 | 輸入下列命令來刪除叢集。如需此命令的詳細資訊，請參閱 AWS ParallelCluster 文件中的 [pcluster delete-cluster](https://docs.aws.amazon.com/parallelcluster/latest/ug/pcluster.delete-cluster-v3.html)。 ParallelCluster <pre>pcluster delete-cluster -n <cluster_name></pre> | AWS 管理員 | 
| 刪除 IAM 政策。 | 刪除您為前端節點和運算節點建立的政策。如需刪除政策的詳細資訊，請參閱 [IAM 文件中的刪除 IAM 政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-delete.html)。 | AWS 管理員 | 
| 刪除安全群組和規則。 | 刪除您為前端節點建立的安全群組。如需詳細資訊，請參閱 Amazon VPC 文件中的[刪除安全群組規則](https://docs.aws.amazon.com/vpc/latest/userguide/working-with-security-groups.html#deleting-security-group-rules)和[刪除安全群組](https://docs.aws.amazon.com/vpc/latest/userguide/working-with-security-groups.html#deleting-security-groups)。 | AWS 管理員 | 
| 刪除 S3 儲存貯體。 | 刪除您建立來存放組態指令碼的 S3 儲存貯體。如需詳細資訊，請參閱 Amazon S3 文件中的[刪除儲存貯](https://docs.aws.amazon.com/AmazonS3/latest/userguide/delete-bucket.html)體。 | 一般 AWS | 

## 疑難排解
<a name="set-up-a-grafana-monitoring-dashboard-for-aws-parallelcluster-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| 無法在瀏覽器中存取前端節點。 | 檢查安全群組並確認傳入連接埠 443 已開啟。 | 
| Grafana 未開啟。 | 在前端節點上，檢查 的容器日誌`docker logs Grafana`。 | 
| 有些指標沒有資料。 | 在前端節點上，檢查所有容器的容器日誌。 | 

## 相關資源
<a name="set-up-a-grafana-monitoring-dashboard-for-aws-parallelcluster-resources"></a>

**AWS 文件**
+ [適用於 Amazon EC2 的 IAM 政策](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-policies-for-amazon-ec2.html)

**其他 AWS 資源**
+ [AWS ParallelCluster](https://aws.amazon.com/hpc/parallelcluster/)
+ [AWS ParallelCluster 的監控儀表板](https://aws.amazon.com/blogs/compute/monitoring-dashboard-for-aws-parallelcluster/) (AWS 部落格文章）

**其他資源**
+ [Prometheus 監控系統](https://prometheus.io/)
+ [格拉法納](https://grafana.com/)

# 更多模式
<a name="highperformancecomputing-more-patterns-pattern-list"></a>

**Topics**
+ [使用 K8sGPT 和 Amazon Bedrock 整合實作採用 AI 技術的 Kubernetes 診斷和故障診斷](implement-ai-powered-kubernetes-diagnostics-and-troubleshooting-with-k8sgpt-and-amazon-bedrock-integration.md)

# 混合雲端
<a name="hybrid-pattern-list"></a>

**Topics**
+ [使用 AWS CDK 和 GitLab 在 Amazon ECS Anywhere 上設定混合工作負載的 CI/CD 管道](set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab.md)
+ [更多模式](hybrid-more-patterns-pattern-list.md)

# 使用 AWS CDK 和 GitLab 在 Amazon ECS Anywhere 上設定混合工作負載的 CI/CD 管道
<a name="set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab"></a>

*Rafael Ortiz，Amazon Web Services*

## 摘要
<a name="set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab-summary"></a>

Amazon ECS Anywhere 是 Amazon Elastic Container Service (Amazon ECS) 的延伸。它支援向 Amazon ECS 叢集註冊*外部執行個體*，例如現場部署伺服器或虛擬機器 (VM)。 功能有助於降低成本並減輕複雜的本機容器協同運作和操作。您可以使用 ECS Anywhere 在內部部署和雲端環境中部署和執行容器應用程式。它消除了您的團隊學習多個網域和技能集的需求，或自行管理複雜軟體的需求。

此模式描述使用 step-by-step方法。 Amazon ECS Anywhere 然後，您可以使用 AWS CodePipeline 來設定持續整合和持續部署 (CI/CD) 管道。然後，您將 GitLab 程式碼儲存庫複寫至 AWS CodeCommit，並在 Amazon ECS 叢集上部署容器化應用程式。

此模式旨在協助使用現場部署基礎設施來執行容器應用程式，並使用 GitLab 管理應用程式程式碼庫的人員。您可以使用 AWS 雲端服務來管理這些工作負載，而不會干擾現有的現場部署基礎設施。

## 先決條件和限制
<a name="set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ 在內部部署基礎設施上執行的容器應用程式。
+ 您管理應用程式程式碼庫的 GitLab 儲存庫。如需詳細資訊，請參閱[儲存庫](https://docs.gitlab.com/ee/user/project/repository/) (GitLab)。
+ 安裝並設定 AWS Command Line Interface (AWS CLI)。如需詳細資訊，請參閱[安裝或更新最新版本的 AWS CLI ](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)(AWS CLI 文件）。
+ AWS CDK Toolkit，全域安裝和設定。如需詳細資訊，請參閱[安裝 AWS CDK](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html#getting_started_install) (AWS CDK 文件）。
+ npm，在 TypeScript 中為 AWS CDK 安裝和設定。如需詳細資訊，請參閱[下載並安裝 Node.js 和 npm ](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)(npm 文件）。

**限制**
+ 如需限制和考量，請參閱 [Amazon ECS Anywhere)](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-anywhere.html#ecs-anywhere-considerations)。

**產品版本**
+ AWS CDK Toolkit 2.27.0 版或更新版本
+ npm 7.20.3 版或更新版本
+ Node.js 16.6.1 版或更新版本

## Architecture
<a name="set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab-architecture"></a>

**目標技術堆疊**
+ AWS CDK
+ AWS CloudFormation
+ AWS CodeBuild
+ AWS CodeCommit
+ AWS CodePipeline
+ Amazon ECS Anywhere
+ Amazon Elastic Container Registry (Amazon ECR)
+ AWS Identity and Access Management (IAM)
+ AWS System Manager
+ GitLab 儲存庫

**目標架構**

![\[設定 Amazon ECS 叢集和 CI/CD 管道的架構圖。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/b0f35986-a839-4b01-8eb0-4748182ddafc/images/85b8d4d9-3591-4d69-a54b-64aa543498f1.png)


此圖表代表此模式中描述的兩個主要工作流程，佈建 Amazon ECS 叢集並設定 CI/CD 管道，以設定和部署 CI/CD 管道，如下所示：

1. **佈建 Amazon ECS 叢集**

   1. 當您部署第一個 AWS CDK 堆疊時，它會在 AWS 上建立 CloudFormation 堆疊。

   1. 此 CloudFormation 堆疊會佈建 Amazon ECS 叢集和相關的 AWS 資源。

   1. 若要向 Amazon ECS 叢集註冊外部執行個體，您必須在 VM 上安裝 AWS Systems Manager Agent (SSM Agent)，並將 VM 註冊為 AWS Systems Manager 受管執行個體。 

   1. 您還必須在 VM 上安裝 Amazon ECS 容器代理程式和 Docker，以將其註冊為 Amazon ECS 叢集的外部執行個體。

   1. 使用 Amazon ECS 叢集註冊和設定外部執行個體時，它可以在您的 VM 上執行多個容器，其已註冊為外部執行個體。

   1. Amazon ECS 叢集處於作用中狀態，可以透過容器執行應用程式工作負載。Amazon ECS Anywhere 容器執行個體會在內部部署環境中執行，但與雲端中的 Amazon ECS 叢集相關聯。

1. **設定和部署 CI/CD 管道**

   1. 當您部署第二個 AWS CDK 堆疊時，它會在 AWS 上建立另一個 CloudFormation 堆疊。

   1. 此 CloudFormation 堆疊會在 CodePipeline 和相關的 AWS 資源中佈建管道。

   1. 您可以將應用程式程式碼變更推送並合併到現場部署 GitLab 儲存庫。 

   1. GitLab 儲存庫會自動複寫至 CodeCommit 儲存庫。

   1. CodeCommit 儲存庫的更新會自動啟動 CodePipeline。 

   1. CodePipeline 從 CodeCommit 複製程式碼，並在 CodeBuild 中建立可部署的應用程式建置。

   1. CodePipeline 會建立 CodeBuild 組建環境的 Docker 映像，並將其推送至 Amazon ECR 儲存庫。

   1. CodePipeline 會啟動 CodeDeploy 動作，從 Amazon ECR 儲存庫提取容器映像。

   1. CodePipeline 在 Amazon ECS 叢集上部署容器映像。

**自動化和擴展**

此模式使用 AWS CDK 做為基礎設施做為程式碼 (IaC) 工具來設定和部署此架構。AWS CDK 可協助您協調 AWS 資源，並設定 Amazon ECS Anywhere 和 CI/CD 管道。

## 工具
<a name="set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab-tools"></a>

**AWS 服務**
+ [AWS 雲端開發套件 (AWS CDK)](https://docs.aws.amazon.com/cdk/latest/guide/home.html) 是一種軟體開發架構，可協助您在程式碼中定義和佈建 AWS 雲端基礎設施。
+ [AWS CodeCommit](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html) 是一種版本控制服務，可協助您私下存放和管理 Git 儲存庫，而無需管理您自己的來源控制系統。
+ [AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html) 可協助您快速建模和設定軟體版本的不同階段，並自動化持續發行軟體變更所需的步驟。
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 是一種開放原始碼工具，可協助您透過命令列 shell 中的命令與 AWS 服務互動。
+ [Amazon Elastic Container Registry (Amazon ECR)](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html) 是一種受管容器映像登錄服務，安全、可擴展且可靠。
+ [Amazon Elastic Container Service (Amazon ECS)](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/Welcome.html) 是快速、可擴展的容器管理服務，可協助您執行、停止和管理叢集上的容器。此模式也使用 [Amazon ECS Anywhere](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-anywhere.html)，可提供將內部部署伺服器或 VM 註冊至 Amazon ECS 叢集的支援。

**其他工具**
+ [Node.js](https://nodejs.org/en/docs/) 是一種事件驅動的 JavaScript 執行期環境，旨在建置可擴展的網路應用程式。
+ [npm](https://docs.npmjs.com/about-npm) 是在 Node.js 環境中執行的軟體登錄檔，用於共用或借用套件和管理私有套件的部署。
+ [Vagrant](https://developer.hashicorp.com/vagrant/docs) 是一種開放原始碼公用程式，用於建置和維護可攜式虛擬軟體開發環境。基於示範目的，此模式使用 Vagrant 來建立內部部署 VM。

**程式碼儲存庫**

此模式的程式碼可在 Amazon ECS Anywhere 的 GitHub CI/CD 管道中使用 AWS CDK 儲存庫。 [ Amazon ECS Anywhere ](https://github.com/aws-samples/amazon-ecs-anywhere-cicd-pipeline-cdk-sample) 

## 最佳實務
<a name="set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab-best-practices"></a>

部署此模式時，請考慮下列最佳實務：
+ [使用 AWS CDK 開發和部署雲端基礎設施的最佳實務](https://docs.aws.amazon.com/cdk/v2/guide/best-practices.html)
+ [使用 AWS CDK 開發雲端應用程式的最佳實務](https://aws.amazon.com/blogs/devops/best-practices-for-developing-cloud-applications-with-aws-cdk/) (AWS 部落格文章）

## 史詩
<a name="set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab-epics"></a>

### 驗證 AWS CDK 組態
<a name="verify-the-aws-cdk-configuration"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 驗證 AWS CDK 版本。 | 輸入下列命令來驗證 AWS CDK Toolkit 的版本。<pre>cdk --version</pre>此模式需要 2.27.0 版或更新版本。如果您有較舊的版本，請遵循 [AWS CDK 文件](https://docs.aws.amazon.com/cdk/latest/guide/cli.html)中的指示進行更新。 | DevOps 工程師 | 
| 驗證 npm 版本。 | 輸入下列命令來驗證 npm 的版本。<pre>npm --version</pre>此模式需要 7.20.3 版或更新版本。如果您有較舊的版本，請遵循 [npm 文件](https://docs.npmjs.com/try-the-latest-stable-version-of-npm)中的指示進行更新。 | DevOps 工程師 | 
| 設定 AWS 登入資料。 | 輸入 `aws configure`命令並遵循提示來設定 AWS 登入資料。<pre>$aws configure<br />AWS Access Key ID [None]: <your-access-key-ID><br />AWS Secret Access Key [None]: <your-secret-access-key><br />Default region name [None]: <your-Region-name><br />Default output format [None]:</pre> | DevOps 工程師 | 

### 引導 AWS CDK 環境
<a name="bootstrap-the-aws-cdk-environment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 複製 AWS CDK 程式碼儲存庫。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab.html) | DevOps 工程師 | 
| 引導環境。 | 輸入下列命令，將 CloudFormation 範本部署到您想要使用的帳戶和 AWS 區域。<pre>cdk bootstrap <account-number>/<Region></pre>如需詳細資訊，請參閱 AWS CDK 文件中的[引導](https://docs.aws.amazon.com/cdk/latest/guide/bootstrapping.html)。 | DevOps 工程師 | 

### 建置和部署 Amazon ECS Anywhere 的基礎設施
<a name="build-and-deploy-the-infrastructure-for-amazon-ecs-anywhere"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 安裝套件相依性並編譯 TypeScript 檔案。 | 輸入下列命令來安裝套件相依性並編譯 TypeScript 檔案。<pre>$cd EcsAnywhereCdk<br />$npm install<br />$npm fund </pre>這些命令會從範例儲存庫安裝所有套件。如需詳細資訊，請參閱 [npm 文件中的 npm ci](https://docs.npmjs.com/cli/v7/commands/npm-ci) [和 npm 安裝](https://docs.npmjs.com/cli/v7/commands/npm-install)。如果您在輸入這些命令時收到有關遺失套件的任何錯誤，請參閱此模式的[故障診斷](#set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab-troubleshooting)一節。 | DevOps 工程師 | 
| 建置專案。 | 若要建置專案程式碼，請輸入下列命令。<pre>npm run build</pre>如需建置和部署專案的詳細資訊，請參閱 [AWS CDK 文件中的您的第一個 AWS CDK 應用程式](https://docs.aws.amazon.com/cdk/latest/guide/hello_world.html#:~:text=the%20third%20parameter.-,Synthesize%20an%20AWS%20CloudFormation%20template,-Synthesize%20an%20AWS)。 | DevOps 工程師 | 
| 部署 Amazon ECS Anywhere 基礎設施堆疊。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab.html) | DevOps 工程師 | 
| 驗證堆疊建立和輸出。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab.html) | DevOps 工程師 | 

### 設定內部部署 VM
<a name="set-up-an-on-premises-vm"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定您的 VM。 | 從 Vagrantfile 所在的根目錄輸入 `vagrant up`命令，以建立 Vagrant VM。如需詳細資訊，請參閱 [Vagrant 文件](https://developer.hashicorp.com/vagrant/docs/cli/up)。 | DevOps 工程師 | 
| 將您的 VM 註冊為外部執行個體。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab.html)這會設定您的 VM 是 Amazon ECS Anywhere 外部執行個體，並在 Amazon ECS 叢集中註冊執行個體。如需詳細資訊，請參閱 Amazon ECS 文件中的[向叢集註冊外部執行個體](https://docs.amazonaws.cn/en_us/AmazonECS/latest/developerguide/ecs-anywhere-registration.html)。如果您遇到任何問題，請參閱[故障診斷](#set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab-troubleshooting)一節。 | DevOps 工程師 | 
| 驗證 Amazon ECS Anywhere 和外部 VM 的狀態。 | 若要驗證您的 VM 是否已連線至 Amazon ECS 控制平面並執行，請使用下列命令。<pre>$aws ssm describe-instance-information<br />$aws ecs list-container-instances --cluster $CLUSTER_NAME</pre> | DevOps 工程師 | 

### 部署 CI/CD 管道
<a name="deploy-the-ci-cd-pipeline"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在 CodeCommit 儲存庫中建立分支。 | 透過建立儲存庫的第一個遞交，在 CodeCommit 儲存庫`main`中建立名為 的分支。您可以依照 AWS 文件在 [ CodeCommit 中建立遞交](https://docs.aws.amazon.com/codecommit/latest/userguide/how-to-create-commit.html#create-first-commit)。下列是範例命令。<pre>aws codecommit put-file \<br />  --repository-name EcsAnywhereRepo \<br />  --branch-name main \<br />  --file-path README.md \<br />  --file-content "Test" \<br />  --name "Dev Ops" \<br />  --email "devops@example.com" \<br />  --commit-message "Adding README."</pre> | DevOps 工程師 | 
| 設定儲存庫鏡像。 | 您可以在外部來源之間鏡像 GitLab 儲存庫。您可以選擇哪個儲存庫做為來源。分支、標籤和遞交會自動同步。在託管應用程式的 GitLab 儲存庫與 CodeCommit 儲存庫之間設定推送鏡。如需說明，請參閱[設定從 GitLab 到 CodeCommit 的推播鏡](https://docs.gitlab.com/ee/user/project/repository/mirror/push.html#set-up-a-push-mirror-from-gitlab-to-aws-codecommit) (GitLab 文件）。根據預設，鏡像會自動同步儲存庫。如果您想要手動更新儲存庫，請參閱[更新鏡像 ](https://docs.gitlab.com/ee/user/project/repository/mirror/#update-a-mirror)(GitLab 文件）。 | DevOps 工程師 | 
| 部署 CI/CD 管道堆疊。 | 輸入下列命令來部署`EcsAnywherePipelineStack`堆疊。<pre>$cdk  deploy EcsAnywherePipelineStack</pre> | DevOps 工程師 | 
| 測試 CI/CD 管道。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab.html) | DevOps 工程師 | 

### 清除
<a name="clean-up"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 清除和刪除資源。 | 逐步解說此模式之後，您應該移除您建立的proof-of-concept資源。若要清除，請輸入下列命令。<pre>$cdk destroy EcsAnywherePipelineStack<br />$cdk destroy EcsAnywhereInfraStack</pre> | DevOps 工程師 | 

## 疑難排解
<a name="set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| 安裝套件相依性時遺失套件的錯誤。 | 輸入下列其中一個命令來解決遺失的套件。<pre>$npm ci</pre>或<pre>$npm install -g @aws-cdk/<package_name></pre> | 
| 當您在 VM 上執行 `aws ssm create-activation`命令時，您會收到下列錯誤。`An error occurred (ValidationException) when calling the CreateActivation operation: Nonexistent role or missing ssm service principal in trust policy: arn:aws:iam::000000000000:role/EcsAnywhereInstanceRole` | `EcsAnywhereInfraStack` 堆疊尚未完全部署，且執行此命令所需的 IAM 角色尚未建立。在 CloudFormation 主控台中檢查堆疊狀態。在狀態變更為 後重試命令`CREATE_COMPLETE`。 | 
| Amazon ECS 運作狀態檢查會傳回 `UNHEALTHY`，而您會在 Amazon ECS 主控台中叢集**的服務**區段中看到下列錯誤。`service EcsAnywhereService was unable to place a task because no container instance met all of its requirements. Reason: No Container Instances were found in your cluster.` | 輸入下列命令，在您的 Vagrant VM 上重新啟動 Amazon ECS 代理程式。<pre>$vagrant ssh<br />$sudo systemctl restart ecs<br />$sudo systemctl status ecs</pre> | 

## 相關資源
<a name="set-up-a-ci-cd-pipeline-for-hybrid-workloads-on-amazon-ecs-anywhere-by-using-aws-cdk-and-gitlab-resources"></a>
+ [Amazon ECS Anywhere 行銷頁面](https://aws.amazon.com/ecs/anywhere/)
+ [Amazon ECS Anywhere 文件](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-anywhere.html#ecs-anywhere-considerations)
+ [Amazon ECS Anywhere 示範](https://www.youtube.com/watch?v=-eud6yUXsJM) （影片）
+ [Amazon ECS Anywhere 研討會範例](https://github.com/aws-samples/aws-ecs-anywhere-workshop-samples) (GitHub)
+ [儲存庫鏡像 ](https://docs.gitlab.com/ee/user/project/repository/mirror/)(GitLab 文件）

# 更多模式
<a name="hybrid-more-patterns-pattern-list"></a>

**Topics**
+ [使用 AWS Transit Gateway 自動化區域間對等互連的設定](automate-the-setup-of-inter-region-peering-with-aws-transit-gateway.md)
+ [在做為 Docker 容器 AWS IoT Greengrass V2 執行時部署容器化應用程式](deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.md)
+ [使用 AWS CDK 設定 Amazon ECS Anywhere 來管理內部部署容器應用程式](manage-on-premises-container-applications-by-setting-up-amazon-ecs-anywhere-with-the-aws-cdk.md)
+ [當您從 F5 遷移到 AWS 上的 Application Load Balancer 時修改 HTTP 標頭](modify-http-headers-when-you-migrate-from-f5-to-an-application-load-balancer-on-aws.md)
+ [在 AWS 雲端中重新託管內部部署工作負載：遷移檢查清單](rehost-on-premises-workloads-in-the-aws-cloud-migration-checklist.md)
+ [使用 BMC Discovery 查詢來擷取遷移資料以進行遷移規劃](use-bmc-discovery-queries-to-extract-migration-data-for-migration-planning.md)

# 管理與治理
<a name="governance-pattern-list"></a>

**Topics**
+ [當 Amazon Data Firehose 資源未使用 AWS KMS 金鑰加密時，識別和提醒](identify-and-alert-when-amazon-data-firehose-resources-are-not-encrypted-with-an-aws-kms-key.md)
+ [AWS 帳戶 使用 AFT 自動化新 的 Amazon VPC IPAM IPv4 CIDR 配置](automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.md)
+ [使用 AWS Systems Manager 自動化新增或更新 Windows 登錄項目](automate-adding-or-updating-windows-registry-entries-using-aws-systems-manager.md)
+ [使用 Python 在 AMS 中自動建立 RFC](automatically-create-an-rfc-in-ams-using-python.md)
+ [使用 AWS Systems Manager 維護 Windows 自動停止和啟動 Amazon RDS 資料庫執行個體](automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows.md)
+ [使用 Terraform 集中 AWS Organizations 中的軟體套件分佈](centralize-software-package-distribution-in-aws-organizations-by-using-terraform.md)
+ [使用 NLog 在 Amazon CloudWatch Logs 中設定 .NET 應用程式的記錄](configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog.md)
+ [將 AWS Service Catalog 產品複製到不同的 AWS 帳戶和 AWS 區域](copy-aws-service-catalog-products-across-different-aws-accounts-and-aws-regions.md)
+ [為雲端操作模型建立 RACI 或 RASCI 矩陣](create-a-raci-or-rasci-matrix-for-a-cloud-operating-model.md)
+ [使用 Amazon CloudWatch 異常偵測為自訂指標建立警示](create-alarms-for-custom-metrics-using-amazon-cloudwatch-anomaly-detection.md)
+ [建立使用具有預設加密之 Amazon EBS 磁碟區的 AWS Cloud9 IDE](create-an-aws-cloud9-ide-that-uses-amazon-ebs-volumes-with-default-encryption.md)
+ [自動建立標籤型 Amazon CloudWatch 儀表板](create-tag-based-amazon-cloudwatch-dashboards-automatically.md)
+ [記錄您的 AWS 登陸區域設計](document-your-aws-landing-zone-design.md)
+ [使用 AWS CDK 跨多個 AWS 區域、帳戶和 OUs 啟用 Amazon DevOps Guru，以改善營運效能](improve-operational-performance-by-enabling-amazon-devops-guru-across-multiple-aws-regions-accounts-and-ous-with-the-aws-cdk.md)
+ [使用 Account Factory for Terraform 管理多個帳戶的許可集](govern-permission-sets-aft.md)
+ [使用引導管道實作 Account Factory for Terraform (AFT)](implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.md)
+ [在多個 AWS 帳戶和 AWS 區域中管理 AWS Service Catalog 產品](manage-aws-service-catalog-products-in-multiple-aws-accounts-and-aws-regions.md)
+ [使用 AWS 服務監控 SAP RHEL Pacemaker 叢集](monitor-sap-rhel-pacemaker-clusters-by-using-aws-services.md)
+ [使用 CloudWatch Logs Insights 監控應用程式活動](monitor-application-activity-by-using-cloudwatch-logs-insights.md)
+ [跨多個 監控共用 Amazon Machine Image 的使用 AWS 帳戶](monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts.md)
+ [檢視 AWS 帳戶或組織的 EBS 快照詳細資訊](view-ebs-snapshot-details-for-your-aws-account-or-organization.md)
+ [更多模式](governance-more-patterns-pattern-list.md)

# 當 Amazon Data Firehose 資源未使用 AWS KMS 金鑰加密時，識別和提醒
<a name="identify-and-alert-when-amazon-data-firehose-resources-are-not-encrypted-with-an-aws-kms-key"></a>

*Ram Kandaswamy，Amazon Web Services*

## 總結
<a name="identify-and-alert-when-amazon-data-firehose-resources-are-not-encrypted-with-an-aws-kms-key-summary"></a>

為了合規，某些組織必須在 Amazon Data Firehose 等資料交付資源上啟用加密。此模式顯示一種方法來監控、偵測和通知資源不合規的情況。

為了維持加密需求，此模式可用於 ， AWS 以自動監控和偵測未以 AWS Key Management Service (AWS KMS) 金鑰加密的 Amazon Data Firehose 交付資源。解決方案會傳送提醒通知，並且可以延伸以執行自動修復。此解決方案可套用至個別帳戶或多帳戶環境，例如使用 AWS 登陸區域 或 的環境 AWS Control Tower。

## 先決條件和限制
<a name="identify-and-alert-when-amazon-data-firehose-resources-are-not-encrypted-with-an-aws-kms-key-prereqs"></a>

**先決條件**
+ Amazon Data Firehose 交付串流
+ 有足夠的許可和熟悉度 CloudFormation，用於此基礎設施自動化

**限制**
+ 解決方案不是即時的，因為它使用 AWS CloudTrail 事件進行偵測，而且在建立未加密資源和傳送通知之間存在延遲。

## Architecture
<a name="identify-and-alert-when-amazon-data-firehose-resources-are-not-encrypted-with-an-aws-kms-key-architecture"></a>

**目標技術堆疊**

解決方案使用無伺服器技術和下列服務：
+ AWS CloudTrail
+ Amazon CloudWatch
+ AWS Command Line Interface (AWS CLI)
+ AWS Identity and Access Management (IAM)
+ Amazon Data Firehose
+ AWS Lambda
+ Amazon Simple Notification Service (Amazon SNS)

**目標架構**

![\[Data Firehose 資源未加密時產生提醒的程序。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/897ba8cf-d1c2-4149-98e7-09d3d90d13d6/images/d694f718-bd0c-4d14-a2e4-e0ea58dc048e.png)


圖表說明這些步驟：

1. 使用者建立或修改 Amazon Data Firehose。

1. 偵測到並比對 CloudTrail 事件。

1. 叫用 Lambda。

1. 識別不合規的資源。

1. 系統會傳送電子郵件通知。

**自動化和擴展**

您可以使用 CloudFormation StackSets，透過單一命令將此解決方案套用至多個 AWS 區域 或 帳戶。

## 工具
<a name="identify-and-alert-when-amazon-data-firehose-resources-are-not-encrypted-with-an-aws-kms-key-tools"></a>
+ [AWS CloudTrail](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-user-guide.html) 是 AWS 服務 ，可協助您啟用 的控管、合規，以及操作和風險稽核 AWS 帳戶。使用者、角色或 採取的動作 AWS 服務 會在 CloudTrail 中記錄為事件。事件包括在 AWS 管理主控台、 AWS CLI、 AWS SDKs和 API 操作中採取的動作。
+ [Amazon CloudWatch Events](https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/WhatIsCloudWatchEvents.html) 提供近乎即時的系統事件串流，描述 AWS 資源的變更。
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 是一種開放原始碼工具，可讓您在命令列 Shell 中使用命令 AWS 服務 與 互動。 
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 是一種 Web 服務，可協助您安全地控制對 AWS 資源的存取。您可以使用 IAM 來控制能通過身分驗證 (登入) 和授權使用資源的 (具有許可) 的人員。 
+ [Amazon Data Firehose](https://docs.aws.amazon.com/firehose/latest/dev/what-is-this-service.html) 是一種全受管服務，可提供即時串流資料。使用 Firehose，您不需要撰寫應用程式或管理資源。將您的資料產生來源設定為把資料傳送至 Firehose，它就會將資料自動交付至您指定的目的地。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一種運算服務，支援執行程式碼，無需佈建或管理伺服器。Lambda 只有在需要時才會執行程式碼，可自動從每天數項請求擴展成每秒數千項請求。您只需為使用的運算時間付費，程式碼未執行時無需付費。 
+ [Amazon Simple Notification Service (Amazon SNS)](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) 是一種受管服務，可將訊息從發佈者交付給訂閱者 （也稱為生產者和消費者）。

## 史詩
<a name="identify-and-alert-when-amazon-data-firehose-resources-are-not-encrypted-with-an-aws-kms-key-epics"></a>

### 強制加密以符合規範
<a name="enforce-encryption-for-compliance"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| Deploy CloudFormation StackSets。 | 在 中 AWS CLI，執行下列命令，使用 `firehose-encryption-checker.yaml` 範本 （已連接） 來建立堆疊集。 為 參數提供有效的 Amazon SNS 主題 Amazon Resource Name (ARN)。部署應該成功建立 CloudWatch Events 規則、Lambda 函數，以及具有必要許可的 IAM 角色，如範本中所述。<pre>aws cloudformation create-stack-set    --stack-set-name my-stack-set   --template-body file://firehose-encryption-checker.yaml </pre> | 雲端架構師、系統管理員 | 
| 建立堆疊執行個體。 | 您可以在 AWS 區域 您選擇的 以及一或多個帳戶中建立堆疊。 若要建立堆疊執行個體，請執行下列命令。將堆疊名稱、帳戶號碼和區域取代為您自己的 。<pre>aws cloudformation create-stack-instances     --stack-set-name my-stack-set    --accounts 123456789012 223456789012   --regions us-east-1 us-east-2 us-west-1 us-west-2     --operation-preferences FailureToleranceCount=1 </pre> | 雲端架構師、系統管理員 | 

## 相關資源
<a name="identify-and-alert-when-amazon-data-firehose-resources-are-not-encrypted-with-an-aws-kms-key-resources"></a>
+ [使用 CloudFormation StackSets](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/what-is-cfnstacksets.html)
+ [什麼是 Amazon CloudWatch Events？](https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/WhatIsCloudWatchEvents.html)

## 附件
<a name="attachments-897ba8cf-d1c2-4149-98e7-09d3d90d13d6"></a>

若要存取與本文件相關聯的其他內容，請解壓縮下列檔案： [attachment.zip](samples/p-attach/897ba8cf-d1c2-4149-98e7-09d3d90d13d6/attachments/attachment.zip)

# AWS 帳戶 使用 AFT 自動化新 的 Amazon VPC IPAM IPv4 CIDR 配置
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft"></a>

*Kien Pham 和 Alex Pazik，Amazon Web Services*

## 總結
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft-summary"></a>

此模式說明如何 AWS 帳戶 使用 [AWS Control Tower Account Factory for Terraform (AFT) 自動化新 的 Amazon VPC IP Address Manager (IPAM)](https://docs.aws.amazon.com/controltower/latest/userguide/aft-overview.html) IPv4 CIDR 配置。這是使用帳戶層級自訂來完成的，該自訂會使用 `aft-account-customizations`模組，將 IPv4 CIDR 區塊從 IPAM 配置到新的虛擬私有雲端 (VPC)。

使用 IPAM，您可以大規模組織、指派、監控和稽核 IP 地址，讓您輕鬆地規劃、追蹤和監控 AWS 工作負載的 IP 地址。您可以[建立 IPAM](https://docs.aws.amazon.com/vpc/latest/ipam/create-ipam.html) 和 IPAM 集區，以在帳戶販賣程序期間用來將 IPv4 CIDR 區塊配置給新的 VPC。

## 先決條件和限制
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft-prereqs"></a>

**先決條件**
+ 在支援的 [AWS 區域](https://docs.aws.amazon.com/controltower/latest/userguide/region-how.html)和部署的 AFT 中 AWS Control Tower 啟用 AWS 帳戶 的作用中
+ 支援的[版本控制系統 (VCS) 供應商](https://github.com/aws-ia/terraform-aws-control_tower_account_factory?tab=readme-ov-file#input_vcs_provider)，例如 BitBucket、GitHub 和 GitHub Enterprise
+ [已安裝](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli) Terraform 命令列界面 (CLI)
+ 執行期環境，您可以在其中執行安裝 AFT 的 Terraform 模組
+ 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/getting-started-quickstart.html)

**限制**
+ 有些 AWS 服務 不適用於所有 AWS 區域。如需區域可用性，請參閱[AWS 依區域的服務](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。如需特定端點，請參閱[服務端點和配額](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)，然後選擇服務的連結。

**產品版本**
+ [AWS Control Tower 登陸區域](https://docs.aws.amazon.com/controltower/latest/userguide/2022-all.html#version-3.0) 3.0 版或更新版本，早於 4.0 版
+ [AFT](https://github.com/aws-ia/terraform-aws-control_tower_account_factory) 1.13.0 版或更新版本，早於 2.0.0 版
+ Terraform OSS 1.2.0 版或更新版本，早於 2.0.0 版
+ [Terraform AWS Provider](https://registry.terraform.io/providers/hashicorp/aws/latest/docs) (`terraform-provider-aws`) 5.11.0 版或更新版本，早於 6.0.0 版
+ [IPAM () 2.1.0 版或更新版本的 Terraform 模組](https://github.com/aws-ia/terraform-aws-ipam) `aws-ia/ipam/aws`

## Architecture
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft-architecture"></a>

下圖顯示此模式的工作流程和元件。

![\[建立 Amazon VPC IPAM IPv4 CIDR 配置的工作流程。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/986cfc7d-058b-4490-9029-6cd1eadd1dd2/images/f90b84dd-0420-460e-ac0f-9f22b4a9fdc4.png)


工作流程包含下列主要任務：

1. **觸發器變更** – Terraform 和 IPAM 自訂的變更會遞交至 GitHub 儲存庫並推送。此任務會自動觸發 AWS CodeBuild 管道。

1. **自動化建置** – 在 CodeBuild 中，會觸發多個建置專案 AWS Step Functions。

1. **套用自訂** – Step Functions 與 CodeBuild 協調，以規劃和套用 Terraform 變更。此任務使用 AFT Terraform 模組來協調 AWS 佈建帳戶的 IPAM 集區 IP 指派。

## 工具
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft-tools"></a>

**AWS 服務**
+ [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html) 是一種全受管建置服務，可協助您編譯原始程式碼、執行單元測試，並產生準備好部署的成品。
+ [AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html) 可協助您快速建模和設定軟體版本的不同階段，並自動化持續發行軟體變更所需的步驟。
+ [AWS Control Tower](https://docs.aws.amazon.com/controltower/latest/userguide/what-is-control-tower.html) 協調其他數個 的功能 AWS Organizations，[AWS 服務](https://docs.aws.amazon.com/controltower/latest/userguide/integrated-services.html)包括 AWS Service Catalog和 AWS IAM Identity Center。它可協助您設定和管理 AWS 多帳戶環境，並遵循規範性最佳實務。
+ [Amazon DynamoDB](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html) 是一項全受管 NoSQL 資料庫服務，可提供快速、可預期且可擴展的效能。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一項運算服務，可協助您執行程式碼，無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。
+ [適用於 Python (Boto3) 的 AWS SDK](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html) 是一種軟體開發套件，可協助您整合 Python 應用程式、程式庫或指令碼 AWS 服務。
+ [AWS Service Catalog](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/introduction.html) 可協助您集中管理已核准的 IT 服務目錄 AWS。最終使用者可在機構所設的限制範圍內，迅速地只部署自己需要且經核准的 IT 服務。
+ [AWS Step Functions](https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html) 是一種無伺服器協同運作服務，可協助您結合 AWS Lambda 函數和其他 AWS 服務 來建置業務關鍵型應用程式。
+ [Amazon Virtual Private Cloud (Amazon VPC)](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) 可協助您在已定義的虛擬網路中啟動 AWS 資源。此虛擬網路與您在自己的資料中心中操作的傳統網路相似，且具備使用 AWS可擴展基礎設施的優勢。Amazon VPC IP Address Manager (IPAM) 是一種 VPC 功能，可讓您更輕鬆地規劃、追蹤和監控 AWS 工作負載的 IP 地址。

**其他工具**
+ [GitHub](https://docs.github.com/) 是一種開發人員平台，開發人員可用來建立、存放、管理和共用程式碼。
+ [HashiCorp Terraform](https://www.terraform.io/) 是一種基礎設施即程式碼 (IaC) 工具，可協助您建立和管理雲端和內部部署資源。這包括低階元件，例如運算執行個體、儲存和聯網，以及高階元件，例如 DNS 項目和軟體服務 (SaaS) 功能。
+ [Python](https://www.python.org/) 是一種一般用途的電腦程式設計語言。您可以使用它在 上建置應用程式、自動化任務和開發服務[AWS 雲端](https://aws.amazon.com/developer/language/python/)。

**程式碼儲存庫**
+ 此模式的程式碼可在 GitHub [AWS Control Tower Account Factory for Terraform](https://github.com/aws-ia/terraform-aws-control_tower_account_factory) 儲存庫中取得。

## 最佳實務
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft-best-practices"></a>

部署 AFT 時，建議您遵循最佳實務，以協助確保安全、有效率且成功的實作。實作和操作 AFT 的重要準則和建議包括下列項目：
+ **徹底檢閱輸入 – **仔細檢閱並了解每個[輸入](https://github.com/aws-ia/terraform-aws-control_tower_account_factory)。正確的輸入組態對於 AFT 的設定和運作至關重要。
+ **定期範本更新 **– 使用最新 AWS 功能和 Terraform 版本來更新範本。定期更新可協助您利用新功能並維護安全性。
+ **版本控制 **– 固定您的 AFT 模組版本，並盡可能使用個別的 AFT 部署進行測試。
+ **範圍** – 僅使用 AFT 來部署基礎設施護欄和自訂。請勿使用它來部署您的應用程式。
+ **內嵌和驗證 **– AFT 管道需要內嵌和驗證的 Terraform 組態。在將組態推送至 AFT 儲存庫之前，執行 lint、驗證和測試。
+ **Terraform 模組** – 建置可重複使用的 Terraform 程式碼做為模組，並一律指定 Terraform 和 AWS 提供者版本以符合組織的需求。

## 史詩
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft-epics"></a>

### 設定您的 AWS 環境
<a name="set-up-and-configure-your-aws-environment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署 AWS Control Tower。 |  AWS Control Tower 在您的 AWS 環境中設定和配置，以確保集中管理和控管您的 AWS 帳戶。如需詳細資訊，請參閱 AWS Control Tower 文件中的 [入門 AWS Control Tower](https://docs.aws.amazon.com/controltower/latest/userguide/getting-started-with-control-tower.html)。 | 雲端管理員 | 
| 部署 AWS Control Tower Account Factory for Terraform (AFT)。 | 在新的專用 AFT 管理帳戶中設定 AFT。如需詳細資訊，請參閱 AWS Control Tower 文件中的 [Configure 和啟動 AWS Control Tower Account Factory for Terraform](https://docs.aws.amazon.com/controltower/latest/userguide/aft-getting-started.html#aft-configure-and-launch)。 | 雲端管理員 | 
| 完成 AFT 部署後。 | AFT 基礎設施部署完成後，請完成 AWS Control Tower 文件中的[部署後步驟](https://docs.aws.amazon.com/controltower/latest/userguide/aft-post-deployment.html)。 | 雲端管理員 | 

### 建立 IPAM
<a name="create-ipam"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 委派 IPAM 管理員。 | 若要委派 AWS 組織中的 IPAM 管理員帳戶，請使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html)或者，您可以使用 AWS CLI 並執行下列命令：<pre>aws ec2 enable-ipam-organization-admin-account \<br />    --delegated-admin-account-id 012345678901</pre>如需詳細資訊，請參閱《Amazon VPC 文件》中的將 [IPAM 與 AWS 組織中的帳戶整合](https://docs.aws.amazon.com/vpc/latest/ipam/enable-integ-ipam.html)，以及《 AWS CLI 命令參考》中的 [enable-ipam-organization-admin-account](https://docs.aws.amazon.com/cli/latest/reference/ec2/enable-ipam-organization-admin-account.html)。 若要繼續使用 IPAM，您必須登入委派管理員帳戶。下一個步驟中指定的 SSO 設定檔或 AWS 環境變數必須允許您登入該帳戶，並授予建立 IPAM 頂層和區域集區的許可。 | AWS 管理員 | 
| 建立 IPAM 頂層和區域集區。 | 此模式的 GitHub 儲存庫包含 Terraform 範本，可用來建立 IPAM 頂層集區和區域集區。然後，您可以使用 AWS Resource Access Manager () 與組織、組織單位 (OU) AWS 帳戶或其他資源共用集區AWS RAM。使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html)記下建立後輸出的資源集區 ID。提交帳戶請求時，您將需要 ID。如果您忘記資源集區 ID，可以稍後從 取得 AWS 管理主控台。 請確定建立的集區 CIDRs 不會與您工作區域中的任何其他集區重疊。您可以在沒有 CIDR 的情況下建立集區，但是在為其佈建 CIDR 之前，您將無法使用集區進行配置。您可以隨時編輯集區，將 CIDR 新增至集區。 | AWS 管理員 | 

### 整合 IPAM 與 AFT
<a name="integrate-ipam-with-aft"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 開始建立帳戶自訂。 | 若要開始新的帳戶自訂，請從終端機執行下列命令：<pre># Default name for customization repo<br />cd aft-account-customizations # Replace with your actual repo name if different than the default<br />mkdir -p APG-AFT-IPAM/terraform # Replace APG-AFT-IPAM with your desired customization name<br />cd APG-AFT-IPAM/terraform</pre> | DevOps 工程師 | 
| 建立 `aft-providers.jinja` 檔案。 | 將動態程式碼新增至 檔案`aft-providers.jinja`，指定要使用的 Terraform 後端和提供者。使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html) | DevOps 工程師 | 
| 建立 `backend.jinja` 檔案。 | 將動態程式碼新增至 檔案`backend.jinja`，指定要使用的 Terraform 後端和提供者。使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html) | DevOps 工程師 | 
| 建立 `main.tf` 檔案。 | 建立新的`main.tf`檔案並新增程式碼，以定義從 AWS Systems Manager (`aws_ssm`) 擷取兩個值並建立 VPC 的兩個資料來源。使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html) | DevOps 工程師 | 
| 建立 `variables.tf` 檔案。 | 建立宣告 Terraform 模組所用變數`variables.tf`的檔案。使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html) | DevOps 工程師 | 
| 建立 `terraform.tfvars` 檔案。 | 建立 `terraform.tfvars` 檔案，定義傳遞至 `main.tf` 檔案的變數值。使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html) | DevOps 工程師 | 
| 建立 `outputs.tf` 檔案。 | 建立新的`outputs.tf`檔案，公開 CodeBuild 中的一些值。使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html) | DevOps 工程師 | 
| 遞交自訂項目。 | 若要將新的自訂遞交至帳戶自訂儲存庫，請執行下列命令：<pre># Assumes you are still in the /terraform directory<br />cd .. # Skip if you are in the account customization root directory (APG-AFT-IPAM)<br />git add .<br />git commit -m "APG customization"<br />git push origin</pre> | DevOps 工程師 | 
| 套用自訂。 | 使用新建立的帳戶自訂，將程式碼新增至請求新帳戶的 `account-requests.tf` 檔案。自訂欄位會在使用正確 IPAM 配置的 IPv4 CIDR 建立 VPC 所需的付費帳戶中建立 Systems Manager 參數。使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html) | AWS DevOps | 
| 驗證自訂。 | 登入新付費帳戶並確認已成功套用自訂。使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html) | DevOps 工程師 | 

## 疑難排解
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
|  由於許可不足，您在資源建立或管理時遇到失敗。 |  檢閱連接到 Step Functions、CodeBuild 和部署所涉及其他服務的 AWS Identity and Access Management (IAM) 角色和政策。確認他們具有必要的許可。如果有許可問題，請調整 IAM 政策以授予必要的存取權。 | 
|  您在部署期間達到 AWS 服務 配額。 |  部署管道之前，請檢查 資源的配額，例如 Amazon Simple Storage Service AWS 服務 (Amazon S3) 儲存貯體、IAM 角色和 AWS Lambda 函數。如有必要，請求增加配額。如需詳細資訊，請參閱《AWS 一般參考》**中的[AWS 服務 配額](https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html)。 | 

## 相關資源
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft-resources"></a>

**AWS 服務 文件**
+ [AWS Control Tower 使用者指南](https://docs.aws.amazon.com/controltower/latest/userguide/what-is-control-tower.html)
+ [IPAM 的運作方式](https://docs.aws.amazon.com/vpc/latest/ipam/how-it-works-ipam.html)
+ [IAM 中的安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)
+ [AWS 服務 配額](https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html)

**其他資源**
+ [Terraform AWS 提供者文件](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)

# 使用 AWS Systems Manager 自動化新增或更新 Windows 登錄項目
<a name="automate-adding-or-updating-windows-registry-entries-using-aws-systems-manager"></a>

*Appasaheb Bagali，Amazon Web Services*

## 總結
<a name="automate-adding-or-updating-windows-registry-entries-using-aws-systems-manager-summary"></a>

AWS Systems Manager 是 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體的遠端管理工具。Systems Manager 提供對 Amazon Web Services 上基礎設施的可見性和控制。此多用途工具可用來修正安全性漏洞掃描報告識別為漏洞的 Windows 登錄檔變更。 

此模式涵蓋透過自動化基於環境安全而建議的登錄檔變更，來保護執行 Windows 作業系統之 EC2 執行個體安全的步驟。模式使用 Run 命令來執行 Command 文件。程式碼已連接，一部分包含在*程式碼*區段中。

## 先決條件和限制
<a name="automate-adding-or-updating-windows-registry-entries-using-aws-systems-manager-prereqs"></a>
+ 作用中的 AWS 帳戶
+ 存取 EC2 執行個體和 Systems Manager 的許可

## Architecture
<a name="automate-adding-or-updating-windows-registry-entries-using-aws-systems-manager-architecture"></a>

**目標技術堆疊**
+ 具有兩個子網路和網路位址轉譯 (NAT) 閘道的虛擬私有雲端 (VPC)
+ 用於新增或更新登錄檔名稱和值的 Systems Manager 命令文件
+ Systems Manager Run Command 在指定的 EC2 執行個體上執行命令文件

**目標架構**

![\[如何使用 AWS Systems Manager 自動新增或更新 Windows 登錄項目。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/2ecf680d-9f36-4070-8a19-2af262db7fcc/images/c992bcb0-d894-4aa7-9bb3-3d60c9c79e8d.png)


 

## 工具
<a name="automate-adding-or-updating-windows-registry-entries-using-aws-systems-manager-tools"></a>

**工具**
+ [IAM 政策和角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) – AWS Identity and Access Management (IAM) 是一種 Web 服務，可協助您安全地控制對 AWS 資源的存取。您可以使用 IAM 來控制能通過身分驗證 (登入) 和授權使用資源的 (具有許可) 的人員。
+ [Amazon Simple Storage Service](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) – Amazon Simple Storage Service (Amazon S3) 是網際網路的儲存體。此服務旨在降低開發人員進行網路規模運算的難度。在此模式中，會使用 S3 儲存貯體來存放 Systems Manager 日誌。
+ [AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html) – AWS Systems Manager 是一種 AWS 服務，可用來檢視和控制 AWS 上的基礎設施。Systems Manager 透過掃描受*管執行個體*並報告 （或對其採取修正動作） 偵測到的任何政策違規，協助您維護安全性和合規性。
+ [AWS Systems Manager Command 文件](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-ssm-docs.html) – Run Command 會使用 AWS Systems Manager Command 文件。Systems Manager 支援的所有 Linux 和 Windows Server 作業系統都支援大多數命令文件。
+ [AWS Systems Manager Run Command](https://docs.aws.amazon.com/systems-manager/latest/userguide/execute-remote-commands.html) – AWS Systems Manager Run Command 可讓您從遠端安全地管理受管執行個體的組態。您可以使用 Run Command 自動化常見的管理任務，並大規模執行一次性組態變更。

**Code**

您可以使用下列範例程式碼，將 Microsoft Windows 登錄檔名稱新增至 或更新，將`Version`登錄檔路徑新增至 `HKCU:\Software\ScriptingGuys\Scripts`，並將值新增至 `2`。

```
#Windows registry path which needs to add/update
$registryPath ='HKCU:\\Software\\ScriptingGuys\\Scripts'
#Windows registry Name  which needs to add/update
$Name = 'Version'
#Windows registry value  which needs to add/update
$value = 2
# Test-Path cmdlet to see if the registry key exists. 
IF(!(Test-Path $registryPath))
        {
           New-Item -Path $registryPath -Force | Out-Null
           New-ItemProperty -Path $registryPath -Name $name -Value     $value ` -PropertyType DWORD -                 Force | Out-        Null 
        } ELSE {
                      New-ItemProperty -Path $registryPath -Name $name -Value $value ` -PropertyType            DWORD        -Force | Out-Null
            }
echo 'Registry Path:'$registryPath
 echo 'Registry Name:'$registryPath
 echo 'Registry Value:'(Get-ItemProperty -Path $registryPath -Name $Name).version
```

連接完整的 Systems Manager 命令文件 JavaScript 物件標記法 (JSON) 程式碼範例。 

## 史詩
<a name="automate-adding-or-updating-windows-registry-entries-using-aws-systems-manager-epics"></a>

### 設定 VPC
<a name="set-up-a-vpc"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 VPC。 | 在 AWS 管理主控台上，建立具有公有和私有子網路和 NAT 閘道的 VPC。如需詳細資訊，請參閱 [AWS 文件](https://docs.aws.amazon.com/batch/latest/userguide/create-public-private-vpc.html)。 | 雲端管理員 | 
| 建立安全群組。 | 確保每個安全群組允許從來源 IP 地址存取遠端桌面通訊協定 (RDP)。 | 雲端管理員 | 

### 建立 IAM 政策和 IAM 角色
<a name="create-an-iam-policy-and-an-iam-role"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 IAM 政策。 | 建立可讓您存取 Amazon S3、Amazon EC2 和 Systems Manager 的 IAM 政策。 | 雲端管理員 | 
| 建立 IAM 角色。 | 建立 IAM 角色，並連接提供 Amazon S3、Amazon EC2 和 Systems Manager 存取權的 IAM 政策。 | 雲端管理員 | 

### 執行自動化
<a name="run-the-automation"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 Systems Manager 命令文件。 | 建立 Systems Manager Command 文件，以部署要新增或更新的 Microsoft Windows 登錄檔變更。 | 雲端管理員 | 
| 執行 Systems Manager Run Command。 | 執行 Systems Manager Run Command，選取 Command 文件和 Systems Manager 目標執行個體。這會將所選命令文件中的 Microsoft Windows 登錄檔變更推送到目標執行個體。 | 雲端管理員 | 

## 相關資源
<a name="automate-adding-or-updating-windows-registry-entries-using-aws-systems-manager-resources"></a>
+ [AWS Systems Manager](https://aws.amazon.com/systems-manager/)
+ [AWS Systems Manager 文件](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-ssm-docs.html)
+ [AWS Systems Manager 執行命令](https://docs.aws.amazon.com/systems-manager/latest/userguide/execute-remote-commands.html)

## 附件
<a name="attachments-2ecf680d-9f36-4070-8a19-2af262db7fcc"></a>

若要存取與本文件相關聯的其他內容，請解壓縮下列檔案： [attachment.zip](samples/p-attach/2ecf680d-9f36-4070-8a19-2af262db7fcc/attachments/attachment.zip)

# 使用 Python 在 AMS 中自動建立 RFC
<a name="automatically-create-an-rfc-in-ams-using-python"></a>

*Gnanasekaran Kailasam，Amazon Web Services*

## 摘要
<a name="automatically-create-an-rfc-in-ams-using-python-summary"></a>

AWS Managed Services (AMS) 透過持續管理 Amazon Web Services (AWS) 基礎設施，協助您更有效率且安全地操作雲端型基礎設施。若要變更受管環境，您需要建立並提交新的變更請求 (RFC)，其中包含特定操作或動作的變更類型 (CT) ID。

不過，手動建立 RFC 可能需要大約五分鐘的時間，而組織中的團隊可能需要每天提交多個 RFCs。此模式可協助您自動化 RFC 建立程序、縮短每個 RFC 的建立時間，並消除手動錯誤。  

此模式說明如何使用 Python 程式碼自動建立 `Stop EC2 instance` RFC，以停止 AMS 帳戶中的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體。然後，您可以將此模式的方法和 Python 自動化套用至其他 RFC 類型。 

## 先決條件和限制
<a name="automatically-create-an-rfc-in-ams-using-python-prereqs"></a>

**先決條件**
+ AMS 進階帳戶。如需詳細資訊，請參閱 [AWS Managed Services 文件中的 AMS 操作計劃](https://docs.aws.amazon.com/managedservices/latest/accelerate-guide/what-is-ams-op-plans.html)。 AWS Managed Services 
+ 您的 AMS 帳戶中至少有一個現有的 EC2 執行個體。
+ 了解如何在 AMS 中建立和提交 RFCs。
+ 熟悉 Python。

**限制**
+ 您只能將 RFCs用於 AMS 帳戶中的變更。您的 AWS 帳戶會針對類似的變更使用不同的程序。

## Architecture
<a name="automatically-create-an-rfc-in-ams-using-python-architecture"></a>

**技術堆疊  **
+ AMS
+ AWS 命令列界面 (AWS CLI)
+ 適用於 Python 的 AWS SDK (Boto3)
+ Python 及其所需的套件 (JSON 和 Boto3)

**自動化和擴展**

此模式提供範例程式碼來自動化 `Stop EC2 instance` RFC，但您可以將此模式的範例程式碼和方法用於其他 RFCs。

## 工具
<a name="automatically-create-an-rfc-in-ams-using-python-tools"></a>
+ [AWS Managed Services](https://docs.aws.amazon.com/managedservices/latest/ctexguide/ex-rfc-use-examples.html) – AMS 可協助您更有效率且安全地操作 AWS 基礎設施。
+ [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) – AWS Command Line Interface (AWS CLI) 是管理 AWS 服務的統一工具。在 AMS 中，變更管理 API 提供建立和管理 RFCs的操作。
+ [適用於 Python 的 AWS 開發套件 (Boto3)](https://docs.aws.amazon.com/pythonsdk/) – 適用於 Python 的開發套件可讓您輕鬆地將 Python 應用程式、程式庫或指令碼與 AWS 服務整合。

**Code**

`AMS Stop EC2 Instance.zip` 檔案 （已連接） 包含用於建立 `Stop EC2 instance` RFC 的 Python 程式碼。您也可以將此程式碼設定為為多個 EC2 執行個體提交單一 RFC。

## 史詩
<a name="automatically-create-an-rfc-in-ams-using-python-epics"></a>

### 選項 1 – 設定 macOS 或 Linux 的環境
<a name="option-1-ndash-set-up-environment-for-macos-or-linux"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
|  安裝並驗證 Python。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-create-an-rfc-in-ams-using-python.html) | AWS 系統管理員 | 
|  安裝 AWS CLI。 | 執行 `pip install awscli --upgrade –user`命令來安裝 AWS CLI*。 * | AWS 系統管理員 | 
|  安裝 Boto3。 | 執行 `pip install boto3`命令來安裝 Boto3。 | AWS 系統管理員 | 
| 安裝 JSON。 | 執行 `pip install json`命令來安裝 JSON。 | AWS 系統管理員 | 
| 設定 AMS CLI。 | 登入 AWS 管理主控台，開啟 AMS 主控台，然後選擇 **文件**。下載包含 AMS CLI 的 .zip 檔案，將其解壓縮，然後將其安裝在本機電腦上。安裝 AMS CLI 之後，請執行 `aws amscm help`命令。輸出提供 AMS 變更管理程序的相關資訊。 | AWS 系統管理員 | 

### 選項 2 – 設定 Windows 的環境
<a name="option-2-ndash-set-up-environment-for-windows"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
|  安裝並驗證 Python。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-create-an-rfc-in-ams-using-python.html) | AWS 系統管理員 | 
| 安裝 AWS CLI。 | 執行 `pip install awscli --upgrade –user`命令來安裝 AWS CLI。 | AWS 系統管理員 | 
|  安裝 Boto3。 | 執行 `pip install boto3`命令來安裝 Boto3。 | AWS 系統管理員 | 
| 安裝 JSON。 | 執行 `pip install json`命令來安裝 JSON。 | AWS 系統管理員 | 
| 設定 AMS CLI。 | 登入 AWS 管理主控台，開啟 AMS 主控台，然後選擇 **文件**。下載包含 AMS CLI 的 .zip 檔案，將其解壓縮，然後將其安裝在本機電腦上。安裝 AMS CLI 之後，請執行 `aws amscm help`命令。輸出提供有關 AMS 變更管理程序的資訊 | AWS 系統管理員 | 

### 擷取 RFC 的 CT ID 和執行參數
<a name="extract-the-ct-id-and-execution-parameters-for-the-rfc"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 擷取 RFC 的 CT ID、版本和執行參數。 | 每個 RFC 都有不同的 CT ID、版本和執行參數。您可以使用下列其中一個選項來擷取此資訊：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-create-an-rfc-in-ams-using-python.html)若要針對其他 RFCs 調整此模式的 Python 自動化，請將 `ams_stop_ec2_instance` Python 程式碼檔案中的 CT 類型和參數值取代為您解壓縮`AMS Stop EC2 Instance.zip`的檔案 （已連接）。 | AWS 系統管理員 | 

### 執行 Python 自動化
<a name="run-the-python-automation"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 執行 Python 自動化。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-create-an-rfc-in-ams-using-python.html) | AWS 系統管理員 | 

## 相關資源
<a name="automatically-create-an-rfc-in-ams-using-python-resources"></a>
+ [什麼是變更類型？](https://docs.aws.amazon.com/managedservices/latest/ctexguide/understanding-cts.html)
+ [CLI 教學課程：高可用性雙層堆疊 (Linux/RHEL)](https://docs.aws.amazon.com/managedservices/latest/ctexguide/tut-create-ha-stack.html)

## 附件
<a name="attachments-2b6c68fd-a27e-4c8b-934d-caec50c196ed"></a>

若要存取與本文件相關聯的其他內容，請解壓縮下列檔案： [attachment.zip](samples/p-attach/2b6c68fd-a27e-4c8b-934d-caec50c196ed/attachments/attachment.zip)

# 使用 AWS Systems Manager 維護 Windows 自動停止和啟動 Amazon RDS 資料庫執行個體
<a name="automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows"></a>

*Ashita Dsilva，Amazon Web Services*

## 總結
<a name="automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows-summary"></a>

此模式示範如何使用 AWS Systems Manager 維護時段，依特定排程自動停止和啟動 Amazon Relational Database Service (Amazon RDS) 資料庫執行個體 （例如，在營業時間外關閉資料庫執行個體以降低成本）。為此，Systems Manager 對於典型的使用案例具有成本效益。

AWS Systems Manager 自動化提供 `AWS-StopRdsInstance`和 `AWS-StartRdsInstance` Runbook 來停止和啟動 Amazon RDS 資料庫執行個體。這表示您不需要使用 AWS Lambda 函數撰寫自訂邏輯或建立 Amazon CloudWatch Events 規則。

Systems Manager 提供兩種排程任務的功能：[State Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-state-about.html) [和維護 Windows](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-maintenance.html)。State Manager 會為您的 Amazon Web Services (AWS) 帳戶中的資源設定和維護所需的狀態組態一次，或依特定排程進行。維護 Windows 會在特定時段內對您帳戶中的資源執行任務。雖然您可以在狀態管理員或維護時段使用此模式的方法，但我們建議您使用維護時段，因為它可以根據指派的優先順序執行一或多個任務，也可以執行 AWS Lambda 函數和 AWS Step Functions 任務。如需 State Manager 和維護 Windows 的詳細資訊，請參閱 Systems Manager 文件中的[在 State Manager 和維護 Windows 之間進行選擇](https://docs.aws.amazon.com/systems-manager/latest/userguide/state-manager-vs-maintenance-windows.html)。

此模式提供詳細步驟來設定兩個單獨的維護時段，這些時段使用 cron 表達式來停止，然後啟動 Amazon RDS 資料庫執行個體。 

## 先決條件和限制
<a name="automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows-prereqs"></a>

**先決條件**
+ 作用中 AWS 帳戶。
+ 您要在特定排程中停止和啟動的現有 Amazon RDS 資料庫執行個體。
+ 所需排程的 Cron 表達式。例如，表達式會在每個星期一、星期二、星期三、星期四和星期五的 09：00 `cron(0 9 ? * MON-FRI *)`執行任務。如需詳細資訊，請參閱 Systems Manager 文件中的[維護時段的 Cron 和 Rate 表達](https://docs.aws.amazon.com/systems-manager/latest/userguide/reference-cron-and-rate-expressions.html#reference-cron-and-rate-expressions-maintenance-window)式。
+ 熟悉 Systems Manager。
+ 啟動和停止 RDS 執行個體的許可。如需詳細資訊，請參閱 [Epics](#automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows-epics) 區段。

**限制**
+ Amazon RDS 資料庫執行個體一次最多可停止七天。七天後，資料庫執行個體會自動重新啟動，以確保收到任何必要的維護更新。
+ 您無法停止僅供讀取複本或具有僅供讀取複本的資料庫執行個體。
+ 您無法在多可用區組態中停止 Amazon RDS for SQL Server 資料庫執行個體。
+ 服務配額適用於維護 Windows 和 Systems Manager 自動化。如需服務配額的詳細資訊，請參閱 AWS 一般參考 文件中的[AWS Systems Manager 端點和配額](https://docs.aws.amazon.com/general/latest/gr/ssm.html)。 
+ 有些 AWS 服務 完全無法使用 AWS 區域。如需區域可用性，請參閱[AWS 服務 依區域](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。如需特定端點，請參閱[服務端點和配額](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)頁面，然後選擇服務的連結。

## Architecture
<a name="automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows-architecture"></a>

下圖顯示自動停止和啟動 Amazon RDS 資料庫執行個體的工作流程。

![\[自動停止和啟動 Amazon RDS 資料庫執行個體的工作流程\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/45b81621-5674-4bcf-bf7c-75ae6f62524e/images/7d943830-716e-46a3-be44-7e668c3c01ff.png)


 

工作流程有下列步驟：

1. 建立維護時段並使用 cron 表達式來定義 Amazon RDS 資料庫執行個體的停止和啟動排程。

2. 使用 `AWS-StopRdsInstance`或 `AWS-StartRdsInstance` Runbook 將 Systems Manager Automation 任務註冊到維護時段。

3. 為您的 Amazon RDS 資料庫執行個體使用標籤型資源群組，向維護時段註冊目標。

**技術堆疊**
+ AWS CloudFormation
+ AWS Identity and Access Management (IAM)
+ Amazon RDS
+ Systems Manager

**自動化和擴展**

您可以同時停止和啟動多個 Amazon RDS 資料庫執行個體，方法是標記所需的 Amazon RDS 資料庫執行個體、建立包含所有已標記資料庫執行個體的資源群組，以及將此資源群組註冊為維護時段的目標。

## 工具
<a name="automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows-tools"></a>
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 是一項服務，可協助您建立和設定 AWS 資源的模型。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 是一種 Web 服務，可協助您安全地控制對 AWS 資源的存取。
+ [Amazon Relational Database Service (Amazon RDS)](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Welcome.html) 是一種 Web 服務，可讓您更輕鬆地在 中設定、操作和擴展關聯式資料庫 AWS 雲端。
+ [AWS Resource Groups](https://docs.aws.amazon.com/ARG/latest/userguide/welcome.html) 可協助您將 AWS 資源組織成群組、標記資源，以及管理、監控和自動化分組資源上的任務。
+ [AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html) 是 AWS 服務 ，可用來檢視和控制您的基礎設施 AWS。此模式使用 Systems Manager 的下列功能：
  + [AWS Systems Manager 自動化](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-automation.html)可簡化 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體和其他 AWS 資源的常見維護和部署任務。
  + [AWS Systems Manager 維護 Windows](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-maintenance.html) 可協助您定義何時對執行個體執行潛在破壞性動作的排程。

## 史詩
<a name="automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows-epics"></a>

### 建立和設定 Systems Manager Automation 的 IAM 服務角色
<a name="create-and-configure-the-iam-service-role-for-sys-automation"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定 Systems Manager Automation 的 IAM 服務角色。 | 登入 AWS 管理主控台 並建立 Systems Manager Automation 的服務角色。您可以使用下列兩種方法之一來建立此服務角色：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows.html)Systems Manager Automation 工作流程會使用服務角色在 Amazon RDS 資料庫執行個體上執行啟動和停止動作，以叫用 Amazon RDS。必須使用下列[內嵌政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html#add-policies-console)來設定服務角色，該政策具有啟動和停止 Amazon RDS 資料庫執行個體的許可：<pre>{<br />    "Version": "2012-10-17",		 	 	 <br />    "Statement": [<br />        {<br />            "Sid": "RdsStartStop",<br />            "Effect": "Allow",<br />            "Action": [<br />                "rds:StopDBInstance",<br />                "rds:StartDBInstance"<br />            ],<br />            "Resource": "<RDS_Instance_ARN>"               <br />        },<br />        {<br />            "Sid": "RdsDescribe",<br />            "Effect": "Allow",<br />            "Action": "rds:DescribeDBInstances",<br />            "Resource": "*"<br />        }<br />    ]<br />}</pre>請務必將 `<RDS_Instance_ARN>`取代為 Amazon RDS 資料庫執行個體的 Amazon Resource Name (ARN)。如果您不熟悉使用 IAM 政策和角色，請遵循[排程 Amazon RDS 停止和開始使用 AWS Systems Manager](https://aws.amazon.com/blogs/database/schedule-amazon-rds-stop-and-start-using-aws-systems-manager/)部落格文章的解決方案*概觀*一節中的指示。請務必記錄服務角色的 ARN。 | AWS 管理員 | 

### 建立資源群組
<a name="create-a-resource-group"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 標記 Amazon RDS 資料庫執行個體。 | 開啟 [Amazon RDS 主控台](https://console.aws.amazon.com/rds/)，並標記您要新增至資源群組的 Amazon RDS 資料庫執行個體。標籤是指派給 AWS 資源的中繼資料，由索引鍵/值對組成。我們建議您使用*動作*做為**標籤索引鍵**，並使用 *StartStop* 做為**值**。如需詳細資訊，請參閱 Amazon RDS 文件中的[新增、列出和移除標籤](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_Tagging.html#Tagging.HowTo)。 | AWS 管理員 | 
| 為您的標記 Amazon RDS 資料庫執行個體建立資源群組。 | 開啟 [AWS Resource Groups 主控台](https://console.aws.amazon.com/resource-groups)，並根據您為 Amazon RDS 資料庫執行個體建立的標籤建立資源群組。在**分組條件**下，請確定您為資源類型選擇 **AWS::RDS::DBInstance**，然後提供標籤的鍵值對 （例如，「Action-StartStop」)。這可確保服務只會檢查 Amazon RDS 資料庫執行個體，而不是具有此標籤的其他資源。****請確定您記錄資源群組的名稱。如需詳細資訊和詳細步驟，請參閱 AWS Resource Groups 文件中的[建置標籤型查詢和建立群組](https://docs.aws.amazon.com/ARG/latest/userguide/gettingstarted-query.html#gettingstarted-query-tag-based)。  | AWS 管理員 | 

### 設定維護時段以停止 Amazon RDS 資料庫執行個體
<a name="configure-a-maintenance-window-to-stop-the-rds-db-instances"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立維護時段。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows.html)停止資料庫執行個體的任務會在啟動時幾乎立即執行，而且不會跨越維護時段的整個持續時間。此模式提供**持續時間**和**停止啟動任務**的最小值，因為它們是維護時段的必要參數。如需詳細資訊和詳細步驟，請參閱 Systems Manager 文件中的[建立維護時段 （主控台）](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-maintenance-create-mw.html)。 | AWS 管理員 | 
| 將目標指派給維護時段。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows.html)如需詳細資訊和詳細步驟，請參閱 Systems Manager 文件中的[將目標指派給維護時段 （主控台）](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-maintenance-assign-targets.html)。 | AWS 管理員 | 
| 將任務指派給維護時段。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows.html)******服務角色**選項定義維護時段執行任務所需的服務角色。不過，此角色與您先前為 Systems Manager Automation 建立的服務角色不同。如需詳細資訊和詳細步驟，請參閱 Systems Manager 文件中的將[任務指派給維護時段 （主控台）](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-maintenance-assign-tasks.html)。 | AWS 管理員 | 

### 設定維護時段以啟動 Amazon RDS 資料庫執行個體
<a name="configure-a-maintenance-window-to-start-the-rds-db-instances"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定維護時段以啟動 Amazon RDS 資料庫執行個體。 | 重複*設定維護時段中的步驟，停止 Amazon RDS 資料庫執行個體* epic，以設定另一個維護時段，在排程時間啟動 Amazon RDS 資料庫執行個體。當您設定維護時段以啟動資料庫執行個體時，必須進行下列變更：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows.html) | AWS 管理員 | 

## 相關資源
<a name="automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows-resources"></a>
+ [使用 Systems Manager Automation 文件來管理執行個體並降低非上班時間的成本 ](https://aws.amazon.com/blogs/mt/systems-manager-automation-documents-manage-instances-cut-costs-off-hours/)(AWS 部落格文章）

# 使用 Terraform 集中 AWS Organizations 中的軟體套件分佈
<a name="centralize-software-package-distribution-in-aws-organizations-by-using-terraform"></a>

*Pradip kumar Pandey、Chintamani Aphale、T.V.R.L.Phani Kumar Dadi、Pratap Kumar Nanda、Aarti Rajput 和 Mayuri Shinde、Amazon Web Services*

## 總結
<a name="centralize-software-package-distribution-in-aws-organizations-by-using-terraform-summary"></a>

企業通常會維護分散在多個 的多個 AWS 帳戶 ， AWS 區域 以便在工作負載之間建立強大的隔離障礙。為了保持安全與合規，其管理團隊會安裝代理程式型工具，例如用於安全性掃描的 [CrowdStrike](https://www.crowdstrike.com/falcon-platform/)、[SentinelOne](https://www.sentinelone.com/platform/) 或 [TrendMicro](https://www.trendmicro.com/en_sg/business.html) 工具，以及用於監控的 [Amazon CloudWatch 代理](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Install-CloudWatch-Agent.html)程式、[Datadog Agent](https://www.datadoghq.com/) 或 [AppDynamics 代理](https://www.appdynamics.com/product/how-it-works/agents-and-controller)程式。這些團隊通常會在想要集中自動化軟體套件管理和分佈到這個大型環境中時面臨挑戰。

[Distributor](https://docs.aws.amazon.com/systems-manager/latest/userguide/distributor.html) 是 的一項功能[AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html)，可透過單一簡化界面，將軟體封裝和發佈至雲端和內部部署伺服器中受管 Microsoft Windows 和 Linux 執行個體的程序自動化。此模式示範如何使用 Terraform 來進一步簡化管理軟體安裝的程序，並在 內的大量執行個體和成員帳戶上執行指令碼 AWS Organizations ，而只需最少的努力。

此解決方案適用於由 Systems Manager 管理的 Amazon、Linux 和 Windows 執行個體。

## 先決條件和限制
<a name="centralize-software-package-distribution-in-aws-organizations-by-using-terraform-prereqs"></a>
+ 要安裝軟體的 [Distributor 套件](https://docs.aws.amazon.com/systems-manager/latest/userguide/distributor-working-with-packages-create.html) 
+ [Terraform](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli) 0.15.0 版或更新版本
+ [由 Systems Manager 管理](https://docs.aws.amazon.com/systems-manager/latest/userguide/managed_instances.html)且具有存取目標帳戶中 Amazon [Simple Storage Service (Amazon S3](https://repost.aws/knowledge-center/ec2-instance-access-s3-bucket)Amazon EC2) 執行個體
+ 您組織使用 設定的登陸區域 [AWS Control Tower](https://docs.aws.amazon.com/controltower/latest/userguide/what-is-control-tower.html)
+ （選用） [適用於 Terraform (AFT) 的帳戶工廠](https://catalog.workshops.aws/control-tower/en-US/customization/aft)

## Architecture
<a name="centralize-software-package-distribution-in-aws-organizations-by-using-terraform-architecture"></a>

**資源詳細資訊**

此模式使用 [Account Factory for Terraform (AFT) ](https://catalog.workshops.aws/control-tower/en-US/customization/aft)建立所有必要 AWS 的資源和程式碼管道，以在部署帳戶中部署資源。程式碼管道在兩個儲存庫中執行：
+ **全域自訂**包含將跨向 AFT 註冊的所有帳戶執行的 Terraform 程式碼。
+ **帳戶自訂**包含將在部署帳戶中執行的 Terraform 程式碼。

您也可以在帳戶自訂資料夾中執行 [Terraform](https://developer.hashicorp.com/terraform/intro) 命令，在不使用 AFT 的情況下部署此解決方案。

Terraform 程式碼會部署下列資源：
+ AWS Identity and Access Management (IAM) 角色和政策
  + [SystemsManager-AutomationExecutionRole](https://docs.aws.amazon.com/systems-manager/latest/userguide/running-automations-multiple-accounts-regions.html) 授予使用者在目標帳戶中執行自動化的許可。
  + [SystemsManager-AutomationAdministrationRole](https://docs.aws.amazon.com/systems-manager/latest/userguide/running-automations-multiple-accounts-regions.html) 授予使用者在多個帳戶和組織單位 (OUs許可。
+ 套件的壓縮檔案和 manifest.json
  + 在 Systems Manager 中，[套件](https://docs.aws.amazon.com/systems-manager/latest/userguide/distributor-working-with-packages-create.html)包含至少一個軟體或可安裝資產的 .zip 檔案。
  + JSON 資訊清單包含套件程式碼檔案的指標。
+ S3 bucket (S3 儲存貯體)
  + 跨組織共用的分散式套件會安全地存放在 Amazon S3 儲存貯體中。
+ AWS Systems Manager 文件 (SSM 文件）
  + `DistributeSoftwarePackage` 包含將軟體套件分發到成員帳戶中每個目標執行個體的邏輯。
  + `AddSoftwarePackageToDistributor` 包含可封裝可安裝軟體資產並將其新增至 Automation 的邏輯。 AWS Systems Manager
+ Systems Manager 關聯
  + Systems Manager 關聯用於部署解決方案。

**架構和工作流程**

![\[在 AWS Organizations 中集中軟體套件分佈的架構圖\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/da584449-e12b-4878-a61d-00d8cea3d3d7/images/2718f2c4-f816-4e34-89b8-8182c128e6db.png)


此圖說明了下列步驟：

1. 若要從集中式帳戶執行解決方案，您可以將套件或軟體以及部署步驟上傳至 S3 儲存貯體。

1. 您的自訂套件可在 Systems Manager 主控台[文件](https://ap-southeast-2.console.aws.amazon.com/systems-manager/documents?region=ap-southeast-2)區段的**「由我擁有」**索引標籤中使用。

1. State Manager 是 Systems Manager 的功能，可在整個組織中建立、排程和執行套件的關聯。關聯指定必須先在受管節點上安裝和執行軟體套件，才能安裝在目標節點上。

1. 關聯會指示 Systems Manager 在目標節點上安裝套件。

1. 對於任何後續安裝或變更，使用者可以定期或從單一位置手動執行相同的關聯，以跨帳戶執行部署。

1. 在成員帳戶中，自動化會將部署命令傳送至 Distributor。

1. Distributor 會將軟體套件分散到各個執行個體。

此解決方案使用 內的管理帳戶 AWS Organizations，但您也可以指定 帳戶 （委派管理員） 來代表組織管理此帳戶。

## 工具
<a name="centralize-software-package-distribution-in-aws-organizations-by-using-terraform-tools"></a>

**AWS 服務**
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 是一種雲端型物件儲存服務，可協助您儲存、保護和擷取任何數量的資料。此模式使用 Amazon S3 來集中和安全地存放分散式套件。
+ [AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html) 可協助您管理在 中執行的應用程式和基礎設施 AWS 雲端。它可簡化應用程式和資源管理、縮短偵測和解決操作問題的時間，並協助您大規模安全地管理 AWS 資源。此模式使用以下 Systems Manager 功能：
  + [Distributor ](https://docs.aws.amazon.com/systems-manager/latest/userguide/distributor.html)可協助您封裝軟體並將其發佈至 Systems Manager 受管執行個體。
  + [自動化](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-automation.html)可簡化許多 AWS 服務的常見維護、部署和修復任務。
  + [文件](https://docs.aws.amazon.com/systems-manager/latest/userguide/documents.html)會在您的組織和帳戶中對 Systems Manager 受管執行個體執行動作。
+ [AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_introduction.html) 是一種帳戶管理服務，可協助您將多個 AWS 帳戶合併到您建立和集中管理的組織。

**其他工具**
+ [Terraform](https://www.terraform.io/) 是 HashiCorp 的基礎設施即程式碼 (IaC) 工具，可協助您建立和管理雲端和內部部署資源。

**程式碼儲存庫**

此模式的說明和程式碼可在 GitHub [集中式套件分發](https://github.com/aws-samples/aws-organization-centralised-package-distribution)儲存庫中取得。

## 最佳實務
<a name="centralize-software-package-distribution-in-aws-organizations-by-using-terraform-best-practices"></a>
+ 若要將標籤指派給關聯，請使用 [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 或 [AWS Tools for PowerShell](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-welcome.html)。不支援使用 Systems Manager 主控台將標籤新增至關聯。如需詳細資訊，請參閱 [Systems Manager 文件中的標記 Systems Manager 資源](https://docs.aws.amazon.com/systems-manager/latest/userguide/tagging-resources.html)。
+ 若要使用從另一個帳戶共用的文件新版本來執行關聯，請將文件版本設定為 `default`。
+ 若要僅標記目標節點，請使用一個標籤索引鍵。如果您想要使用多個標籤索引鍵將節點設為目標，請使用資源群組選項。

## 史詩
<a name="centralize-software-package-distribution-in-aws-organizations-by-using-terraform-epics"></a>

### 設定來源檔案和帳戶
<a name="configure-source-files-and-accounts"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 複製儲存庫。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/centralize-software-package-distribution-in-aws-organizations-by-using-terraform.html) | DevOps 工程師 | 
| 更新全域變數。 | 更新 `global-customization/variables.tf` 檔案中的下列輸入參數。這些變數適用於由 AFT 建立和管理的所有帳戶。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/centralize-software-package-distribution-in-aws-organizations-by-using-terraform.html) | DevOps 工程師 | 
| 更新帳戶變數。 | 在 `account-customization/variables.tf` 檔案中更新下列輸入參數。這些變數僅適用於由 AFT 建立和管理的特定帳戶。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/centralize-software-package-distribution-in-aws-organizations-by-using-terraform.html) | DevOps 工程師 | 

### 自訂參數和部署檔案
<a name="customize-parameters-and-deployment-files"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 更新狀態管理員關聯的輸入參數。 | 更新 `account-customization/association.tf` 檔案中的下列輸入參數，以定義您想要在執行個體上維護的狀態。如果預設參數值支援您的使用案例，您可以使用這些參數值。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/centralize-software-package-distribution-in-aws-organizations-by-using-terraform.html) | DevOps 工程師 | 
| 準備壓縮檔案和套件`manifest.json`的檔案。 | 此模式提供範例 PowerShell 可安裝檔案 (Windows 為 .msi，Linux 為 .rpm)，並在 `account-customization/package` 資料夾中安裝和解除安裝指令碼。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/centralize-software-package-distribution-in-aws-organizations-by-using-terraform.html) | DevOps 工程師 | 

### 執行 Terraform 命令來佈建資源
<a name="run-terraform-commands-to-provision-resources"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 初始化 Terraform 組態。 | 若要使用 AFT 自動部署解決方案，請將程式碼推送至 AWS CodeCommit：<pre>$ git add *<br />$ git commit -m "message"<br />$ git push</pre>您也可以從 `account-customization` 資料夾執行 Terraform 命令，在不使用 AFT 的情況下部署此解決方案。若要初始化包含 Terraform 檔案的工作目錄，請執行：<pre>$ terraform init</pre> | DevOps 工程師 | 
| 預覽變更。 | 若要預覽 Terraform 對基礎設施所做的變更，請執行 命令：<pre>$ terraform plan</pre>此命令會評估 Terraform 組態，以判斷已宣告之資源的所需狀態。它也會比較所需狀態與要在工作區內佈建的實際基礎設施。 | DevOps 工程師 | 
| 套用變更。 | 執行下列命令來實作您對`variables.tf`檔案所做的變更：<pre>$ terraform apply</pre> | DevOps 工程師 | 

### 驗證資源
<a name="validate-resources"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 驗證 SSM 文件的建立。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/centralize-software-package-distribution-in-aws-organizations-by-using-terraform.html)您應該會看到 `DistributeSoftwarePackage`和 `AddSoftwarePackageToDistributor`套件。 | DevOps 工程師 | 
| 驗證自動化的成功部署。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/centralize-software-package-distribution-in-aws-organizations-by-using-terraform.html) | DevOps 工程師 | 
| 驗證部署到目標成員帳戶執行個體的套件。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/centralize-software-package-distribution-in-aws-organizations-by-using-terraform.html) | DevOps 工程師 | 

## 疑難排解
<a name="centralize-software-package-distribution-in-aws-organizations-by-using-terraform-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| 狀態管理員關聯失敗或停滯在待定狀態。 | 請參閱 AWS 知識中心的[疑難排解資訊](https://repost.aws/knowledge-center/ssm-state-manager-association-fail)。 | 
| 排程的關聯無法執行。 | 您的排程規格可能無效。State Manager 目前不支援在關聯 cron 表達式中指定月份。使用 [Cron 或 Rate 運算式](https://docs.aws.amazon.com/systems-manager/latest/userguide/reference-cron-and-rate-expressions.html)來確認排程。 | 

## 相關資源
<a name="centralize-software-package-distribution-in-aws-organizations-by-using-terraform-resources"></a>
+ [集中式套件分佈](https://github.com/aws-samples/aws-organization-centralised-package-distribution) (GitHub 儲存庫）
+ [Terraform 帳戶工廠 (AFT)](https://catalog.workshops.aws/control-tower/en-US/customization/aft)
+ [使用案例和最佳實務 ](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-best-practices.html)(AWS Systems Manager 文件）

# 使用 NLog 在 Amazon CloudWatch Logs 中設定 .NET 應用程式的記錄
<a name="configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog"></a>

*Amazon Web Services 的聖胡島和羅布希爾 (AWS)*

## 總結
<a name="configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog-summary"></a>

此模式說明如何使用 NLog 開放原始碼記錄架構，在 [Amazon CloudWatch Logs ](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/WhatIsCloudWatchLogs.html)中記錄 .NET 應用程式用量和事件。在 CloudWatch 主控台中，您可以近乎即時地檢視應用程式的日誌訊息。您也可以設定[指標](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/MonitoringLogData.html)和設定[警示](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/ConsoleAlarms.html)，以便在超過指標閾值時通知您。使用 CloudWatch Application Insights，您可以檢視自動或自訂儀表板，以顯示受監控應用程式的潛在問題。CloudWatch Application Insights 旨在協助您快速隔離應用程式和基礎設施的持續問題。

若要將日誌訊息寫入 CloudWatch Logs，請將 `AWS.Logger.NLog` NuGet 套件新增至 .NET 專案。然後，您更新 `NLog.config` 檔案以使用 CloudWatch Logs 做為目標。

## 先決條件和限制
<a name="configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ 符合下列條件的 .NET Web 或主控台應用程式：
  + 使用支援的 .NET Framework 或 .NET Core 版本。如需詳細資訊，請參閱*產品版本*。
  + 使用 NLog 將日誌資料傳送至 Application Insights。
+ 為 AWS 服務建立 IAM 角色的許可。如需詳細資訊，請參閱[服務角色許可](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-service.html#id_roles_create_service-permissions)。
+ 將角色傳遞至 AWS 服務的許可。如需詳細資訊，請參閱[授予使用者將角色傳遞至 AWS 服務](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_passrole.html)。

**產品版本**
+ .NET Framework 3.5 版或更新版本
+ .NET Core 1.0.1、2.0.0 或更新版本

## Architecture
<a name="configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog-architecture"></a>

**目標技術堆疊**
+ NLog
+ Amazon CloudWatch Logs

**目標架構**

![\[NLog 將 .NET 應用程式日誌資料寫入 Amazon ClodWatch Logs 的架構圖。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/0ac9c3ad-2a28-415f-afc3-7fe3494b2b63/images/daea9f2f-7242-4ed2-843e-655d843dcfdf.png)


1. .NET 應用程式會將日誌資料寫入 NLog 記錄架構。

1. NLog 會將日誌資料寫入 CloudWatch Logs。

1. 您可以使用 CloudWatch 警示和自訂儀表板來監控 .NET 應用程式。

## 工具
<a name="configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog-tools"></a>

**AWS 服務**
+ [Amazon CloudWatch Application Insights](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html) 可協助您觀察應用程式和基礎 AWS 資源的運作狀態。
+ [Amazon CloudWatch Logs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/WhatIsCloudWatchLogs.html) 可協助您集中所有系統、應用程式和 AWS 服務的日誌，以便您可以監控日誌並將其安全地存檔。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 可透過控制已驗證並獲授權使用的人員，協助您安全地管理對 AWS 資源的存取。
+ [AWS Tools for PowerShell](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-welcome.html) 是一組 PowerShell 模組，可協助您從 PowerShell 命令列對 AWS 資源執行指令碼操作。

**其他工具**
+ [Logger.NLog](https://www.nuget.org/packages/AWS.Logger.NLog) 是 NLog 目標，可將日誌資料記錄到 CloudWatch Logs。
+ [NLog](https://nlog-project.org/) 是 .NET 平台的開放原始碼記錄架構，可協助您將日誌資料寫入目標，例如資料庫、日誌檔案或主控台。
+ [PowerShell](https://learn.microsoft.com/en-us/powershell/) 是在 Windows、Linux 和 macOS 上執行的 Microsoft 自動化和組態管理程式。
+ [Visual Studio](https://docs.microsoft.com/en-us/visualstudio/get-started/visual-studio-ide?view=vs-2022) 是一種整合的開發環境 (IDE)，其中包含編譯器、程式碼完成工具、圖形設計師和其他支援軟體開發的功能。

## 最佳實務
<a name="configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog-best-practices"></a>
+ 設定目標日誌群組的[保留政策](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/Working-with-log-groups-and-streams.html#SettingLogRetention)。這必須在 NLog 組態之外完成。在預設情況下，日誌資料會無限期存放於 CloudWatch Logs。
+ 遵守[管理 AWS 存取金鑰的最佳實務](https://docs.aws.amazon.com/accounts/latest/reference/credentials-access-keys-best-practices.html)。

## 史詩
<a name="configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog-epics"></a>

### 設定存取和工具
<a name="set-up-access-and-tools"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 IAM 政策。 | 請遵循 IAM 文件中的[使用 JSON 編輯器建立政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create-console.html#access_policies_create-json-editor)中的指示。輸入下列 JSON 政策，該政策具有允許 CloudWatch Logs 讀取和寫入日誌所需的最低權限許可。<pre>{<br />    "Version": "2012-10-17",		 	 	 <br />    "Statement": [<br />        {<br />            "Effect": "Allow",<br />            "Action": [<br />                "logs:CreateLogGroup",<br />                "logs:CreateLogStream",<br />                "logs:GetLogEvents",<br />                "logs:PutLogEvents",<br />                "logs:DescribeLogGroups",<br />                "logs:DescribeLogStreams",<br />                "logs:PutRetentionPolicy"<br />            ],<br />            "Resource": [<br />                "*"<br />            ]<br />        }<br />    ]<br />}</pre> | AWS 管理員、AWS DevOps | 
| 建立 IAM 角色。 | 遵循 IAM 文件中[建立角色以將許可委派給 AWS 服務](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-service.html)中的指示。選取您先前建立的政策。這是 CloudWatch Logs 執行記錄動作所擔任的角色。 | AWS 管理員、AWS DevOps | 
| 設定適用於 PowerShell 的 AWS 工具。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog.html) | 一般 AWS | 

### 設定 NLog
<a name="configure-nlog"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 安裝 NuGet 套件。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog.html) | 應用程式開發人員 | 
| 設定記錄目標。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog.html)如需範例組態檔案，請參閱此模式[的其他資訊](#configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog-additional)一節。當您執行應用程式時，NLog 會撰寫日誌訊息，並將其傳送至 CloudWatch Logs。 | 應用程式開發人員 | 

### 驗證和監控日誌
<a name="validate-and-monitor-logs"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 驗證記錄。 | 遵循 [ CloudWatch Logs 文件中檢視傳送至 CloudWatch Logs 的日誌資料](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/Working-with-log-groups-and-streams.html#ViewingLogData)中的指示。 CloudWatch 驗證日誌事件是否正在為 .NET 應用程式記錄。如果未記錄日誌事件，請參閱此模式中的[故障診斷](#configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog-troubleshooting)一節。 | 一般 AWS | 
| 監控 .NET 應用程式堆疊。 | 根據您的使用案例，視需要在 CloudWatch 中設定監控。您可以使用 [CloudWatch Logs Insights](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AnalyzingLogData.html)、[CloudWatch Metrics Insights](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/query_with_cloudwatch-metrics-insights.html) 和 [CloudWatch Application Insights](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html) 來監控 .NET 工作負載。您也可以設定[警示](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html)，以便接收警示，也可以建立自訂[儀表板](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)，從單一檢視監控工作負載。 | 一般 AWS | 

## 疑難排解
<a name="configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| 日誌資料不會顯示在 CloudWatch Logs 中。 | 請確定 IAM 政策已連接至 CloudWatch Logs 擔任的 IAM 角色。如需說明，請參閱《[Epics](#configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog-epics)》中的*設定存取和工具*一節。 | 

## 相關資源
<a name="configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog-resources"></a>
+ [使用日誌群組和日誌串流 ](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/Working-with-log-groups-and-streams.html)(CloudWatch Logs 文件）
+ [Amazon CloudWatch Logs 和 .NET Logging Framework ](https://aws.amazon.com/blogs/developer/amazon-cloudwatch-logs-and-net-logging-frameworks/)(AWS 部落格文章）

## 其他資訊
<a name="configure-logging-for-net-applications-in-amazon-cloudwatch-logs-by-using-nlog-additional"></a>

以下是範例`NLog.config`檔案。

```
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
  </startup>
  <nlog>
    <extensions>
      <add assembly="NLog.AWS.Logger" />
    </extensions>
    <targets>
      <target name="aws" type="AWSTarget" logGroup="NLog.TestGroup" region="us-east-1" profile="demo"/>
    </targets>
    <rules>
      <logger name="*" minlevel="Info" writeTo="aws" />
    </rules>    
  </nlog>
</configuration>
```

# 將 AWS Service Catalog 產品複製到不同的 AWS 帳戶和 AWS 區域
<a name="copy-aws-service-catalog-products-across-different-aws-accounts-and-aws-regions"></a>

*Sachin Vighe 和 Santosh Kale，Amazon Web Services*

## 總結
<a name="copy-aws-service-catalog-products-across-different-aws-accounts-and-aws-regions-summary"></a>

AWS Service Catalog 是一項區域服務，這表示 AWS Service Catalog [產品組合和產品](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/what-is_concepts.html)只能在建立它們的 AWS 區域中顯示。如果您在新區域中設定 [AWS Service Catalog 中樞](https://aws.amazon.com/about-aws/whats-new/2020/06/aws-service-catalog-now-supports-sharing-portfolios-across-an-organization-from-a-delegated-member-account/)，則必須重新建立現有的產品，這可能會是耗時的程序。

此模式的方法透過描述如何將來源 AWS 帳戶或區域中的 AWS Service Catalog 中樞中的產品複製到目的地帳戶或區域中的新中樞，協助簡化此程序。如需 AWS Service Catalog 中樞和語音模型的詳細資訊，請參閱 [AWS Service Catalog 中樞和語音模型：如何將 AWS Service Catalog 的部署和管理自動化至 AWS 管理和控管部落格上的許多帳戶](https://aws.amazon.com/blogs/mt/aws-service-catalog-hub-and-spoke-model-how-to-automate-the-deployment-and-management-of-service-catalog-to-many-accounts/)。 

模式也提供跨帳戶或其他區域複製 AWS Service Catalog 產品所需的個別程式碼套件。透過使用此模式，您的組織可以節省時間、在新的 AWS Service Catalog 中樞中提供現有和先前的產品版本、將手動錯誤的風險降至最低，以及將方法擴展到多個帳戶或區域。

**注意**  
此模式的 *Epics* 區段提供兩種複製 產品的選項。您可以使用選項 1 跨帳戶複製產品，或選擇選項 2 跨區域複製產品。

## 先決條件和限制
<a name="copy-aws-service-catalog-products-across-different-aws-accounts-and-aws-regions-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ 來源帳戶或區域中現有的 AWS Service Catalog 產品。
+ 目的地帳戶或區域中現有的 AWS Service Catalog 中樞。
+ 如果您想要跨帳戶複製產品，您必須共用，然後將包含產品的 AWS Service Catalog 產品組合匯入目的地帳戶。如需詳細資訊，請參閱 AWS Service Catalog 文件中的[共用和匯入產品組合](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/catalogs_portfolios_sharing.html)。

**限制**
+ 您要跨區域或帳戶複製的 AWS Service Catalog 產品不能屬於多個產品組合。

## Architecture
<a name="copy-aws-service-catalog-products-across-different-aws-accounts-and-aws-regions-architecture"></a>

下圖顯示將 AWS Service Catalog 產品從來源帳戶複製到目的地帳戶。

![\[區域 1 中的跨帳戶角色、區域 2 中的 Lambda 執行角色和 Lambda 函數。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/7ede5d17-89eb-4455-928f-6953d145ac9f/images/26738220-1ed2-4f84-911b-3c88e954b60e.png)


 下圖顯示將 AWS Service Catalog 產品從來源區域複製到目的地區域。

![\[使用區域 2 中的 Lambda scProductCopy 函數複製的產品。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/7ede5d17-89eb-4455-928f-6953d145ac9f/images/0a936792-3bdc-45c2-ba05-17e828615061.png)


**技術堆疊**
+ Amazon CloudWatch
+ AWS Identity and Access Management (IAM)
+ AWS Lambda
+ AWS Service Catalog

**自動化和擴展**

您可以使用 Lambda 函數來擴展此模式的方法，該函數可根據收到的請求數量或您需要複製的 AWS Service Catalog 產品數量進行擴展。如需詳細資訊，請參閱 AWS Lambda [Lambda 文件中的 Lambda 函數擴展](https://docs.aws.amazon.com/lambda/latest/dg/invocation-scaling.html)。

## 工具
<a name="copy-aws-service-catalog-products-across-different-aws-accounts-and-aws-regions-tools"></a>
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 是一種開放原始碼工具，可協助您透過命令列 shell 中的命令與 AWS 服務互動。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 可透過控制已驗證並獲授權使用的人員，協助您安全地管理對 AWS 資源的存取。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一種運算服務，可協助您執行程式碼，而不需要佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。
+ [AWS Service Catalog](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/introduction.html) 可協助您集中管理針對 AWS 核准的 IT 服務目錄。最終使用者可在機構所設的限制範圍內，迅速地只部署自己需要且經核准的 IT 服務。

**Code**

您可以使用 ` cross-account-copy`套件 （已連接） 跨帳戶複製 AWS Service Catalog 產品，或使用 `cross-region-copy`套件 （已連接） 跨區域複製產品。

`cross-account-copy` 套件包含下列檔案：
+ `copyconf.properties` – 包含區域和 AWS 帳戶 ID 參數的組態檔案，用於跨帳戶複製產品。
+ `scProductCopyLambda.py` – 用於跨帳戶複製產品的 Python 函數。
+ `createDestAccountRole.sh` – 在目的地帳戶中建立 IAM 角色的指令碼。
+ `createSrcAccountRole.sh` – 在來源帳戶中建立 IAM 角色的指令碼。
+ `copyProduct.sh` – 用來建立和叫用 Lambda 函數以跨帳戶複製產品的指令碼。

`cross-region-copy` 套件包含下列檔案：
+ `copyconf.properties` – 包含區域和 AWS 帳戶 ID 參數的組態檔案，用於跨區域複製產品。
+ `scProductCopyLambda.py` – 跨區域複製產品的 Python 函數。
+ `copyProduct.sh` – 用來建立 IAM 角色，以及建立和叫用 Lambda 函數以跨區域複製產品的指令碼。

## 史詩
<a name="copy-aws-service-catalog-products-across-different-aws-accounts-and-aws-regions-epics"></a>

### 選項 1 – 跨帳戶複製 AWS Service Catalog 產品
<a name="option-1-ndash-copy-aws-service-catalog-products-across-accounts"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 更新組態檔案。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-aws-service-catalog-products-across-different-aws-accounts-and-aws-regions.html) | AWS 管理員、AWS 系統管理員、雲端管理員 | 
| 在目的地帳戶中設定 AWS CLI 的登入資料。 | 執行 `aws configure`命令並提供下列值，以設定您的登入資料來存取目的地帳戶中的 AWS CLI：<pre>$aws configure <br />AWS Access Key ID [None]: <your_access_key_id> <br />AWS Secret Access Key [None]: <your_secret_access_key> <br />Default region name [None]: Region<br />Default output format [None]:</pre>如需詳細資訊，請參閱 AWS Command Line Interface 文件中的[組態基本概念](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html)。  | AWS 管理員、AWS 系統管理員、雲端管理員 | 
| 在來源帳戶中設定 AWS CLI 的登入資料。 | 執行 `aws configure`命令並提供下列值，以設定您的登入資料來存取來源帳戶中的 AWS CLI： <pre>$aws configure<br />AWS Access Key ID [None]: <your_access_key_id><br />AWS Secret Access Key [None]: <your_secret_access_key><br />Default region name [None]: Region<br />Default output format [None]:</pre>如需詳細資訊，請參閱 AWS 命令列界面文件中[的組態基本](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html)概念。  | AWS 管理員、AWS 系統管理員、雲端管理員 | 
| 在目的地帳戶中建立 Lambda 執行角色。 | 在您的目的地帳戶中執行`createDestAccountRole.sh `指令碼。指令碼會實作下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-aws-service-catalog-products-across-different-aws-accounts-and-aws-regions.html) | AWS 管理員、AWS 系統管理員、雲端管理員 | 
| 在來源帳戶中建立跨帳戶 IAM 角色。 | 在您的來源帳戶中執行`createSrcAccountRole.sh `指令碼。指令碼會實作下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-aws-service-catalog-products-across-different-aws-accounts-and-aws-regions.html) | AWS 管理員、AWS 系統管理員、雲端管理員 | 
| 在目的地帳戶中執行 copyProduct 指令碼。 | 在目的地帳戶中執行`copyProduct.sh `指令碼。指令碼會實作下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-aws-service-catalog-products-across-different-aws-accounts-and-aws-regions.html) | AWS 管理員、AWS 系統管理員、雲端管理員 | 

### 選項 2 – 將 AWS Service Catalog 產品從來源區域複製到目的地區域
<a name="option-2-ndash-copy-aws-service-catalog-products-from-a-source-region-to-a-destination-region"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 更新組態檔案。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-aws-service-catalog-products-across-different-aws-accounts-and-aws-regions.html) | AWS 系統管理員、雲端管理員、AWS 管理員 | 
| 設定 AWS CLI 的登入資料。 | 執行 `aws configure`命令並提供下列值，以設定您的登入資料來存取您環境中的 AWS CLI：<pre>$aws configure<br />AWS Access Key ID [None]: <your_access_key_id><br />AWS Secret Access Key [None]: <your_secret_access_key><br />Default region name [None]: Region<br />Default output format [None]:</pre>如需詳細資訊，請參閱 AWS 命令列界面文件中[的組態基本](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html)概念。  | AWS 管理員、AWS 系統管理員、雲端管理員 | 
| 執行 copyProduct 指令碼。 | 在目的地區域中執行`copyProduct.sh`指令碼。指令碼會實作下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-aws-service-catalog-products-across-different-aws-accounts-and-aws-regions.html) | AWS 管理員、AWS 系統管理員、雲端管理員 | 

## 相關資源
<a name="copy-aws-service-catalog-products-across-different-aws-accounts-and-aws-regions-resources"></a>
+ [建立 Lambda 執行角色](https://docs.aws.amazon.com/lambda/latest/dg/lambda-intro-execution-role.html) (AWS Lambda 文件）
+ [建立 Lambda 函數](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-awscli.html) (AWS Lambda 文件）
+ [AWS Service Catalog API 參考](https://docs.aws.amazon.com/servicecatalog/latest/dg/API_Operations_AWS_Service_Catalog.html)
+ [AWS Service Catalog 文件](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/what-is_concepts.html)

## 附件
<a name="attachments-7ede5d17-89eb-4455-928f-6953d145ac9f"></a>

若要存取與本文件相關聯的其他內容，請解壓縮下列檔案： [attachment.zip](samples/p-attach/7ede5d17-89eb-4455-928f-6953d145ac9f/attachments/attachment.zip)

# 為雲端操作模型建立 RACI 或 RASCI 矩陣
<a name="create-a-raci-or-rasci-matrix-for-a-cloud-operating-model"></a>

*Teddy Germade、Jerome Descreux、Florian Leroux 和 Josselin LE MINEUR，Amazon Web Services*

## 總結
<a name="create-a-raci-or-rasci-matrix-for-a-cloud-operating-model-summary"></a>

Cloud Center of Excellence (CCoE) 或 CEE (Cloud Enablement Engine) 是一個強大且負責任的團隊，專注於為雲端做好營運準備。其主要重點是將資訊 IT 組織從內部部署操作模型轉換為雲端操作模型。CCoE 應該是一個跨職能團隊，其中包含來自基礎設施、應用程式、操作和安全性的表示。

雲端操作模型的其中一個關鍵元件是 *RACI 矩陣*或 *RASCI 矩陣*。這用於定義涉及遷移活動和雲端操作的所有各方的角色和責任。矩陣名稱衍生自矩陣中定義的責任類型：負責人 (R)、責任 (A)、支援 (S)、諮詢 (C) 和知情 (I)。支援類型為選用。如果您包含它，則稱為 *RASCI 矩陣*，如果您排除它，則稱為 *RACI 矩陣*。

從連接的範本開始，您的 CCoE 團隊可以為您的組織建立 RACI 或 RASCI 矩陣。範本包含雲端操作模型中常見的團隊、角色和任務。此矩陣的基礎是與操作整合和 CCoE 功能相關的任務。不過，您可以自訂此範本，以滿足組織結構和使用案例的需求。

RACI 矩陣的實作沒有限制。此方法適用於大型組織、新創公司及其間的所有項目。對於小型組織，相同的資源可以填滿多個角色。

## 史詩
<a name="create-a-raci-or-rasci-matrix-for-a-cloud-operating-model-epics"></a>

### 建立矩陣
<a name="create-the-matrix"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 識別關鍵利益相關者。 | 識別與您雲端營運模型策略目標連結的關鍵服務和團隊經理。 | 專案經理 | 
| 自訂矩陣範本。 | 在[附件](#attachments-b3df3d2c-c596-4736-bbaa-8edbcf335352)區段中下載範本，然後更新 RACI 或 RASCI 矩陣，如下所示：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-a-raci-or-rasci-matrix-for-a-cloud-operating-model.html) | 專案經理 | 
| 規劃會議。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-a-raci-or-rasci-matrix-for-a-cloud-operating-model.html) | 專案經理 | 
| 完成矩陣。 | 在與所有利益相關者的會議中，執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-a-raci-or-rasci-matrix-for-a-cloud-operating-model.html) | 專案經理 | 
| 共用 RASCI 矩陣。 | 當 RACI 或 RASCI 矩陣完成時，讓領導層核准。將它儲存在共用儲存庫或所有利益相關者都可以存取的中央位置。我們建議您使用標準文件控制程序來記錄和核准矩陣的修訂。 | 專案經理 | 

## 相關資源
<a name="create-a-raci-or-rasci-matrix-for-a-cloud-operating-model-resources"></a>
+ [AWS 共同責任模型](https://aws.amazon.com/compliance/shared-responsibility-model/)

## 附件
<a name="attachments-b3df3d2c-c596-4736-bbaa-8edbcf335352"></a>

若要存取與本文件相關聯的其他內容，請解壓縮下列檔案： [attachment.zip](samples/p-attach/b3df3d2c-c596-4736-bbaa-8edbcf335352/attachments/attachment.zip)

# 使用 Amazon CloudWatch 異常偵測為自訂指標建立警示
<a name="create-alarms-for-custom-metrics-using-amazon-cloudwatch-anomaly-detection"></a>

*Ram Kandaswamy 和 Raheem Jiwani，Amazon Web Services*

## 總結
<a name="create-alarms-for-custom-metrics-using-amazon-cloudwatch-anomaly-detection-summary"></a>

在 Amazon Web Services (AWS) 雲端上，您可以使用 Amazon CloudWatch 建立警示，以監控指標並傳送通知，或在超過閾值時自動進行變更。

若要避免受到[靜態閾值](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/ConsoleAlarms.html)的限制，您可以根據過去的模式建立警示，並在特定指標超出正常操作時段時通知您。例如，您可以從 Amazon API Gateway 監控 API 的回應時間，並接收有關無法滿足服務層級協議 (SLA) 的異常通知。

此模式說明如何針對自訂指標使用 CloudWatch 異常偵測。模式說明如何在 Amazon CloudWatch Logs Insights 中建立自訂指標，或使用 AWS Lambda 函數發佈自訂指標，然後使用 Amazon Simple Notification Service (Amazon SNS) 設定異常偵測和建立通知。

## 先決條件和限制
<a name="create-alarms-for-custom-metrics-using-amazon-cloudwatch-anomaly-detection-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ 現有的 SNS 主題，設定為傳送電子郵件通知。如需詳細資訊，請參閱[《Amazon SNS 文件](https://docs.aws.amazon.com/sns/latest/dg/sns-getting-started.html)》中的 Amazon SNS 入門。
+ 使用 [CloudWatch Logs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_GettingStarted.html) 設定的現有應用程式。

**限制**
+ CloudWatch 指標不支援毫秒時間間隔。如需一般和自訂指標精細程度的詳細資訊，請參閱 [Amazon CloudWatch FAQs](https://aws.amazon.com/cloudwatch/faqs/)。

## Architecture
<a name="create-alarms-for-custom-metrics-using-amazon-cloudwatch-anomaly-detection-architecture"></a>

![\[CloudWatch 使用 Amazon SNS 主題，在警示啟動時傳送電子郵件通知。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/d47e6f7f-e469-4cb9-b34b-8c4b78d71820/images/49f30340-9552-430a-893a-d0608bb09e38.png)


 該圖顯示以下工作流程：

1. 使用 CloudWatch Logs 建立和更新的指標的日誌會串流至 CloudWatch。

1. 警示會根據閾值啟動，並將警示傳送至 SNS 主題。

1. Amazon SNS 會傳送電子郵件通知給您。

**技術堆疊**
+ CloudWatch
+ AWS Lambda
+ Amazon SNS

## 工具
<a name="create-alarms-for-custom-metrics-using-amazon-cloudwatch-anomaly-detection-tools"></a>
+ [Amazon CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html) 提供可靠、可擴展且靈活的監控解決方案。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一種運算服務，可協助您執行程式碼，而無需佈建或管理伺服器。
+ [Amazon Simple Notification Service (Amazon SNS)](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) 是一種受管服務，可將訊息從發佈者交付給訂閱者。

## 史詩
<a name="create-alarms-for-custom-metrics-using-amazon-cloudwatch-anomaly-detection-epics"></a>

### 設定自訂指標的異常偵測
<a name="set-up-anomaly-detection-for-a-custom-metric"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 選項 1 - 使用 Lambda 函數建立自訂指標。 | 下載 `lambda_function.py` 檔案 （已連接），然後取代 AWS 文件 GitHub 上 [aws-lambda-developer-guide](https://github.com/awsdocs/aws-lambda-developer-guide/tree/main/sample-apps/blank-python/function) 儲存庫中的範例`lambda_function.py`檔案。這為您提供了將自訂指標傳送至 CloudWatch Logs 的範例 Lambda 函數。Lambda 函數使用 Boto3 API 與 CloudWatch 整合。 執行 Lambda 函數之後，您可以登入 AWS 管理主控台、開啟 CloudWatch 主控台，而且已發佈的指標可在已發佈的命名空間下使用。 | DevOps 工程師，AWS DevOps | 
| 選項 2 – 從 CloudWatch 日誌群組建立自訂指標。 | 登入 AWS 管理主控台，開啟 CloudWatch 主控台，然後選擇**日誌群組**。選擇您要為其建立指標的日誌群組。 選擇**動作**，然後選擇**建立指標篩選條件**。針對**篩選條件模式**，輸入您要使用的篩選條件模式。如需詳細資訊，請參閱 CloudWatch 文件中的[篩選和模式語法](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html)。 若要測試篩選條件模式，請在**測試模式**下輸入一或多個日誌事件。每個日誌事件都必須在一行內，因為 **Log event messages** (日誌事件訊息) 方塊中使用換行來分隔日誌事件。測試模式之後，您可以在指標**詳細資訊**下輸入指標的名稱和值。 如需建立自訂指標的詳細資訊和步驟，請參閱 CloudWatch 文件中的[為日誌群組建立指標篩選條件](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CreateMetricFilterProcedure.html)。 | DevOps 工程師，AWS DevOps | 
| 為您的自訂指標建立警示。 | 在 CloudWatch 主控台上，選擇**警示**，然後選擇**建立警示**。選擇**選取指標**，然後在搜尋方塊中輸入您先前建立的指標名稱。選擇**圖形化指標**索引標籤，並根據您的需求設定選項。在**條件**下，選擇**異常偵測**，而非**靜態閾值**。這會顯示以兩個標準預設偏差為基礎的頻帶。您可以設定閾值，並根據需求調整閾值。選擇**下一步**。頻帶是動態的，取決於資料點的品質。當您開始彙總更多資料時，頻帶和閾值會自動更新。  | DevOps 工程師，AWS DevOps | 
| 設定 SNS 通知。 | 在**通知**下，選擇要在警示處於`ALARM`狀態、`OK`狀態或`INSUFFICIENT_DATA`狀態時通知的 SNS 主題。若要讓警示針對相同的警示狀態或不同警示狀態傳送多個通知，請選擇 **Add notification (新增通知)**。選擇**下一步**。輸入警示的名稱與說明。名稱只能包含 ASCII 字元。然後選擇**下一步**。在**預覽和建立**下，確認資訊和條件正確無誤，然後選擇**建立警示**。 | DevOps 工程師，AWS DevOps | 

## 相關資源
<a name="create-alarms-for-custom-metrics-using-amazon-cloudwatch-anomaly-detection-resources"></a>
+ [將自訂指標發佈至 CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/publishingMetrics.html)
+ [使用 CloudWatch 異常偵測](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Anomaly_Detection.html)
+ [警示事件和 Amazon EventBridge](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-and-eventbridge.html)
+ [將自訂指標推送至 Cloud Watch 時，應遵循哪些最佳實務？](https://www.youtube.com/watch?v=mVffHIzIL60)（影片）
+ [CloudWatch Application Insights 簡介](https://www.youtube.com/watch?v=PBO636_t9n0) （影片）
+ [使用 CloudWatch（影片） 偵測異常](https://www.youtube.com/watch?v=8umIX-pUy3k)

## 附件
<a name="attachments-d47e6f7f-e469-4cb9-b34b-8c4b78d71820"></a>

若要存取與本文件相關聯的其他內容，請解壓縮下列檔案： [attachment.zip](samples/p-attach/d47e6f7f-e469-4cb9-b34b-8c4b78d71820/attachments/attachment.zip)

# 建立使用具有預設加密之 Amazon EBS 磁碟區的 AWS Cloud9 IDE
<a name="create-an-aws-cloud9-ide-that-uses-amazon-ebs-volumes-with-default-encryption"></a>

*Janardhan Malyala 和 Dhrubajyoti Mukherjee，Amazon Web Services*

## 總結
<a name="create-an-aws-cloud9-ide-that-uses-amazon-ebs-volumes-with-default-encryption-summary"></a>

**注意**： AWS Cloud9 不再提供給新客戶。的現有客戶 AWS Cloud9 可以繼續正常使用服務。[進一步了解](https://aws.amazon.com/blogs/devops/how-to-migrate-from-aws-cloud9-to-aws-ide-toolkits-or-aws-cloudshell/)

您可以在 Amazon Web Services (AWS) 雲端上使用[預設加密](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html#encryption-by-default)，強制加密 Amazon Elastic Block Store (Amazon EBS) 磁碟區和快照副本。 

您可以建立使用預設加密之 EBS 磁碟區的 AWS Cloud9 整合開發環境 (IDE)。不過，AWS Cloud9 的 AWS Identity and Access Management (IAM) [服務連結角色](https://docs.aws.amazon.com/cloud9/latest/user-guide/using-service-linked-roles.html)需要存取這些 EBS 磁碟區的 AWS Key Management Service (AWS KMS) 金鑰。 AWS Cloud9 如果未提供存取權，AWS Cloud9 IDE 可能無法啟動，且偵錯可能很困難。 

此模式提供將 AWS Cloud9 的服務連結角色新增至 EBS 磁碟區所使用的 AWS KMS 金鑰的步驟。此模式描述的設定可協助您成功建立和啟動 IDE，該 IDE 預設使用具有加密功能的 EBS 磁碟區。

## 先決條件和限制
<a name="create-an-aws-cloud9-ide-that-uses-amazon-ebs-volumes-with-default-encryption-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ EBS 磁碟區的預設加密已開啟。如需預設加密的詳細資訊，請參閱《[Amazon Elastic Compute Cloud (Amazon EC2) 文件》中的 Amazon EBS 加密](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html)。Amazon EC2
+ 用於加密 EBS 磁碟區的現有[客戶受管 KMS 金鑰](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#customer-cmk)。

**注意**  
您不需要為 AWS Cloud9 建立服務連結角色。當您建立 AWS Cloud9 開發環境時，AWS Cloud9 會為您建立服務連結角色。

## Architecture
<a name="create-an-aws-cloud9-ide-that-uses-amazon-ebs-volumes-with-default-encryption-architecture"></a>

![\[使用 AWS Cloud9 IDE 強制加密 EBS 磁碟區和快照。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/dd98fbb4-0949-4299-b701-bc857e13049c/images/6b22b8d1-75d9-4f06-b5d6-5fff7397f22d.png)


**技術堆疊**
+ AWS Cloud9
+ IAM
+ AWS KMS

## 工具
<a name="create-an-aws-cloud9-ide-that-uses-amazon-ebs-volumes-with-default-encryption-tools"></a>
+ [AWS Cloud9](https://docs.aws.amazon.com/cloud9/latest/user-guide/welcome.html) 是整合式開發環境 (IDE)，可協助您編寫、建置、執行、測試和偵錯軟體。它還可協助您將軟體發佈至 AWS 雲端。
+ [Amazon Elastic Block Store (Amazon EBS)](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AmazonEBS.html) 提供區塊層級儲存磁碟區，可與 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體搭配使用。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 可透過控制已驗證並獲授權使用的人員，協助您安全地管理對 AWS 資源的存取。
+ [AWS Key Management Service (AWS KMS)](https://docs.aws.amazon.com/kms/latest/developerguide/overview.html) 可協助您建立和控制密碼編譯金鑰，以協助保護您的資料。

## 史詩
<a name="create-an-aws-cloud9-ide-that-uses-amazon-ebs-volumes-with-default-encryption-epics"></a>

### 尋找預設加密金鑰值
<a name="find-the-default-encryption-key-value"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 記錄 EBS 磁碟區的預設加密金鑰值。 | 登入 AWS 管理主控台並開啟 Amazon EC2 主控台。選擇 **EC2 儀表板**，然後在**帳戶屬性**中選擇**資料保護和安全性**。在 **EBS 加密**區段中，複製並記錄**預設加密金鑰**中的值。 | 雲端架構師、DevOps 工程師 | 

### 提供 AWS KMS 金鑰的存取權
<a name="provide-access-to-the-aws-kms-key"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 提供 AWS Cloud9 存取 EBS 磁碟區的 KMS 金鑰。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-an-aws-cloud9-ide-that-uses-amazon-ebs-volumes-with-default-encryption.html)如需更新金鑰政策的詳細資訊，請參閱[如何變更金鑰政策](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-modifying.html#key-policy-modifying-how-to) (AWS KMS 文件）。當您啟動第一個 IDE 時，會自動建立 AWS Cloud9 的服務連結角色。如需詳細資訊，請參閱 AWS Cloud9 文件中的[建立服務連結角色](https://docs.aws.amazon.com/cloud9/latest/user-guide/using-service-linked-roles.html#create-service-linked-role)。  | 雲端架構師、DevOps 工程師 | 

### 建立和啟動 IDE
<a name="create-and-launch-the-ide"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立並啟動 AWS Cloud9 IDE。 | 開啟 AWS Cloud9 主控台，並依照 AWS Cloud9 文件中建立 [ EC2 環境](https://docs.aws.amazon.com/cloud9/latest/user-guide/create-environment-main.html)的步驟，根據您的需求選擇建立 AWS Cloud9********Configure IDE。  | 雲端架構師、DevOps 工程師 | 

## 相關資源
<a name="create-an-aws-cloud9-ide-that-uses-amazon-ebs-volumes-with-default-encryption-resources"></a>
+ [加密 AWS Cloud9 使用的 EBS 磁碟區](https://docs.aws.amazon.com/cloud9/latest/user-guide/move-environment.html#encrypting-volumes)
+ [為 AWS Cloud9 建立服務連結角色](https://docs.aws.amazon.com/cloud9/latest/user-guide/using-service-linked-roles.html#create-service-linked-role)
+ [在 AWS Cloud9 中建立 EC2 環境](https://docs.aws.amazon.com/cloud9/latest/user-guide/create-environment-main.html)

## 其他資訊
<a name="create-an-aws-cloud9-ide-that-uses-amazon-ebs-volumes-with-default-encryption-additional"></a>

**AWS KMS 金鑰政策更新**

使用您的 AWS 帳戶 ID 取代 `<aws_accountid>`。

```
{
            "Sid": "Allow use of the key",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::<aws_accountid>:role/aws-service-role/cloud9.amazonaws.com/AWSServiceRoleForAWSCloud9"
            },
            "Action": [
                "kms:Encrypt",
                "kms:Decrypt",
                "kms:ReEncrypt*",
                "kms:GenerateDataKey*",
                "kms:DescribeKey"
            ],
            "Resource": "*"
        },
        {
            "Sid": "Allow attachment of persistent resources",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::<aws_accountid>:role/aws-service-role/cloud9.amazonaws.com/AWSServiceRoleForAWSCloud9"
            },
            "Action": [
                "kms:CreateGrant",
                "kms:ListGrants",
                "kms:RevokeGrant"
            ],
            "Resource": "*",
            "Condition": {
                "Bool": {
                    "kms:GrantIsForAWSResource": "true"
                }
            }
        }
```

**使用跨帳戶金鑰**

如果您想要使用跨帳戶 KMS 金鑰，您必須使用授權搭配 KMS 金鑰政策。這可讓跨帳戶存取 金鑰。在您用來建立 Cloud9 環境的相同帳戶中，在終端機中執行下列命令。

```
aws kms create-grant \
 --region <Region where Cloud9 environment is created> \
 --key-id <The cross-account KMS key ARN> \
 --grantee-principal arn:aws:iam::<The account where Cloud9 environment is created>:role/aws-service-role/cloud9.amazonaws.com/AWSServiceRoleForAWSCloud9 \
 --operations "Encrypt" "Decrypt" "ReEncryptFrom" "ReEncryptTo" "GenerateDataKey" "GenerateDataKeyWithoutPlaintext" "DescribeKey" "CreateGrant"
```

執行此命令後，您可以使用 EBS 加密搭配不同帳戶中的金鑰來建立 Cloud9 環境。

# 自動建立標籤型 Amazon CloudWatch 儀表板
<a name="create-tag-based-amazon-cloudwatch-dashboards-automatically"></a>

*Janak Vadaria、Vinodkumar Mandalapu 和 RAJNEESH TYAGI，Amazon Web Services*

## 總結
<a name="create-tag-based-amazon-cloudwatch-dashboards-automatically-summary"></a>

手動建立不同的 Amazon CloudWatch 儀表板可能很耗時，尤其是當您必須建立和更新多個資源以自動擴展環境時。自動建立和更新 CloudWatch 儀表板的解決方案可以節省您的時間。此模式可協助您部署全自動化 AWS Cloud Development Kit (AWS CDK) 管道，以根據標籤變更事件建立和更新 AWS 資源的 CloudWatch 儀表板，以顯示 Golden Signals 指標。

在網站可靠性工程 (SRE) 中，黃金訊號是指一組全面的指標，可從使用者或消費者的角度提供服務的廣泛檢視。這些指標包含延遲、流量、錯誤和飽和。如需詳細資訊，請參閱 AWS 網站上的[什麼是網站可靠性工程 (SRE)？](https://aws.amazon.com/what-is/sre/)。

此模式提供的解決方案是事件驅動的。部署後，它會持續監控標籤變更事件，並自動更新 CloudWatch 儀表板和警示。

## 先決條件和限制
<a name="create-tag-based-amazon-cloudwatch-dashboards-automatically-prereqs"></a>

** 先決條件 **
+ 作用中 AWS 帳戶
+ AWS Command Line Interface (AWS CLI)，[已安裝並設定](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
+  AWS CDK v2 [的先決條件](https://docs.aws.amazon.com/cdk/v2/guide/work-with.html#work-with-prerequisites) 
+ 上的[引導環境](https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html) AWS
+ [Python 第 3 版](https://www.python.org/downloads/)
+ [AWS 適用於 Python 的 SDK (Boto3)](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html)，已安裝
+ [Node.js 第 18 版](https://nodejs.org/en/download/current)或更新版本
+ 節點套件管理員 (npm)，[已安裝並針對 設定](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) AWS CDK
+ 中等 （層級 200) 熟悉 AWS CDK 和 AWS CodePipeline

**限制**

此解決方案目前僅針對下列 AWS 服務建立自動化儀表板：
+ [Amazon Relational Database Service (Amazon RDS)](https://aws.amazon.com/rds/)
+ [AWS Auto Scaling](https://aws.amazon.com/autoscaling/)
+ [Amazon Simple Notification Service (Amazon SNS)](https://aws.amazon.com/sns/)
+ [Amazon DynamoDB](https://aws.amazon.com/dynamodb/)
+ [AWS Lambda](https://aws.amazon.com/lambda/)

## Architecture
<a name="create-tag-based-amazon-cloudwatch-dashboards-automatically-architecture"></a>

**目標技術堆疊**
+ [CloudWatch 儀表板](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)
+ [CloudWatch 警示](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html)

**目標架構**

![\[建立標籤型 CloudWatch 儀表板的目標架構\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/f234fe30-87db-446f-a291-d33928ca2ccb/images/f63ca697-f252-416d-8a1b-0239f38c10c5.png)


1. 已設定應用程式標籤或程式碼變更的 AWS 標籤變更事件會在 中啟動管道， AWS CodePipeline 以建置和部署更新的 CloudWatch 儀表板。

1. AWS CodeBuild 執行 Python 指令碼來尋找已設定標籤的資源，並將資源 IDs存放在 CodeBuild 環境中的本機檔案中。

1. CodeBuild 會執行 **cdk 合成**來產生部署 CloudWatch 儀表板和警示的 CloudFormation 範本。

1. CodePipeline 會將 CloudFormation 範本部署到指定的 AWS 帳戶 和 區域。

1. 堆疊成功 CloudFormation 部署後，您可以檢視 CloudWatch 儀表板和警示。

**自動化和擴展**

此解決方案已使用 自動化 AWS CDK。您可以在 Amazon CloudWatch 儲存庫上的 GitHub Golden Signals Dashboards 中找到程式碼。 [ Amazon CloudWatch](https://github.com/aws-samples/golden-signals-dashboards-sample-app) 對於其他擴展和建立自訂儀表板，您可以設定多個標籤索引鍵和值。

## 工具
<a name="create-tag-based-amazon-cloudwatch-dashboards-automatically-tools"></a>

**Amazon 服務**
+ [Amazon EventBridge](https://aws.amazon.com/eventbridge/) 是一種無伺服器事件匯流排服務，可協助您將應用程式與各種來源的即時資料連線，包括 AWS Lambda 函數、使用 API 目的地的 HTTP 呼叫端點，或其他事件匯流排 AWS 帳戶。
+ [AWS CodePipeline](https://aws.amazon.com/codepipeline/) 可協助您快速建模和設定軟體版本的不同階段，並自動化持續發行軟體變更所需的步驟。
+ [AWS CodeBuild](https://aws.amazon.com/codebuild/) 是一種全受管建置服務，可協助您編譯原始程式碼、執行單元測試，並產生準備好部署的成品。
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 是一種開放原始碼工具，可協助您透過命令列 shell 中的命令與 AWS 服務互動。
+ [AWS Identity and Access Management (IAM)](https://aws.amazon.com/iam/) 透過控制已驗證並獲授權使用的人員，協助您安全地管理對 AWS 資源的存取。
+ [Amazon Simple Storage Service (Amazon S3)](https://aws.amazon.com/s3/) 是一種雲端型物件儲存服務，可協助您儲存、保護和擷取任何數量的資料。

## 最佳實務
<a name="create-tag-based-amazon-cloudwatch-dashboards-automatically-best-practices"></a>

作為安全最佳實務，您可以對連接到管道的來源儲存庫使用加密和身分驗證。如需其他最佳實務，請參閱 [CodePipeline 文件中的 CodePipeline 最佳實務和使用案例](https://docs.aws.amazon.com/codepipeline/latest/userguide/best-practices.html)。 CodePipeline 

## 史詩
<a name="create-tag-based-amazon-cloudwatch-dashboards-automatically-epics"></a>

### 設定和部署範例應用程式
<a name="configure-and-deploy-the-sample-application"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定和部署範例應用程式。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-tag-based-amazon-cloudwatch-dashboards-automatically.html) | AWS DevOps | 
| 自動建立儀表板和警示。 | 部署範例應用程式後，您可以使用預期的標籤值來建立此解決方案支援的任何資源，這會自動建立指定的儀表板和警示。若要測試此解決方案，請建立 AWS Lambda 函數：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-tag-based-amazon-cloudwatch-dashboards-automatically.html) | AWS DevOps | 

### 移除範例應用程式
<a name="remove-the-sample-application"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 移除 `golden-signals-dashboard` 建構。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-tag-based-amazon-cloudwatch-dashboards-automatically.html) | AWS DevOps | 

## 疑難排解
<a name="create-tag-based-amazon-cloudwatch-dashboards-automatically-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| 找不到 Python 命令 （請參閱第 8 `findresources.sh`行）。 | 檢查 Python 安裝的版本。如果您已安裝 Python 第 3 版，請將 `resources.sh` 檔案第 8 行`python3`的 `python`取代為 ，然後再次執行 `sh deploy.sh`命令以部署解決方案。 | 

## 相關資源
<a name="create-tag-based-amazon-cloudwatch-dashboards-automatically-resources"></a>
+ [引導 ](https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html)(AWS CDK 文件）
+ [使用具名設定檔](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-methods) (AWS CLI 文件）
+ [AWS CDK 研討會](https://cdkworkshop.com/)

## 其他資訊
<a name="create-tag-based-amazon-cloudwatch-dashboards-automatically-additional"></a>

下圖顯示在此解決方案中建立的 Amazon RDS 範例儀表板。

![\[Amazon RDS 的範例儀表板\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/f234fe30-87db-446f-a291-d33928ca2ccb/images/706a262f-8650-47ff-ac44-e04ce5f4023e.png)


# 記錄您的 AWS 登陸區域設計
<a name="document-your-aws-landing-zone-design"></a>

*Michael Daehnert、Florian Langer 和 Michael Lodemann，Amazon Web Services*

## 總結
<a name="document-your-aws-landing-zone-design-summary"></a>

*登陸區域*是架構良好的多帳戶環境，以安全和合規最佳實務為基礎。這是整個企業的容器，可存放所有組織單位 (OUs)、 AWS 帳戶使用者和其他資源。登陸區域可以擴展以符合任何大小企業的需求。 AWS 有兩種建立登陸區域的選項：使用 的服務型登陸區域[AWS Control Tower](https://docs.aws.amazon.com/controltower/latest/userguide/what-is-control-tower.html)或您建置的自訂登陸區域。每個選項都需要不同層級 AWS 的知識。

AWS 建立 AWS Control Tower 以協助您透過自動設定登陸區域來節省時間。 AWS Control Tower 由 管理 AWS ，並使用最佳實務和指導方針來協助您建立基礎環境。 AWS Control Tower 使用整合的服務，例如 [AWS Service Catalog](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/introduction.html)和 [AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_introduction.html)，在您的登陸區域中佈建帳戶，並管理這些帳戶的存取權。

AWS 登陸區域專案的需求、實作詳細資訊和操作動作項目會有所不同。每個登陸區域實作都需要處理自訂層面。這包括 （但不限於） 如何處理存取管理、使用哪個技術堆疊，以及監控對卓越營運有何要求。此模式提供的範本可協助您記錄登陸區域專案。透過使用 範本，您可以更快速地記錄專案，並協助您的開發和營運團隊了解您的登陸區域。

## 先決條件和限制
<a name="document-your-aws-landing-zone-design-prereqs"></a>

**限制**

此模式不會描述什麼是登陸區域或如何實作登陸區域。如需這些主題的詳細資訊，請參閱[相關資源](#document-your-aws-landing-zone-design-resources)一節。

## 史詩
<a name="document-your-aws-landing-zone-design-epics"></a>

### 建立設計文件
<a name="create-the-design-document"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 識別關鍵利益相關者。 | 識別與您的登陸區域連結的關鍵服務和團隊經理。 | 專案經理 | 
| 自訂 範本。 | 在[附件](#attachments-9e39a05a-8f51-4fe3-8999-522feafed6ca)區段中下載範本，然後更新範本，如下所示：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/document-your-aws-landing-zone-design.html) | 專案經理 | 
| 完成範本。 | 在與利益相關者的會議或使用write-and-review程序時，完成範本，如下所示：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/document-your-aws-landing-zone-design.html) | 專案經理 | 
| 共用設計文件。 | 當您的登陸區域設計文件完成後，請將其儲存在共用儲存庫或所有利益相關者都可以存取的中央位置。我們建議您使用標準文件控制程序來記錄和核准設計文件的修訂。 | 專案經理 | 

## 相關資源
<a name="document-your-aws-landing-zone-design-resources"></a>
+ [AWS Control Tower 文件](https://docs.aws.amazon.com/controltower/latest/userguide/what-is-control-tower.html)
  + [規劃您的 AWS Control Tower 登陸區域](https://docs.aws.amazon.com/controltower/latest/userguide/planning-your-deployment.html)
  + [AWSAWS Control Tower 登陸區域的多帳戶策略](https://docs.aws.amazon.com/controltower/latest/userguide/aws-multi-account-landing-zone.html)
  + [登陸區域設定的管理秘訣](https://docs.aws.amazon.com/controltower/latest/userguide/tips-for-admin-setup.html)
  + [對登陸區域組態的期望](https://docs.aws.amazon.com/controltower/latest/userguide/getting-started-configure.html)
+ [的自訂 AWS Control Tower](https://aws.amazon.com/solutions/implementations/customizations-for-aws-control-tower/)(AWS 解決方案程式庫）
+ [設定安全且可擴展的多帳戶 AWS 環境](https://docs.aws.amazon.com/prescriptive-guidance/latest/migration-aws-environment/welcome.html) (AWS 方案指引）

## 附件
<a name="attachments-9e39a05a-8f51-4fe3-8999-522feafed6ca"></a>

若要存取與本文件相關聯的其他內容，請解壓縮下列檔案： [attachment.zip](samples/p-attach/9e39a05a-8f51-4fe3-8999-522feafed6ca/attachments/attachment.zip)

# 使用 AWS CDK 跨多個 AWS 區域、帳戶和 OUs 啟用 Amazon DevOps Guru，以改善營運效能
<a name="improve-operational-performance-by-enabling-amazon-devops-guru-across-multiple-aws-regions-accounts-and-ous-with-the-aws-cdk"></a>

*Rahul Sharad Gaikwad 醫生，Amazon Web Services*

## 摘要
<a name="improve-operational-performance-by-enabling-amazon-devops-guru-across-multiple-aws-regions-accounts-and-ous-with-the-aws-cdk-summary"></a>

此模式示範使用 TypeScript 中的 AWS 雲端開發套件 (AWS CDK)，跨多個 Amazon Web Services (AWS) 區域、帳戶和組織單位 (OUs) 啟用 Amazon DevOps Guru 服務的步驟。您可以使用 AWS CDK 堆疊從管理員 （主要） AWS 帳戶部署 AWS CloudFormation StackSets，以跨多個帳戶啟用 Amazon DevOps Guru，而不是登入每個帳戶，並為每個帳戶個別啟用 DevOps Guru。

Amazon DevOps Guru 提供人工智慧操作 (AIOps) 功能，可協助您改善應用程式的可用性，並更快速地解決操作問題。DevOps Guru 透過套用機器學習 (ML) 支援的建議來減少手動工作量，而不需要任何 ML 專業知識。DevOps Guru 會分析您的資源和操作資料。如果偵測到任何異常，它會提供指標、事件和建議，以協助您解決問題。

此模式說明啟用 Amazon DevOps Guru 的三個部署選項：
+ 對於跨多個帳戶和區域的所有堆疊資源
+ 對於跨 OUs 的所有堆疊資源
+ 對於跨多個帳戶和區域的特定堆疊資源

## 先決條件和限制
<a name="improve-operational-performance-by-enabling-amazon-devops-guru-across-multiple-aws-regions-accounts-and-ous-with-the-aws-cdk-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ 安裝並設定 AWS Command Line Interface (AWS CLI)。（請參閱 [AWS CLI 文件中的安裝、更新和解除安裝](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) AWS CLI。)
+ 安裝並設定 AWS CDK Toolkit。（請參閱 [AWS CDK 文件](https://docs.aws.amazon.com/cdk/latest/guide/cli.html)中的 AWS CDK Toolkit。)
+ Node Package Manager (npm)，已安裝並設定用於 TypeScript 中的 AWS CDK。（請參閱 [npm 文件中的下載和安裝 Node.js 和](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) npm。)
+ 已安裝並設定 Python3，用於執行 Python 指令碼將流量注入範例無伺服器應用程式。（請參閱 [Python 文件中的 Python 設定和用量](https://docs.python.org/3/using/index.html)。)
+ Pip，已安裝並設定為安裝 Python 請求程式庫。（請參閱 PyPl 網站上的 [pip 安裝說明](https://pypi.org/project/pip/)。)

**產品版本**
+ AWS CDK Toolkit 1.107.0 版或更新版本
+ npm 7.9.0 版或更新版本
+ Node.js 15.3.0 版或更新版本

## Architecture
<a name="improve-operational-performance-by-enabling-amazon-devops-guru-across-multiple-aws-regions-accounts-and-ous-with-the-aws-cdk-architecture"></a>

**技術**

此模式的架構包含下列服務：
+ [Amazon DevOps Guru](https://aws.amazon.com/devops-guru/)
+ [AWS CloudFormation](https://aws.amazon.com/cloudformation/)
+ [Amazon API Gateway](https://aws.amazon.com/api-gateway/)
+ [AWS Lambda](https://aws.amazon.com/lambda/)
+ [Amazon DynamoDB](https://aws.amazon.com/dynamodb/)
+ [Amazon CloudWatch](https://aws.amazon.com/cloudwatch/)
+ [AWS CloudTrail](https://aws.amazon.com/cloudtrail/)

**AWS CDK 堆疊**

模式使用下列 AWS CDK 堆疊： 
+ `CdkStackSetAdminRole` – 建立 AWS Identity and Access Management (IAM) 管理員角色，以在管理員和目標帳戶之間建立信任關係。
+ `CdkStackSetExecRole` – 建立 IAM 角色以信任管理員帳戶。
+ `CdkDevopsGuruStackMultiAccReg` – 跨多個 AWS 區域和帳戶為所有堆疊啟用 DevOps Guru，並設定 Amazon Simple Notification Service (Amazon SNS) 通知。
+ `CdkDevopsGuruStackMultiAccRegSpecStacks` – 讓 DevOps Guru 跨多個 AWS 區域和帳戶進行特定堆疊，並設定 Amazon SNS 通知。
+ `CdkDevopsguruStackOrgUnit` – 跨 OUs 啟用 DevOps Guru，並設定 Amazon SNS 通知。 
+ `CdkInfrastructureStack` – 在管理員帳戶中部署範例無伺服器應用程式元件，例如 API Gateway、Lambda 和 DynamoDB，以示範錯誤注入和洞見產生。

**範例應用程式架構**

下圖說明跨多個帳戶和區域部署的範例無伺服器應用程式的架構。模式會使用管理員帳戶來部署所有 AWS CDK 堆疊。它也會使用管理員帳戶做為設定 DevOps Guru 的目標帳戶之一。

1. 啟用 DevOps Guru 時，它會先將每個資源的行為建立基準，然後從 CloudWatch 提供的指標擷取操作資料。

1. 如果偵測到異常，它會將其與 CloudTrail 中的事件建立關聯，並產生洞見。

1. 洞見提供相關的事件序列以及規定的建議，讓運算子能夠識別犯罪資源。

1. Amazon SNS 會將通知訊息傳送至運算子。

![\[已跨多個帳戶和區域部署的範例無伺服器應用程式。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/6075ca48-862a-4aa0-93c6-10bad8195a5c/images/beeb0992-aaa8-4f08-b983-685b6b8b8d5e.png)


**自動化和擴展**

此模式隨附的 [GitHub 儲存庫](https://github.com/aws-samples/amazon-devopsguru-cdk-samples.git)使用 AWS CDK 做為基礎設施做為程式碼 (IaC) 工具，來建立此架構的組態。AWS CDK 可協助您協調資源，並跨多個 AWS 帳戶、區域和 OUs 啟用 DevOps Guru。

## 工具
<a name="improve-operational-performance-by-enabling-amazon-devops-guru-across-multiple-aws-regions-accounts-and-ous-with-the-aws-cdk-tools"></a>

**AWS 服務**
+ [AWS CDK](https://docs.aws.amazon.com/cdk/latest/guide/home.html) – AWS Cloud Development Kit (AWS CDK) 可協助您以五種支援的程式設計語言之一將雲端基礎設施定義為程式碼：TypeScript、JavaScript、Python、Java 和 C\$1。
+ [AWS CLI ](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html)– AWS 命令列界面 (AWS CLI) 是一種統一的工具，可提供一致的命令列界面，以便與 AWS 服務和資源互動。

**Code**

此模式的原始碼可在 [Amazon DevOps Guru CDK 範例](https://github.com/aws-samples/amazon-devopsguru-cdk-samples.git)儲存庫的 GitHub 上取得。AWS CDK 程式碼是以 TypeScript 撰寫。若要複製和使用儲存庫，請遵循下一節中的指示。

**重要**  
此模式中的一些案例包括針對 Unix、Linux 和 macOS 格式化的 AWS CDK 和 AWS CLI 命令範例。對於 Windows，將每一行結尾的反斜線 (\$1) 接續字元替換為插入符號 (^)。

## 史詩
<a name="improve-operational-performance-by-enabling-amazon-devops-guru-across-multiple-aws-regions-accounts-and-ous-with-the-aws-cdk-epics"></a>

### 準備 AWS 資源以進行部署
<a name="prepare-the-aws-resources-for-deployment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定名為 的 AWS 設定檔。 | 如下所示設定您的 AWS 命名設定檔，以在多帳戶環境中部署堆疊。對於管理員帳戶：<pre>$aws configure --profile administrator<br />AWS Access Key ID [****]: <your-administrator-access-key-ID><br />AWS Secret Access Key [****]: <your-administrator-secret-access-key><br />Default region name [None]: <your-administrator-region><br />Default output format [None]: json</pre>對於目標帳戶：<pre>$aws configure --profile target<br />AWS Access Key ID [****: <your-target-access-key-ID><br />AWS Secret Access Key [****]: <your-target-secret-access-key><br />Default region name [None]: <your-target-region><br />Default output format [None]: json</pre>如需詳細資訊，請參閱 AWS CLI 文件中的[使用具名設定檔](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-using-profiles)。 | DevOps 工程師 | 
| 驗證 AWS 設定檔組態。 | （選用） 您可以遵循 AWS CLI 文件中的設定和檢視組態設定中的指示，在 `credentials`和 `config` 檔案中驗證您的 AWS 設定檔組態。 [https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-methods](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-methods) | DevOps 工程師 | 
| 驗證 AWS CDK 版本。 | 執行下列命令來驗證 AWS CDK Toolkit 的版本：<pre>$cdk --version</pre>此模式需要 1.107.0 版或更新版本。如果您有舊版的 AWS CDK，請遵循 [AWS CDK 文件](https://docs.aws.amazon.com/cdk/latest/guide/cli.html)中的指示進行更新。 | DevOps 工程師 | 
| 複製專案程式碼。 | 使用 命令複製此模式的 GitHub 儲存庫：<pre>$git clone https://github.com/aws-samples/amazon-devopsguru-cdk-samples.git</pre> | DevOps 工程師 | 
| 安裝套件相依性並編譯 TypeScript 檔案。 | 安裝套件相依性，並執行下列命令編譯 TypeScript 檔案：<pre>$cd amazon-devopsguru-cdk-samples<br />$npm install<br />$npm fund</pre>這些命令會從範例儲存庫安裝所有套件。如果您收到有關遺失套件的任何錯誤，請使用下列其中一個命令：<pre>$npm ci</pre>—或—<pre>$npm install -g @aws-cdk/<package-name></pre>您可以在 `/amazon-devopsguru-cdk-samples/package.json` 檔案的 `Dependencies`區段中找到套件名稱和版本的清單。如需詳細資訊，請參閱 [npm 文件中的 npm ci](https://docs.npmjs.com/cli/v7/commands/npm-ci) [和 npm 安裝](https://docs.npmjs.com/cli/v7/commands/npm-install)。 | DevOps 工程師 | 

### 建置 （合成） AWS CDK 堆疊
<a name="build-synthesize-the-aws-cdk-stacks"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定 Amazon SNS 通知的電子郵件地址。 | 請依照下列步驟提供 Amazon SNS 通知的電子郵件地址：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/improve-operational-performance-by-enabling-amazon-devops-guru-across-multiple-aws-regions-accounts-and-ous-with-the-aws-cdk.html) | DevOps 工程師 | 
| 建置專案程式碼。 | 執行 命令來建置專案程式碼並合成堆疊：<pre>npm run build && cdk synth </pre>您應該會看到類似下列的輸出： <pre>$npm run build && cdk synth<br />> cdk-devopsguru@0.1.0 build<br />> tsc<br />Successfully synthesized to ~/amazon-devopsguru-cdk-samples/cdk.out<br />Supply a stack id (CdkDevopsGuruStackMultiAccReg,CdkDevopsGuruStackMultiAccRegSpecStacks, CdkDevopsguruStackOrgUnit, CdkInfrastructureStack, CdkStackSetAdminRole, CdkStackSetExecRole) to display its template.</pre>如需詳細資訊和步驟，請參閱 [AWS CDK 文件中的您的第一個](https://docs.aws.amazon.com/cdk/latest/guide/hello_world.html) AWS CDK 應用程式。 | DevOps 工程師 | 
| 列出 AWS CDK 堆疊。 | 執行下列命令以列出所有 AWS CDK 堆疊：<pre>$cdk list</pre>命令會顯示下列清單：<pre>CdkDevopsGuruStackMultiAccReg<br />CdkDevopsGuruStackMultiAccRegSpecStacks<br />CdkDevopsguruStackOrgUnit<br />CdkInfrastructureStack<br />CdkStackSetAdminRole<br />CdkStackSetExecRole</pre> | DevOps 工程師 | 

### 選項 1 - 為跨多個帳戶的所有堆疊資源啟用 DevOps Guru
<a name="option-1---enable-devops-guru-for-all-stack-resources-across-multiple-accounts"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署 AWS CDK 堆疊以建立 IAM 角色。 | 此模式使用 [AWS CloudFormation StackSets](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/what-is-cfnstacksets.html) 跨多個帳戶執行堆疊操作。如果您要建立第一個堆疊集，則必須建立下列 IAM 角色，才能在 AWS 帳戶中設定必要的許可：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/improve-operational-performance-by-enabling-amazon-devops-guru-across-multiple-aws-regions-accounts-and-ous-with-the-aws-cdk.html)角色必須具有這些確切名稱。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/improve-operational-performance-by-enabling-amazon-devops-guru-across-multiple-aws-regions-accounts-and-ous-with-the-aws-cdk.html)如需詳細資訊，請參閱 AWS CloudFormation 文件中的[授予自我管理許可](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacksets-prereqs-self-managed.html)。 | DevOps 工程師 | 
| 部署 AWS CDK 堆疊以跨多個帳戶啟用 DevOps Guru。 | AWS CDK `CdkDevopsGuruStackMultiAccReg`堆疊會建立堆疊集，以跨多個帳戶和區域部署堆疊執行個體。若要部署堆疊，請使用指定的參數執行下列 CLI 命令：<pre>$cdk deploy CdkDevopsGuruStackMultiAccReg \<br />  --profile administrator \<br />  --parameters AdministratorAccountId=<administrator-account-ID> \<br />  --parameters TargetAccountId=<target-account-ID> \<br />  --parameters RegionIds="<region-1>,<region-2>"</pre>Amazon DevOps Guru 目前可在 [DevOps Guru 常見問答集](https://aws.amazon.com/devops-guru/faqs/)中列出的 AWS 區域中使用。 | DevOps 工程師 | 

### 選項 2 - 為跨 OUs 的所有堆疊資源啟用 DevOps Guru
<a name="option-2---enable-devops-guru-for-all-stack-resources-across-ous"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 擷取 OU IDs。 | 在 [AWS Organizations](https://console.aws.amazon.com/organizations/v2/home/accounts) 主控台上，識別您要啟用 DevOps Guru 之組織單位IDs。 | DevOps 工程師 | 
| 啟用 OUs 的服務受管許可。 | 如果您使用 AWS Organizations 進行帳戶管理，則必須授予服務受管許可以啟用 DevOps Guru。使用[組織型受信任存取和服務連結角色 (SLRs)，而不是手動建立 IAM 角色](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacksets-orgs-enable-trusted-access.html)。 | DevOps 工程師 | 
| 部署 AWS CDK 堆疊以跨 OUs 啟用 DevOps Guru。 | AWS CDK `CdkDevopsguruStackOrgUnit`堆疊可跨 OUs 啟用 DevOps Guru 服務。若要部署堆疊，請使用指定的參數執行下列命令：<pre>$cdk deploy CdkDevopsguruStackOrgUnit \<br />  --profile administrator \ <br />  --parameters RegionIds="<region-1>,<region-2>" \<br />  --parameters OrganizationalUnitIds="<OU-1>,<OU-2>"</pre> | DevOps 工程師 | 

### 選項 3 - 為多個帳戶的特定堆疊資源啟用 DevOps Guru
<a name="option-3---enable-devops-guru-for-specific-stack-resources-across-multiple-accounts"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署 AWS CDK 堆疊以建立 IAM 角色。 | 如果您尚未建立第一個選項中顯示的必要 IAM 角色，請先執行此操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/improve-operational-performance-by-enabling-amazon-devops-guru-across-multiple-aws-regions-accounts-and-ous-with-the-aws-cdk.html)如需詳細資訊，請參閱 AWS CloudFormation 文件中的[授予自我管理許可](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacksets-prereqs-self-managed.html)。 | DevOps 工程師 | 
| 刪除現有的堆疊。 | 如果您已使用第一個選項為所有堆疊資源啟用 DevOps Guru，您可以使用下列命令刪除舊堆疊：<pre>$cdk destroy CdkDevopsGuruStackMultiAccReg --profile administrator </pre>或者，您可以在重新部署堆疊時變更` RegionIds` 參數，以避免*堆疊已存在*錯誤。 | DevOps 工程師 | 
| 使用堆疊清單更新 AWS CDK 堆疊。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/improve-operational-performance-by-enabling-amazon-devops-guru-across-multiple-aws-regions-accounts-and-ous-with-the-aws-cdk.html) | 資料工程師 | 
| 部署 AWS CDK 堆疊，以便在多個帳戶中為特定堆疊資源啟用 DevOps Guru。 | AWS CDK `CdkDevopsGuruStackMultiAccRegSpecStacks`堆疊可讓 DevOps Guru 跨多個帳戶進行特定堆疊資源。若要部署堆疊，請執行下列命令：<pre>$cdk deploy CdkDevopsGuruStackMultiAccRegSpecStacks \<br />  --profile administrator  \<br />  --parameters AdministratorAccountId=<administrator-account-ID> \<br />  --parameters TargetAccountId=<target-account-ID> \<br />  --parameters RegionIds="<region-1>,<region-2>"</pre>如果您先前已針對選項 1 部署此堆疊，請變更 `RegionIds` 參數 （請務必從[可用區域](https://aws.amazon.com/devops-guru/faqs/)中選擇），以避免*堆疊已存在*錯誤。 | DevOps 工程師 | 

### 部署 AWS CDK 基礎設施堆疊
<a name="deploy-the-aws-cdk-infrastructure-stack"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署範例無伺服器基礎設施堆疊。 | AWS CDK `CdkInfrastructureStack`堆疊會部署無伺服器元件，例如 API Gateway、Lambda 和 DynamoDB 資料表，以示範 DevOps Guru 洞見。若要部署堆疊，請執行下列命令： <pre>$cdk deploy CdkInfrastructureStack --profile administrator</pre> | DevOps 工程師 | 
| 在 DynamoDB 中插入範例記錄。 | 執行下列命令，將範例記錄填入 DynamoDB 資料表。提供`populate-shops-dynamodb-table.json`指令碼的正確路徑。<pre>$aws dynamodb batch-write-item \<br />  --request-items file://scripts/populate-shops-dynamodb-table.json \<br />  --profile administrator</pre>該命令會顯示下列輸出：<pre>{<br />    "UnprocessedItems": {}<br />}</pre> | DevOps 工程師 | 
| 驗證在 DynamoDB 中插入的記錄。 | 若要驗證 DynamoDB 資料表是否包含 `populate-shops-dynamodb-table.json` 檔案的範例記錄，請存取 `ListRestApiEndpointMonitorOperator` API 的 URL，這會發佈為 AWS CDK 堆疊的輸出。您也可以在`CdkInfrastructureStack`堆疊的 AWS CloudFormation 主控台的**輸出**索引標籤中找到此 URL。AWS CDK 輸出看起來會類似以下內容：<pre>CdkInfrastructureStack.CreateRestApiMonitorOperatorEndpointD1D00045 = https://oure17c5vob.execute-api.<your-region>.amazonaws.com/prod/<br /><br />CdkInfrastructureStack.ListRestApiMonitorOperatorEndpointABBDB8D8 = https://cdff8icfrn4.execute-api.<your-region>.amazonaws.com/prod/</pre> | DevOps 工程師 | 
| 等待資源完成底線。 | 此無伺服器堆疊有幾個資源。建議您等待 2 小時，然後再執行後續步驟。如果您在生產環境中部署此堆疊，最多可能需要 24 小時才能完成基準化，具體取決於您選取要在 DevOps Guru 中監控的資源數量。 | DevOps 工程師 | 

### 產生 DevOps Guru 洞見
<a name="generate-devops-guru-insights"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 更新 AWS CDK 基礎設施堆疊。 | 若要試用 DevOps Guru 洞見，您可以進行一些組態變更，以重現典型的操作問題。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/improve-operational-performance-by-enabling-amazon-devops-guru-across-multiple-aws-regions-accounts-and-ous-with-the-aws-cdk.html) | DevOps 工程師 | 
| 在 API 上注入 HTTP 請求。 | 在 `ListRestApiMonitorOperatorEndpointxxxx` API 上以 HTTP 請求的形式注入輸入流量：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/improve-operational-performance-by-enabling-amazon-devops-guru-across-multiple-aws-regions-accounts-and-ous-with-the-aws-cdk.html) | DevOps 工程師 | 
| 檢閱 DevOps Guru 洞察。 | 在標準條件下，DevOps Guru 儀表板會在持續洞察計數器中顯示零。如果偵測到異常，它會以洞見的形式發出提醒。在導覽窗格中，選擇 **Insights** 以查看異常的詳細資訊，包括概觀、彙總指標、相關事件和建議。如需檢閱洞見的詳細資訊，請參閱[使用 Amazon DevOps Guru 透過 AIOps 取得營運洞](https://aws.amazon.com/blogs/devops/gaining-operational-insights-with-aiops-using-amazon-devops-guru/)見部落格文章。 | DevOps 工程師 | 

### 清除
<a name="clean-up"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 清除和刪除資源。 | 完成此模式之後，您應該移除您建立的資源，以避免產生任何進一步的費用。執行這些命令：<pre>$cdk destroy CdkDevopsGuruStackMultiAccReg --profile administrator<br />$cdk destroy CdkDevopsguruStackOrgUnit --profile administrator<br />$cdk destroy CdkDevopsGuruStackMultiAccRegSpecStacks --profile administrator<br />$cdk destroy CdkInfrastructureStack --profile administrator<br />$cdk destroy CdkStackSetAdminRole --profile administrator<br />$cdk destroy CdkStackSetExecRole --profile administrator<br />$cdk destroy CdkStackSetExecRole --profile target</pre> | DevOps 工程師 | 

## 相關資源
<a name="improve-operational-performance-by-enabling-amazon-devops-guru-across-multiple-aws-regions-accounts-and-ous-with-the-aws-cdk-resources"></a>
+ [使用 Amazon DevOps Guru 獲得 AIOps 的運作洞見](https://aws.amazon.com/blogs/devops/gaining-operational-insights-with-aiops-using-amazon-devops-guru/)
+ [使用 AWS CloudFormation StackSets 輕鬆跨多個帳戶和區域設定 Amazon DevOps Guru](https://aws.amazon.com/blogs/devops/configure-devops-guru-multiple-accounts-regions-using-cfn-stacksets/)
+ [DevOps Guru 研討會](https://aiops-using-devops-guru.workshop.aws/)

# 使用 Account Factory for Terraform 管理多個帳戶的許可集
<a name="govern-permission-sets-aft"></a>

*Anand Krishna Varanasi 和 Siamak Heshmati，Amazon Web Services*

## 總結
<a name="govern-permission-sets-aft-summary"></a>

此模式可協助您將 [AWS Control Tower Account Factory Terraform (AFT)](https://docs.aws.amazon.com/controltower/latest/userguide/aft-overview.html) 與 [AWS IAM Identity Center](https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html) 整合，以 AWS 帳戶 大規模設定多個 的許可。此方法使用自訂 AWS Lambda 函數來自動化以組織身分管理 AWS 帳戶 的 [許可集](https://docs.aws.amazon.com/singlesignon/latest/userguide/permissionsetsconcept.html)指派。這可簡化程序，因為它不需要您平台工程團隊的手動介入。此解決方案可以增強營運效率、安全性和一致性。它提升了 的安全和標準化加入程序 AWS Control Tower，讓企業無法取代其雲端基礎設施的靈活性和可靠性。

## 先決條件和限制
<a name="govern-permission-sets-aft-prereqs"></a>

**先決條件**
+ AWS 帳戶，透過 管理 AWS Control Tower。如需詳細資訊，請參閱 [入門 AWS Control Tower](https://docs.aws.amazon.com/controltower/latest/userguide/getting-started-with-control-tower.html)。
+ 適用於 Terraform 的 Account Factory，部署在您環境中的專用帳戶中。如需詳細資訊，請參閱[部署適用於 Terraform AWS Control Tower 的帳戶工廠](https://docs.aws.amazon.com/controltower/latest/userguide/aft-getting-started.html)。
+ IAM Identity Center 執行個體，在您的環境中設定。如需詳細資訊，請參閱 [IAM Identity Center 入門](https://docs.aws.amazon.com/singlesignon/latest/userguide/getting-started.html)。
+ 已設定的作用中 IAM Identity Center [群組](https://docs.aws.amazon.com/singlesignon/latest/userguide/users-groups-provisioning.html#groups-concept)。 如需詳細資訊，請參閱[將群組新增至您的 IAM Identity Center 目錄](https://docs.aws.amazon.com/singlesignon/latest/userguide/addgroups.html) 。
+ Python 3.9 版或更新版本，已安裝

**限制**
+ 此解決方案只能與透過 管理的帳戶搭配使用 AWS Control Tower。此解決方案是透過使用 Account Factory for Terraform 進行部署。
+ 此模式不包含使用身分來源設定聯合身分的指示。如需如何完成此設定的詳細資訊，請參閱 [IAM Identity Center 文件中的 IAM Identity Center 身分來源教學](https://docs.aws.amazon.com/singlesignon/latest/userguide/tutorials.html)課程。

## Architecture
<a name="govern-permission-sets-aft-architecture"></a>

**AFT 概觀**

AFT 會設定 Terraform 管道，協助您在其中佈建和自訂帳戶 AWS Control Tower。AFT 遵循可自動化帳戶佈建程序的 GitOps 模型 AWS Control Tower。您可以建立*帳戶請求 Terraform 檔案*並將其遞交至儲存庫。這會啟動帳戶佈建的 AFT 工作流程。帳戶佈建完成後，AFT 可以自動執行其他自訂步驟。如需詳細資訊，請參閱 AWS Control Tower 文件中的 [AFT 架構](https://docs.aws.amazon.com/controltower/latest/userguide/aft-architecture.html)。

AFT 提供下列主要儲存庫：
+ `aft-account-request` – 此儲存庫包含要建立或更新的 Terraform 程式碼 AWS 帳戶。
+ `aft-account-customizations` – 此儲存庫包含 Terraform 程式碼，以根據每個帳戶建立或自訂資源。
+ `aft-global-customizations` – 此儲存庫包含 Terraform 程式碼，可大規模為所有帳戶建立或自訂資源。
+ `aft-account-provisioning-customizations` – 此儲存庫會管理僅套用至由 AFT 建立和使用 AFT 管理之特定帳戶的自訂。例如，您可以使用此儲存庫在 IAM Identity Center 中自訂使用者或群組指派，或自動關閉帳戶。

**解決方案概觀**

此自訂解決方案包含 AWS Step Functions 狀態機器和 AWS Lambda 函數，可將許可集指派給多個帳戶的使用者和群組。透過此模式部署的狀態機器會搭配預先存在的 AFT `aft_account_provisioning_customizations` 狀態機器運作。建立新的 時或帳戶建立後，使用者會提交更新 IAM Identity Center AWS 帳戶 使用者和群組指派的請求。他們會將變更推送至`aft-account-request`儲存庫來執行此操作。建立或更新帳戶的請求會在 [Amazon DynamoDB Streams ](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.html)中啟動串流。這會啟動 Lambda 函數，以更新目標的 IAM Identity Center 使用者和群組 AWS 帳戶。

以下是您可以在 Lambda 函數中提供的參數範例，用於將許可集指派至目標使用者和群組：

```
custom_fields = {
    "InstanceArn"         = "<Organization ID>",
    "PermissionSetArn"    = "<Permission set ARN>",
    "PrincipalId"         = "<Principal ID>",
  }
```

以下是此陳述式中的參數：
+ `InstanceArn` – 組織的 Amazon Resource Name (ARN)
+ `PermissionSetArn` – 許可集的 ARN
+ `PrincipalId` – IAM Identity Center 中要套用許可集之使用者或群組的識別符

**注意**  
您必須先建立目標許可集、使用者和群組，才能執行此解決方案。

雖然`InstanceArn`值必須保持一致，但您可以修改 Lambda 函數，將多個許可集指派給多個目標身分。許可集的參數必須以 結尾`PermissionSetArn`，而使用者和群組的參數必須以 結尾`PrincipalId`。您必須定義兩個屬性。以下是如何定義多個許可集和目標使用者和群組的範例：

```
custom_fields = {
    "InstanceArn"                    = "<Organization ID>",
    "AdminAccessPermissionSetArn"    = "<Admin privileges permission set ARN>",
    "AdminAccessPrincipalId"         = "<Admin principal ID>",
    "ReadOnlyAccessPermissionSetArn" = "<Read-only privileges permission set ARN>",
    "ReadOnlyAccessPrincipalId"      = "<Read-only principal ID>",
  }
```

下圖顯示解決方案如何 AWS 帳戶 大規模更新目標中使用者和群組許可集step-by-step工作流程。當使用者啟動帳戶建立請求時，AFT 會啟動 `aft-account-provisioning-framework` Step Functions 狀態機器。此狀態機器會啟動 `extract-alternate-sso` Lambda 函數。Lambda 函數會將許可集指派給目標中的使用者和群組 AWS 帳戶。這些使用者或群組可以來自 IAM Identity Center 中的任何已設定身分來源。身分來源的範例包括 Okta、Active Directory 或 Ping Identity。

![\[建立或更新帳戶時更新許可集的工作流程。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/14751255-3781-48db-a6b7-1a03e28c1020/images/d1de252d-8ac9-4f7d-a559-4ab3e852f325.png)


圖表顯示建立新帳戶時的下列工作流程：

1. 使用者將`custom_fields`變更推送至`aft-account-request`儲存庫。

1. AWS CodePipeline 會啟動將使用者定義中繼資料記錄到 Amazon DynamoDB `aft-request-audit` 資料表 AWS CodeBuild 的任務。此資料表具有屬性來記錄使用者定義的中繼資料。`ddb_event_name` 屬性會定義 AFT 操作的類型：
   + 如果值為 `INSERT`，則解決方案會在建立新的 時，將許可集指派給目標身分 AWS 帳戶 。
   + 如果值為 `UPDATE`，則解決方案會在 AWS 帳戶 建立 之後，將 許可集指派給目標身分。

1. Amazon DynamoDB Streams 會啟動 `aft_alternate_sso_extract` Lambda 函數。

1. `aft_alternate_sso_extract` Lambda 函數在 AWS Control Tower 管理帳戶中擔任 AWS Identity and Access Management (IAM) 角色。

1. Lambda 函數透過對 IAM Identity Center 進行 an 適用於 Python (Boto3) 的 AWS SDK [create\$1account\$1assignment](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sso-admin/client/create_account_assignment.html) API 呼叫，將許可集指派給目標使用者和群組。它會從 Amazon DynamoDB `aft-request-audit` 資料表擷取許可集和身分指派。

1. 當 Step Functions 工作流程完成時，會將許可集指派給目標身分。

**自動化和擴展**

AFT 使用 CodePipeline AWS CodeBuild、DynamoDB 和高度可擴展的 Lambda AWS 服務 等大規模運作。如需其他自動化，您可以將此解決方案與票證或問題管理系統整合，例如 Jira。如需詳細資訊，請參閱此模式[的其他資訊](#govern-permission-sets-aft-additional)一節。

## 工具
<a name="govern-permission-sets-aft-tools"></a>

**AWS 服務**
+ [Account Factory for Terraform (AFT)](https://docs.aws.amazon.com/controltower/latest/userguide/aft-overview.html) 是此解決方案中的主要工具。`aft-account-provisioning-customizations` 儲存庫包含用於建立自訂的 Terraform 程式碼 AWS 帳戶，例如自訂 IAM Identity Center 使用者或群組指派。
+ [Amazon DynamoDB](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html) 是一項全受管 NoSQL 資料庫服務，可提供快速、可預期且可擴展的效能。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一項運算服務，可協助您執行程式碼，無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。
+ [AWS Step Functions](https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html) 是一種無伺服器協同運作服務，可協助您結合 AWS Lambda 函數和其他 AWS 服務 來建置業務關鍵型應用程式。

**其他工具**
+ [Python](https://www.python.org/) 是一種一般用途的電腦程式設計語言。
+ [Terraform](https://www.terraform.io/) 是 HashiCorp 的基礎設施即程式碼 (IaC) 工具，可協助您建立和管理雲端和內部部署資源。

**程式碼儲存庫**

AFT 的程式碼儲存庫可在 GitHub [AWS Control Tower Account Factory for Terraform](https://github.com/aws-ia/terraform-aws-control_tower_account_factory) 儲存庫中使用。此模式的程式碼可在 [AWS 帳戶 Govern SSO 指派中使用 Account Factory for Terraform (AFT)](https://github.com/aws-samples/aft-custom-sso-assignment) 儲存庫。

## 最佳實務
<a name="govern-permission-sets-aft-best-practices"></a>
+ 了解[AWS 共同的責任模型](https://aws.amazon.com/compliance/shared-responsibility-model/)。
+ 遵循 的安全建議 AWS Control Tower。如需詳細資訊，請參閱 [中的安全性 AWS Control Tower](https://docs.aws.amazon.com/controltower/latest/userguide/security.html)。
+ 遵循最低權限原則。如需詳細資訊，請參閱[套用最低權限許可](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege)。
+ 為群組和業務單位建置特定且著重的許可集和 IAM 角色。

## 史詩
<a name="govern-permission-sets-aft-epics"></a>

### 部署解決方案
<a name="deploy-the-solution"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 IAM 角色。 | 在 AWS Control Tower 管理帳戶中，使用 Terraform 建立 IAM 角色。此角色具有跨帳戶存取和信任政策，允許來自身分提供者的聯合存取。它還具有透過 授予其他帳戶存取權的許可 AWS Control Tower。Lambda 函數將擔任此角色。請執行下列操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/govern-permission-sets-aft.html) | AWS DevOps，雲端架構師 | 
| 為您的環境自訂解決方案。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/govern-permission-sets-aft.html) | AWS DevOps，雲端架構師 | 
| 部署解決方案。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/govern-permission-sets-aft.html) | AWS DevOps，雲端架構師 | 
| 設定程式碼儲存庫連線。 | 在您要存放組態檔案的程式碼儲存庫與 之間設定連線 AWS 帳戶。如需說明，請參閱 AWS CodePipeline 文件中的[使用 CodeConnections 將第三方來源提供者新增至管道](https://docs.aws.amazon.com/codepipeline/latest/userguide/pipelines-connections.html)。 | AWS DevOps，雲端架構師 | 

### 使用 解決方案
<a name="use-the-solution"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 啟動 AFT 管道以部署新帳戶。 | 請遵循[使用 AFT 佈建新帳戶](https://docs.aws.amazon.com/controltower/latest/userguide/aft-provision-account.html)中的指示，以啟動 AWS 帳戶 在您的 AWS Control Tower 環境中建立新 的管道。等待帳戶建立程序完成。 | AWS DevOps，雲端架構師 | 
| 驗證變更。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/govern-permission-sets-aft.html) | AWS DevOps，雲端架構師 | 

## 疑難排解
<a name="govern-permission-sets-aft-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| 許可集指派無法運作。 | 確定群組 ARN、組織 ID 和 Lambda 參數正確。如需範例，請參閱此模式*的解決方案概觀*一節。 | 
| 更新儲存庫中的程式碼不會啟動管道。 | 此問題與 AWS 帳戶 和 儲存庫之間的連線有關。在 中 AWS 管理主控台，驗證連線是否作用中。如需詳細資訊，請參閱 AWS CodePipeline 文件中的 [GitHub 連線](https://docs.aws.amazon.com/codepipeline/latest/userguide/connections-github.html)。 | 

## 其他資訊
<a name="govern-permission-sets-aft-additional"></a>

**與票證管理工具整合**

您可以選擇將此解決方案與票證或問題管理工具整合，例如 Jira 或 ServiceNow。下圖顯示此選項的範例工作流程。您可以使用工具的連接器，將票證管理工具與 AFT 解決方案儲存庫整合。如需 Jira 連接器，請參閱[將 Jira 與 GitHub 整合](https://support.atlassian.com/jira-cloud-administration/docs/integrate-jira-software-with-github/)。如需 ServiceNow 連接器，請參閱[與 GitHub 整合](https://www.servicenow.com/docs/bundle/washingtondc-it-asset-management/page/product/software-asset-management2/concept/integrate-with-github.html)。您甚至可以建置自訂解決方案，要求使用者在提取請求核准過程中提供票證 ID。如果 AWS 帳戶 使用 AFT 建立新 的請求獲得核准，該事件可能會啟動工作流程，將自訂欄位新增至 `aft-account-request` GitHub 儲存庫。您可以設計任何符合您使用案例需求的自訂工作流程。

![\[使用 GitHub 動作和票證管理工具的工作流程。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/14751255-3781-48db-a6b7-1a03e28c1020/images/83763f65-32ea-4de0-932f-14a1b2d1d3ad.png)


該圖顯示以下工作流程：

1. 使用者在票證管理工具中請求自訂許可集指派，例如 Jira。

1. 核准案例後，工作流程會開始更新許可集指派。（選用） 您可以使用外掛程式來自訂此步驟的自動化。

1. 運算子會將 Terraform 程式碼與更新的許可集參數傳送至開發或功能分支中的`aft-account-request`儲存庫。

1. GitHub 動作 AWS CodeBuild 會使用 OpenID Connect (OIDC) 呼叫啟動。CodeBuild 使用 [tfsec](https://aquasecurity.github.io/tfsec/v1.20.0/) 和 [checkov](https://www.checkov.io/) 等工具，以程式碼 (IaC) 安全性掃描的形式執行基礎設施。它會警告運算子任何安全違規。

1. 如果找不到違規，GitHub Actions 會建立自動提取請求，並將程式碼檢閱指派給程式碼擁有者。它也會為提取請求建立標籤。

1. 如果程式碼擁有者核准程式碼檢閱，則會啟動另一個 GitHub 動作工作流程。它會檢查提取請求標準，包括：
   + 如果提取請求標題符合要求。
   + 如果提取請求內文包含核准的案例編號。
   + 如果提取請求已正確標記。

1. 如果提取請求符合標準，GitHub 動作會啟動 AFT 產品工作流程。它使用 啟動`ct-aft-account-request`管道 AWS CodePipeline。此管道會在 Step Functions 中啟動`aft-account-provisioning-framework`自訂狀態機器。此狀態機器的運作方式如先前此模式*的解決方案概觀*一節所述。

# 使用引導管道實作 Account Factory for Terraform (AFT)
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline"></a>

*Vinicius Elias 和 Edgar Costa Filho，Amazon Web Services*

## 總結
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline-summary"></a>

此模式提供簡單且安全的方法來從 管理帳戶部署 AWS Control Tower Account Factory for Terraform (AFT) AWS Organizations。解決方案的核心是 CloudFormation 範本，透過建立 Terraform 管道來自動化 AFT 組態，其結構可輕鬆適應初始部署或後續更新。

安全性和資料完整性是 的首要任務 AWS，因此 Terraform 狀態檔案是追蹤受管基礎設施和組態狀態的關鍵元件，可安全地存放在 Amazon Simple Storage Service (Amazon S3) 儲存貯體中。此儲存貯體設定了數種安全措施，包括伺服器端加密和封鎖公開存取的政策，以協助確保您的 Terraform 狀態免於未經授權的存取和資料外洩。

管理帳戶會協調和監督整個環境，因此它是其中的關鍵資源 AWS Control Tower。此模式遵循 AWS 最佳實務，並確保部署程序不僅有效率，也符合安全與控管標準，提供全方位、安全且有效率的方式來在您的 AWS 環境中部署 AFT。

如需 AFT 的詳細資訊，請參閱 [AWS Control Tower 文件](https://docs.aws.amazon.com/controltower/latest/userguide/aft-overview.html)。

## 先決條件和限制
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline-prereqs"></a>

**先決條件**
+ 至少具有下列帳戶的基本 AWS 多帳戶環境：管理帳戶、日誌封存帳戶、稽核帳戶，以及一個用於 AFT 管理的額外帳戶。
+ 已建立 AWS Control Tower 的環境。管理帳戶應該正確設定，因為 CloudFormation 範本會部署在其中。
+  AWS 管理帳戶中的必要許可。您需要足夠的許可來建立和管理資源，例如 S3 儲存貯體、 AWS Lambda 函數、 AWS Identity and Access Management (IAM) 角色和 AWS CodePipeline 專案。
+ 熟悉 Terraform。了解 Terraform 的核心概念和工作流程很重要，因為部署涉及產生和管理 Terraform 組態。

**限制**
+ 請注意您帳戶中[AWS 的資源配額](https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html)。部署可能會建立多個資源，而遇到服務配額可能會阻礙部署程序。
+ 範本專為特定版本的 Terraform 和 而設計 AWS 服務。升級或變更版本可能需要修改範本。
+ 範本不支援自我管理版本控制系統 (VCS) 服務，例如 GitHub Enterprise。

**產品版本**
+ Terraform 1.6.6 版或更新版本
+ AFT 1.11 版或更新版本

## Architecture
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline-architecture"></a>

**目標技術堆疊**
+ CloudFormation
+ AWS CodeBuild
+ AWS CodeCommit
+ AWS CodePipeline
+ Amazon EventBridge
+ IAM
+ AWS Lambda
+ Amazon S3

**目標架構**

下圖說明此模式中討論的實作。

![\[使用引導管道實作 AFT 的工作流程。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/944f9912-87c7-4cc5-8478-7070cf67f7ee/images/4ee74757-940d-4d92-a7f0-fb0db1476247.png)


工作流程包含三個主要任務：建立資源、產生內容和執行管道。

*建立 資源*

[隨此模式提供的 CloudFormation 範本](https://github.com/aws-samples/aft-bootstrap-pipeline/blob/main/code/aft-deployment-pipeline.yaml)會建立和設定所有必要的資源，取決於您在部署範本時選取的參數。範本至少會建立下列資源：
+ 實作 AFT 的 CodePipeline 管道
+ 存放與 AFT 實作相關聯之 Terraform 狀態檔案的 S3 儲存貯體
+ 兩個 CodeBuild 專案實作 Terraform 計劃，並在管道的不同階段套用命令
+ CodeBuild 和 CodePipeline 服務的 IAM 角色
+ 儲存管道執行期成品的第二個 S3 儲存貯體

範本會根據您選取的 VCS 提供者 (CodeCommit 或外部 VCS) 建立下列資源。
+ 對於 **CodeCommit**：
  + 用來存放 AFT Terraform 引導程式碼的 CodeCommit 儲存庫
  + 擷取`main`分支上 CodeCommit 儲存庫變更的 EventBridge 規則
  + EventBridge 規則的另一個 IAM 角色
+ 對於任何其他**外部 VCS 提供者**，例如 GitHub：
  +  AWS CodeConnections 連線

此外，當您選取 CodeCommit 做為 VCS 供應商時，如果您將 `Generate AFT Files` 參數設定為 `true`，範本會建立這些額外的資源來產生內容：
+ 儲存產生內容並用作 CodeCommit 儲存庫來源的 S3 儲存貯體
+ 處理指定參數並產生適當內容的 Lambda 函數
+ 執行 Lambda 函數的 IAM 函數
+ 部署範本時執行 Lambda 函數的 CloudFormation 自訂資源

*產生內容*

若要產生 AFT 引導檔案及其內容，解決方案會使用 Lambda 函數和 S3 儲存貯體。函數會在 儲存貯體中建立資料夾，然後在 資料夾內建立兩個檔案： `main.tf`和 `backend.tf`。函數也會處理提供的 CloudFormation 參數，並以預先定義的程式碼填入這些檔案，取代個別的參數值。

若要檢視用來產生檔案的範本程式碼，請參閱解決方案的 [GitHub 儲存庫](https://github.com/aws-samples/aft-bootstrap-pipeline)。基本上，檔案的產生方式如下。

**main.tf**

```
module "aft" {
  source = "github.com/aws-ia/terraform-aws-control_tower_account_factory?ref=<aft_version>"

  # Required variables
  ct_management_account_id  = "<ct_management_account_id>"
  log_archive_account_id    = "<log_archive_account_id>"
  audit_account_id          = "<audit_account_id>"
  aft_management_account_id = "<aft_management_account_id>"
  ct_home_region            = "<ct_home_region>"

  # Optional variables
  tf_backend_secondary_region = "<tf_backend_secondary_region>"
  aft_metrics_reporting       = "<false|true>"

  # AFT Feature flags
  aft_feature_cloudtrail_data_events      = "<false|true>"
  aft_feature_enterprise_support          = "<false|true>"
  aft_feature_delete_default_vpcs_enabled = "<false|true>"

  # Terraform variables
  terraform_version      = "<terraform_version>"
  terraform_distribution = "<terraform_distribution>"

  # VCS variables (if you have chosen an external VCS)
  vcs_provider                                  = "<github|githubenterprise|gitlab|gitlabselfmanaged|bitbucket>"
  account_request_repo_name                     = "<org-name>/aft-account-request"
  account_customizations_repo_name              = "<org-name>/aft-account-customizations"
  account_provisioning_customizations_repo_name = "<org-name>/aft-account-provisioning-customizations"
  global_customizations_repo_name               = "<org-name>/aft-global-customizations"

}
```

**backend.tf**

```
terraform {
  backend "s3" {
    region = "<aft-main-region>"
    bucket = "<s3-bucket-name>"
    key    = "aft-setup.tfstate"
  }
}
```

在建立 CodeCommit 儲存庫期間，如果您將 `Generate AFT Files` 參數設定為 `true`，範本會使用 S3 儲存貯體搭配產生的內容做為`main`分支的來源，以自動填入儲存庫。

*執行管道*

建立資源並設定引導檔案之後，管道就會執行。第一階段 (*來源*) 從儲存庫的主分支擷取原始程式碼，第二階段 (*建置*) 會執行 Terraform 計劃命令，並產生要檢閱的結果。在第三個階段 (*核准*) 中，管道會等待手動動作核准或拒絕最後一個階段 (*部署*)。在最後一個階段，管道會使用先前 Terraform `apply`命令的結果做為輸入，來執行 Terraform `plan`命令。最後，管理帳戶中的跨帳戶角色和許可將用於在 AFT 管理帳戶中建立 AFT 資源。

**注意**  
如果您選擇外部 VCS 提供者，則需要授權與您的 VCS 提供者憑證的連線。若要完成設定，請遵循 AWS 開發人員工具主控台文件中[更新待定連線](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-update.html)的步驟。

## 工具
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline-tools"></a>

**AWS 服務**
+ [CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 可協助您設定 AWS 資源、快速且一致地佈建資源，以及在整個 AWS 帳戶 和 區域的生命週期中管理這些資源。
+ [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html) 是一種全受管建置服務，可協助您編譯原始程式碼、執行單元測試，並產生準備好部署的成品。 
+ [AWS CodeCommit](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html) 是一種版本控制服務，可協助您私下存放和管理 Git 儲存庫，而無需管理您自己的來源控制系統。
+ [AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html) 可協助您快速建模和設定軟體版本的不同階段，並自動化持續發行軟體變更所需的步驟。
+ [AWS CodeConnections](https://docs.aws.amazon.com/dtconsole/latest/userguide/welcome-connections.html) 可讓 CodePipeline 等 AWS 資源和服務連線至外部程式碼儲存庫，例如 GitHub。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一種運算服務，可執行程式碼以回應事件並自動管理運算資源，提供快速的方式來建立現代化、無伺服器的應用程式以供生產。
+ [適用於 Python (Boto3) 的 AWS SDK](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html) 是一種軟體開發套件，可協助您整合 Python 應用程式、程式庫或指令碼 AWS 服務。

**其他工具**
+ [Terraform](https://developer.hashicorp.com/terraform?product_intent=terraform) 是一種基礎設施即程式碼 (IaC) 工具，可讓您安全且有效率地建置、變更和版本基礎設施。這包括低階元件，例如運算執行個體、儲存和聯網；以及高階元件，例如 DNS 項目和 SaaS 功能。
+ [Python](https://docs.python.org/3.9/tutorial/index.html) 是一種易於學習、功能強大的程式設計語言。它具有高效的高階資料結構，並提供簡單但有效的物件導向程式設計方法。

**程式碼儲存庫**

此模式的程式碼可在 GitHub [AFT 引導管道儲存庫](https://github.com/aws-samples/aft-bootstrap-pipeline)中使用。

如需官方 AFT 儲存庫，請參閱 GitHub 中的 [AWS Control Tower Account Factory for Terraform](https://github.com/aws-ia/terraform-aws-control_tower_account_factory/tree/main)。

## 最佳實務
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline-best-practices"></a>

當您使用提供的 CloudFormation 範本部署 AFT 時，建議您遵循最佳實務，以協助確保安全、有效率且成功的實作。實作和操作 AFT 的重要準則和建議包括下列項目。
+ **徹底檢閱參數**：仔細檢閱並了解 CloudFormation 範本中的每個參數。準確的參數組態對於 AFT 的正確設定和運作至關重要。
+ **定期範本更新**：使用最新的 AWS 功能和 Terraform 版本來更新範本。定期更新可協助您利用新功能並維護安全性。
+ **版本控制**：固定您的 AFT 模組版本，並盡可能使用個別的 AFT 部署進行測試。
+ **範圍**：僅使用 AFT 來部署基礎設施護欄和自訂。請勿使用它來部署您的應用程式。
+ **內嵌和驗證**：AFT 管道需要內嵌和驗證的 Terraform 組態。在將組態推送至 AFT 儲存庫之前，執行 lint、驗證和測試。
+ **Terraform 模組**：建置可重複使用的 Terraform 程式碼做為模組，並一律指定 Terraform 和 AWS 提供者版本以符合組織的需求。

## 史詩
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline-epics"></a>

### 設定環境 AWS
<a name="set-up-and-configure-the-aws-environment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 準備 AWS Control Tower 環境。 |  AWS Control Tower 在您的 AWS 環境中設定和配置，以確保集中管理和控管您的 AWS 帳戶。如需詳細資訊，請參閱 AWS Control Tower 文件中的 [入門 AWS Control Tower](https://docs.aws.amazon.com/controltower/latest/userguide/getting-started-with-control-tower.html)。 | 雲端管理員 | 
| 啟動 AFT 管理帳戶。 | 使用 AWS Control Tower Account Factory 啟動新的 AWS 帳戶 做為您的 AFT 管理帳戶。如需詳細資訊，請參閱 AWS Control Tower 文件中的[使用 Account Factory 佈建 AWS Service Catalog 帳戶](https://docs.aws.amazon.com/controltower/latest/userguide/provision-as-end-user.html)。 | 雲端管理員 | 

### 在管理帳戶中部署 CloudFormation 範本
<a name="deploy-the-cfnshort-template-in-the-management-account"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 啟動 CloudFormation 範本。 | 在此史詩中，您將部署此解決方案隨附的 CloudFormation 範本，以在 AWS 管理帳戶中設定 AFT 引導管道。管道會將 AFT 解決方案部署到您在上一個史詩中所設定的 AFT 管理帳戶中。**步驟 1：開啟 CloudFormation 主控台**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步驟 2：建立新的堆疊**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步驟 3：設定堆疊參數**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步驟 4：決定檔案產生**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步驟 5：填寫 AWS Control Tower 和 AFT 帳戶詳細資訊**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步驟 6：設定 AFT 選項**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步驟 7：指定版本**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步驟 8：檢閱並建立堆疊**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步驟 9：監控堆疊建立**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步驟 10：驗證部署**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html) | 雲端管理員 | 

### 填入並驗證 AFT 引導儲存庫和管道
<a name="populate-and-validate-the-aft-bootstrap-repository-and-pipeline"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 選項 1：填入外部 VCS 的 AFT 引導儲存庫。 | 如果您將 VCS 提供者設定為外部 VCS （而不是 CodeCommit)，請遵循下列步驟。（選用） 部署 CloudFormation 範本之後，您可以在新建立的 AFT 引導儲存庫中填入或驗證內容，並測試管道是否已成功執行。**步驟 1：更新連線**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步驟 2：填入儲存庫**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步驟 2：遞交並推送變更**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html) | 雲端管理員 | 
| 選項 2：填入 CodeCommit 的 AFT 引導儲存庫。 | 如果您將 VCS 提供者設定為 CodeCommit，請遵循下列步驟。（選用） 部署 CloudFormation 範本之後，您可以在新建立的 AFT 引導儲存庫中填入或驗證內容，並測試管道是否已成功執行。如果您將 `Generate AFT Files` 參數設定為 `true`，請跳到下一個案例 （驗證管道）。**步驟 1：填入儲存庫**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步驟 2：遞交並推送您的變更**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html) | 雲端管理員 | 
| 驗證 AFT 引導管道。 | **步驟 1：檢視管道**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步驟 2：核准 Terraform 計劃結果**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步驟 3：等待部署**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步驟 4：檢查已建立的資源**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html) | 雲端管理員 | 

## 疑難排解
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| CloudFormation 範本中包含的自訂 Lambda 函數在部署期間失敗。 | 檢查 Lambda 函數的 Amazon CloudWatch logs以識別錯誤。日誌提供詳細資訊，可協助找出特定問題。確認 Lambda 函數具有必要的許可，並且已正確設定環境變數。 | 
| 由於許可不足，您在資源建立或管理時遇到失敗。 | 檢閱連接到 Lambda 函數、CodeBuild 和涉及部署之其他服務的 IAM 角色和政策。確認他們具有必要的許可。如果有許可問題，請調整 IAM 政策以授予必要的存取權。 | 
| 您正在搭配較新 AWS 服務 或 Terraform 版本使用過時的 CloudFormation 範本版本。 | 定期更新 CloudFormation 範本，使其與最新 AWS 和 Terraform 版本相容。檢查版本備註或文件是否有任何版本特定的變更或要求。 | 
| 您在部署期間達到 AWS 服務 配額。 | 部署管道之前，請檢查 S3 AWS 服務 儲存貯體、IAM 角色和 Lambda 函數等資源的配額。如有必要，請求會增加。如需詳細資訊，請參閱 AWS 網站上的[AWS 服務 配額](https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html)。 | 
| 您因為 CloudFormation 範本中的輸入參數不正確而發生錯誤。 | 仔細檢查所有輸入參數是否有錯字或不正確的值。確認資源識別符，例如帳戶 IDs和區域名稱是準確的。 | 

## 相關資源
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline-resources"></a>

若要成功實作此模式，請檢閱下列資源。這些資源提供其他資訊和指引，在設定和管理 AFT 時，這些資訊和指引非常寶貴 CloudFormation。

**AWS** **文件：**
+ [AWS Control Tower 使用者指南](https://docs.aws.amazon.com/controltower/latest/userguide/what-is-control-tower.html)提供有關設定和管理的詳細資訊 AWS Control Tower。
+ [CloudFormation 文件](https://docs.aws.amazon.com/cloudformation/index.html)提供 CloudFormation 範本、堆疊和資源管理的洞見。

**IAM 政策和最佳實務：**
+ [IAM 中的安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)說明如何使用 IAM 角色和政策來協助保護 AWS 資源。

**上的 Terraform AWS：**
+ [Terraform AWS 提供者文件](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)提供有關搭配 Terraform 使用的完整資訊 AWS。

**AWS 服務 配額：**
+ [AWS 服務 配額](https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html)提供如何檢視 AWS 服務 配額以及如何請求增加的資訊。

# 在多個 AWS 帳戶和 AWS 區域中管理 AWS Service Catalog 產品
<a name="manage-aws-service-catalog-products-in-multiple-aws-accounts-and-aws-regions"></a>

*Ram Kandaswamy，Amazon Web Services*

## 總結
<a name="manage-aws-service-catalog-products-in-multiple-aws-accounts-and-aws-regions-summary"></a>

Amazon Web Services (AWS) Service Catalog 簡化並加速企業基礎設施即程式碼 (IaC) 範本的控管和分發。您可以使用 AWS CloudFormation 範本來定義產品所需的 AWS 資源集合 *（堆疊*)。AWS CloudFormation StackSets 可讓您透過單一操作，跨多個帳戶和 AWS 區域建立、更新或刪除堆疊，藉此擴充此功能。

AWS Service Catalog 管理員使用開發人員撰寫的 CloudFormation 範本來建立產品，並發佈它們。這些產品接著會與產品組合建立關聯，並套用限制來控管。若要將您的產品提供給其他 AWS 帳戶或組織單位 (OUs) 中的使用者，您通常會與他們[共用您的產品組合](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/catalogs_portfolios_sharing.html)。此模式說明管理以 AWS CloudFormation StackSets 為基礎的 AWS Service Catalog 產品方案的替代方法。 AWS CloudFormation StackSets 您可以使用堆疊集限制來設定可部署和使用產品的 AWS 區域和帳戶，而不是共用產品組合。透過使用此方法，您可以在多個帳戶、OUs 和 AWS 區域中佈建 AWS Service Catalog 產品，並從中央位置管理它們，同時滿足您的控管需求。 

此方法的優點：
+ 產品是從主要帳戶佈建和管理，不會與其他帳戶共用。
+ 此方法提供以特定產品為基礎的所有佈建產品 （堆疊） 的合併檢視。
+ 使用 AWS Service Management Connector 的組態比較容易，因為它只以一個帳戶為目標。
+ 查詢和使用來自 AWS Service Catalog 的產品更容易。

## 先決條件和限制
<a name="manage-aws-service-catalog-products-in-multiple-aws-accounts-and-aws-regions-prereqs"></a>

**先決條件**
+ IaC 和版本控制的 AWS CloudFormation 範本
+ 用於佈建和管理 AWS 資源的多帳戶設定和 AWS Service Catalog 

**限制**
+ 此方法使用 AWS CloudFormation StackSets，且適用 StackSets 的限制：
  + StackSets 不支援透過巨集部署 CloudFormation 範本。如果您使用巨集來預先處理範本，您將無法使用 StackSets 型部署。
  + StackSets 可讓您取消堆疊與堆疊集的關聯，因此您可以鎖定特定堆疊來修正問題。不過，取消關聯的堆疊無法與堆疊集重新建立關聯。
+ AWS Service Catalog 會自動產生 StackSet 名稱。目前不支援自訂。

## Architecture
<a name="manage-aws-service-catalog-products-in-multiple-aws-accounts-and-aws-regions-architecture"></a>

**目標架構**

![\[使用者使用 AWS CloudFormation 範本和 StackSets 管理 AWS Service Catalog 產品。 AWS CloudFormation StackSets\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/16458fcd-861d-4ed4-8b91-47e19289a6bb/images/97d23325-b5c6-4ca9-8288-8dec1650c975.png)


1. 使用者會建立 AWS CloudFormation 範本，以 JSON 或 YAML 格式佈建 AWS 資源。

1. CloudFormation 範本會在 AWS Service Catalog 中建立產品，並將其新增至產品組合。

1. 使用者會建立佈建產品，以在目標帳戶中建立 CloudFormation 堆疊。

1. 每個堆疊會佈建 CloudFormation 範本中指定的資源。

## 工具
<a name="manage-aws-service-catalog-products-in-multiple-aws-accounts-and-aws-regions-tools"></a>

**AWS 服務**
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 可協助您設定 AWS 資源、快速且一致地佈建資源，以及在整個 AWS 帳戶和區域的生命週期中管理這些資源。
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 是一種開放原始碼工具，可協助您透過命令列 shell 中的命令與 AWS 服務互動。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 可透過控制已驗證並獲授權使用的人員，協助您安全地管理對 AWS 資源的存取。
+ [AWS Service Catalog](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/introduction.html) 可協助您集中管理針對 AWS 核准的 IT 服務目錄。最終使用者可在機構所設的限制範圍內，迅速地只部署自己需要且經核准的 IT 服務。

## 史詩
<a name="manage-aws-service-catalog-products-in-multiple-aws-accounts-and-aws-regions-epics"></a>

### 跨帳戶佈建產品
<a name="provision-products-across-accounts"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立組合。 | 產品組合是一種容器，其中包含一或多個根據特定條件分組在一起的產品。將產品組合用於您的產品，可協助您在產品集中套用常見的限制條件。若要建立產品組合，請遵循 [AWS Service Catalog 文件](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/portfoliomgmt-create.html)中的指示。如果您使用的是 AWS CLI，以下是範例命令：<pre>aws servicecatalog create-portfolio --provider-name my-provider --display-name my-portfolio</pre>如需詳細資訊，請參閱 [AWS CLI 文件](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/servicecatalog/create-portfolio.html)。 | AWS Service Catalog、IAM | 
| 建立 CloudFormation 範本。 | 建立描述 資源的 CloudFormation 範本。適用時，應參數化資源屬性值。 | AWS CloudFormation、JSON/YAML | 
| 建立具有版本資訊的產品。 | 當您在 AWS Service Catalog 中發佈 CloudFormation 範本時，範本會變成產品。提供選用版本詳細資訊參數的值，例如版本標題和描述；這有助於稍後查詢產品。若要建立產品，請遵循 [AWS Service Catalog 文件](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/productmgmt-cloudresource.html)中的指示。如果您使用的是 AWS CLI，範例命令為：<pre>aws servicecatalog create-product --cli-input-json file://create-product-input.json</pre>其中 `create-product-input.json`是傳遞產品參數的檔案。如需此檔案的範例，請參閱*其他資訊*一節。如需詳細資訊，請參閱 [AWS CLI 文件](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/servicecatalog/create-product.html)。 | AWS Service Catalog | 
| 套用限制條件。 | 將堆疊集限制條件套用至產品組合，以設定產品部署選項，例如多個 AWS 帳戶、區域和許可。如需說明，請參閱 [AWS Service Catalog 文件](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/constraints-stackset.html)。 | AWS Service Catalog | 
| 新增 許可。 | 提供許可給使用者，讓他們可以啟動產品組合中的產品。如需主控台說明，請參閱 [AWS Service Catalog 文件](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/catalogs_portfolios_users.html)。如果您使用的是 AWS CLI，以下是範例命令：<pre>aws servicecatalog associate-principal-with-portfolio \<br />    --portfolio-id port-2s6abcdefwdh4 \<br />    --principal-arn arn:aws:iam::444455556666:role/Admin \<br />    --principal-type IAM</pre>如需詳細資訊，請參閱 [AWS CLI 文件](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/servicecatalog/associate-principal-with-portfolio.html)。 | AWS Service Catalog、IAM | 
| 佈建 產品。 | 佈建的產品是產品的資源執行個體。根據 CloudFormation 範本佈建產品會啟動 CloudFormation 堆疊及其基礎資源。根據堆疊集限制，以適用的 AWS 區域和帳戶為目標來佈建產品。在 AWS CLI 中，以下是範例命令：<pre>aws servicecatalog provision-product \<br />    --product-id prod-abcdfz3syn2rg \<br />    --provisioning-artifact-id pa-abc347pcsccfm \<br />    --provisioned-product-name "mytestppname3"</pre>如需詳細資訊，請參閱 [AWS CLI 文件](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/servicecatalog/provision-product.html)。 | AWS Service Catalog | 

## 相關資源
<a name="manage-aws-service-catalog-products-in-multiple-aws-accounts-and-aws-regions-resources"></a>

**參考**
+ [AWS Service Catalog 概觀](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/what-is_concepts.html)
+ [使用 AWS CloudFormation StackSets](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/using-stacksets.html)

**教學課程和影片**
+ [AWS re：Invent 2019：自動化一切：選項和最佳實務 ](https://www.youtube.com/watch?v=bGBVPIpQMYk)（影片）

## 其他資訊
<a name="manage-aws-service-catalog-products-in-multiple-aws-accounts-and-aws-regions-additional"></a>

當您使用 `create-product`命令時， `cli-input-json` 參數會指向指定資訊的檔案，例如產品擁有者、支援電子郵件和 CloudFormation 範本詳細資訊。以下是此類檔案的範例：

```
{
   "Owner": "Test admin",
      "SupportDescription": "Testing",
         "Name": "SNS",
            "SupportEmail": "example@example.com",
            "ProductType": "CLOUD_FORMATION_TEMPLATE",
               "AcceptLanguage": "en",
                  "ProvisioningArtifactParameters": {
                     "Description": "SNS product",
                        "DisableTemplateValidation": true,
                           "Info": {
                              "LoadTemplateFromURL": "<url>"
                     },
                           "Name": "version 1"
}
```

# 使用 AWS 服務監控 SAP RHEL Pacemaker 叢集
<a name="monitor-sap-rhel-pacemaker-clusters-by-using-aws-services"></a>

*Harsh Thoria、Randy Germann 和 RAVEENDRA Voore，Amazon Web Services*

## 總結
<a name="monitor-sap-rhel-pacemaker-clusters-by-using-aws-services-summary"></a>

此模式概述使用 Amazon CloudWatch 和 Amazon Simple Notification Service (Amazon SNS) 監控和設定適用於 SAP 應用程式和 SAP HANA 資料庫服務的 Red Hat Enterprise Linux (RHEL) Pacemaker 叢集警示的步驟。

組態可讓您在 CloudWatch 日誌串流、指標篩選條件和警示的協助下，監控 SAP SCS 或 ASCS、Enqueue Replication Server (ERS) 和 SAP HANA 叢集資源處於「停止」狀態。Amazon SNS 會傳送電子郵件給基礎設施或 SAP Basis 團隊，告知已停止的叢集狀態。

您可以使用 AWS CloudFormation 指令碼 AWS 或服務主控台來建立此模式 AWS 的資源。此模式假設您使用主控台；它不提供 CloudFormation 指令碼或涵蓋 CloudWatch 和 Amazon SNS 的基礎設施部署。Pacemaker 命令用於設定叢集警示組態。

## 先決條件和限制
<a name="monitor-sap-rhel-pacemaker-clusters-by-using-aws-services-prereqs"></a>

**先決條件**
+ 作用中 AWS 的帳戶。
+ Amazon SNS 會設定 來傳送電子郵件或行動通知。
+ 適用於 ABAP 的 SAP ASCS/ERS 或適用於 Java 的 SCS/ERS，以及 SAP HANA 資料庫 RHEL Pacemaker 叢集。如需詳細說明，請參閱下列主題：
  + [SAP HANA 叢集設定](https://docs.aws.amazon.com/sap/latest/sap-hana/sap-hana-on-aws-manual-deployment-of-sap-hana-on-aws-with-high-availability-clusters.html)
  + [SAP Netweaver ABAP/Java 叢集設定](https://docs.aws.amazon.com/sap/latest/sap-netweaver/sap-netweaver-ha-configuration-guide.html)

**限制**
+ 此解決方案目前適用於 RHEL 7.3 版和更新版本的 Pacemaker 型叢集。它尚未在 SUSE 作業系統上進行測試。

**產品版本**
+ RHEL 7.3 及更新版本

## Architecture
<a name="monitor-sap-rhel-pacemaker-clusters-by-using-aws-services-architecture"></a>

**目標技術堆疊 **
+ RHEL Pacemaker 警示事件驅動型代理程式
+ Amazon Elastic Compute Cloud (Amazon EC2)
+ CloudWatch 警示
+ CloudWatch 日誌群組和指標篩選條件
+ Amazon SNS

**目標架構 **

下圖說明此解決方案的元件和工作流程。

![\[監控 SAP RHEL Pacemaker 叢集的架構\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/ca4d282e-eadd-43fd-8506-3dbeb43e4db6/images/bfc96678-1fd3-47b6-8f09-bf7cf7c4a92c.png)


**自動化和擴展**
+ 您可以使用 CloudFormation 指令碼自動建立 AWS 資源。您也可以使用其他指標篩選條件來擴展和涵蓋多個叢集。

## 工具
<a name="monitor-sap-rhel-pacemaker-clusters-by-using-aws-services-tools"></a>

**AWS 服務**
+ [Amazon CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html) 可協助您 AWS 即時監控 AWS 資源的指標，以及您執行的應用程式。
+  [Amazon Simple Notification Service (Amazon SNS)](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) 可協助您協調和管理發佈者和用戶端之間的訊息交換，包括 Web 伺服器和電子郵件地址。

**工具**
+ CloudWatch 代理程式 （統一） 是一種工具，可從 EC2 執行個體收集系統層級指標、日誌和追蹤，並從應用程式擷取自訂指標。
+ Pacemaker 警示代理程式 （適用於 RHEL 7.3 和更新版本） 是一種工具，可在發生變更時啟動動作，例如當資源在 Pacemaker 叢集中停止或重新啟動時。

## 最佳實務
<a name="monitor-sap-rhel-pacemaker-clusters-by-using-aws-services-best-practices"></a>
+ 如需在 上使用 SAP 工作負載的最佳實務 AWS，請參閱 AWS Well-Architected Framework 的 [SAP Lens](https://docs.aws.amazon.com/wellarchitected/latest/sap-lens/sap-lens.html)。
+ 考慮為 SAP HANA 叢集設定 CloudWatch 監控所涉及的成本。如需詳細資訊，請參閱 [CloudWatch 文件](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_billing.html)。
+ 請考慮使用分頁器或票證機制處理 Amazon SNS 提醒。
+ 一律檢查 RPM 套件的 RHEL 高可用性 (HA) 版本是否有 **pc**、Pacemaker AWS 和 fencing 代理程式。

## 史詩
<a name="monitor-sap-rhel-pacemaker-clusters-by-using-aws-services-epics"></a>

### 設定 Amazon SNS
<a name="set-up-sns"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 SNS 主題。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-sap-rhel-pacemaker-clusters-by-using-aws-services.html) | AWS 管理員 | 
| 修改 SNS 主題的存取政策。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-sap-rhel-pacemaker-clusters-by-using-aws-services.html) | AWS 系統管理員 | 
| 訂閱 SNS 主題。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-sap-rhel-pacemaker-clusters-by-using-aws-services.html)您的 Web 瀏覽器顯示自 Amazon SNS 的確認回覆。 | AWS 系統管理員 | 

### 確認叢集的設定
<a name="confirm-the-setup-of-the-cluster"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 檢查叢集狀態。 | 使用 **pcs 狀態**命令來確認資源已上線。 | SAP Basis 管理員 | 

### 設定 Pacemaker 警示
<a name="configure-pacemaker-alerts"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在主要叢集執行個體上設定 Pacemaker 警示代理程式。 | 登入 pimary 叢集中的 EC2 執行個體，並執行下列命令：<pre>install --mode=0755 /usr/share/pacemaker/alerts/alert_file.sh.sample<br />touch /var/lib/pacemaker/alert_file.sh<br />touch /var/log/pcmk_alert_file.log<br />chown hacluster:haclient /var/log/pcmk_alert_file.log<br />chmod 600 /var/log/pcmk_alert_file.log<br />pcs alert create id=alert_file description="Log events to a file." path=/var/lib/pacemaker/alert_file.sh<br />pcs alert recipient add alert_file id=my-alert_logfile value=/var/log/pcmk_alert_file.log</pre> | SAP Basis 管理員 | 
| 在次要叢集執行個體上設定 Pacemaker 警示代理程式。 | 登入次要叢集中的次要叢集 EC2 執行個體，並執行下列命令：<pre>install --mode=0755 /usr/share/pacemaker/alerts/alert_file.sh.sample<br />touch /var/lib/pacemaker/alert_file.sh<br />touch /var/log/pcmk_alert_file.log<br />chown hacluster:haclient /var/log/pcmk_alert_file.log<br />chmod 600 /var/log/pcmk_alert_file.log</pre> | SAP Basis 管理員 | 
| 確認已建立 RHEL 提醒資源。 | 使用下列命令來確認警示資源已建立：<pre>pcs alert</pre>命令的輸出如下所示：<pre>[root@xxxxxxx ~]# pcs alert <br />Alerts:<br /> Alert: alert_file (path=/var/lib/pacemaker/alert_file.sh)<br />  Description: Log events to a file.<br />  Recipients:<br />   Recipient: my-alert_logfile (value=/var/log/pcmk_alert_file.log)</pre> | SAP Basis 管理員 | 

### 設定 CloudWatch 代理程式
<a name="configure-the-cw-agent"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 安裝 CloudWatch 代理程式。 | 有數種方法可在 EC2 執行個體上安裝 CloudWatch 代理程式。若要使用命令列：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-sap-rhel-pacemaker-clusters-by-using-aws-services.html)如需詳細資訊，請參閱 [CloudWatch 文件](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-on-EC2-Instance.html)。 | AWS 系統管理員 | 
| 將 IAM 角色連接至 EC2 執行個體。 | 若要讓 CloudWatch 代理程式從執行個體傳送資料，您必須將 IAM **CloudWatchAgentServerRole** 角色連接至每個執行個體。或者，您可以將 CloudWatch 代理程式的政策新增至現有的 IAM 角色。如需詳細資訊，請參閱 [CloudWatch 文件](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/create-iam-roles-for-cloudwatch-agent-commandline.html)。 | AWS 管理員 | 
| 設定 CloudWatch 代理程式以監控主要叢集執行個體上的 Pacemaker 警示代理程式日誌檔案。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-sap-rhel-pacemaker-clusters-by-using-aws-services.html) | AWS 管理員 | 
| 在主要和次要叢集執行個體上啟動 CloudWatch 代理程式。 | 若要啟動代理程式，請在主要和次要叢集的 EC2 執行個體上執行下列命令：<pre>sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m<br />ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json</pre> | AWS 管理員 | 

### 設定 CloudWatch 資源
<a name="set-up-cw-resources"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定 CloudWatch 日誌群組。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-sap-rhel-pacemaker-clusters-by-using-aws-services.html)CloudWatch 代理程式會將 Pacemaker 警示檔案以日誌串流的形式傳輸至 CloudWatch 日誌群組。 | AWS 管理員 | 
| 設定 CloudWatch 指標篩選條件。 | 指標篩選條件可協助您搜尋模式，例如 CloudWatch 日誌串流`stop <cluster-resource-name>`中的 。識別此模式時，指標篩選條件會更新自訂指標。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-sap-rhel-pacemaker-clusters-by-using-aws-services.html)當指標篩選條件識別步驟 4 中的模式時，它會將 CloudWatch 自訂指標的值更新`sapcluster_abc`為 **1**。CloudWatch 警示會`SAP-Cluster-QA1-ABC`監控指標，`sapcluster_abc`並在指標值變更為 **1** 時傳送 SNS 通知。這表示叢集資源已停止，且需要採取動作。 | AWS 管理員、SAP Basis 管理員 | 
| 設定 SAP ASCS/SCS 和 ERS 指標的 CloudWatch 指標警示。 | 若要根據單一指標建立警示：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-sap-rhel-pacemaker-clusters-by-using-aws-services.html) | AWS 管理員 | 
| 設定 SAP HANA 指標的 CloudWatch 指標警示。 | 針對下列變更，重複設定上一個任務的 CloudWatch 指標警示的步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-sap-rhel-pacemaker-clusters-by-using-aws-services.html) | AWS 管理員 | 

## 相關資源
<a name="monitor-sap-rhel-pacemaker-clusters-by-using-aws-services-resources"></a>
+ [觸發叢集事件的指令碼 ](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html-single/high_availability_add-on_reference/index#ch-alertscripts-HAAR)(RHEL 文件）
+ [使用精靈建立 CloudWatch 代理程式組態檔案 ](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/create-cloudwatch-agent-configuration-file-wizard.html)(CloudWatch 文件）
+ [在伺服器上安裝和執行 CloudWatch 代理程式 ](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-commandline-fleet.html)(CloudWatch 文件）
+ [根據靜態閾值建立 CloudWatch 警示 ](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/ConsoleAlarms.html)(CloudWatch 文件）
+ [使用高可用性叢集在 AWS 上手動部署 SAP HANA ](https://docs.aws.amazon.com/sap/latest/sap-hana/sap-hana-on-aws-manual-deployment-of-sap-hana-on-aws-with-high-availability-clusters.html)( AWS 網站上的 SAP 文件）
+ [SAP NetWeaver 指南 ](https://docs.aws.amazon.com/sap/latest/sap-netweaver/welcome.html)( AWS 網站上的 SAP 文件）

## 附件
<a name="attachments-ca4d282e-eadd-43fd-8506-3dbeb43e4db6"></a>

若要存取與本文件相關聯的其他內容，請解壓縮下列檔案： [attachment.zip](samples/p-attach/ca4d282e-eadd-43fd-8506-3dbeb43e4db6/attachments/attachment.zip)

# 使用 CloudWatch Logs Insights 監控應用程式活動
<a name="monitor-application-activity-by-using-cloudwatch-logs-insights"></a>

*Ram Kandaswamy，Amazon Web Services*

## 總結
<a name="monitor-application-activity-by-using-cloudwatch-logs-insights-summary"></a>

此模式提供使用 Amazon CloudWatch Logs Insights 自動偵測和提醒應用程式例外狀況的解決方案。透過實作自動化日誌分析和提醒，您可以快速識別和回應生產環境中的應用程式問題。

日誌在監控系統行為、識別問題並確保最佳效能方面扮演重要角色。在遷移程序期間，日誌檔案對於驗證系統在新環境中的運作、偵測相容性問題，以及識別任何非預期行為來說非常寶貴。問題可能與操作或安全性有關。對於與安全相關的問題，儘早偵測未經授權的存取嘗試或可疑活動對於維護安全和法規合規至關重要。處理敏感資料或關鍵系統時，此功能特別重要。

對於需要執行下列動作的團隊來說，此模式特別重要：
+ 維持高可用性的應用程式。
+ 快速回應生產問題。
+ 分析 AWS 服務 日誌未擷取的應用程式特定錯誤。
+ 在沒有預先建置的基礎設施的情況下執行隨需日誌分析。

CloudWatch Logs Insights 最適合用於分析應用程式產生的日誌，其中錯誤內容僅存在於您的應用程式程式碼中。CloudWatch Logs Insights 擅長下列任務：
+ 查詢非結構化或半結構化日誌資料。
+ 在事件回應期間執行隨需分析。
+ 關聯多個日誌群組的事件。
+ 在沒有外部工具的情況下建立快速視覺化。

## 先決條件和限制
<a name="monitor-application-activity-by-using-cloudwatch-logs-insights-prereqs"></a>

**先決條件**
+ 部署在作用中的生產應用程式 AWS 帳戶
+ 基本了解生產應用程式的記錄格式和例外狀況模式
+ 設定為串流至 Amazon CloudWatch Logs 的應用程式日誌

**限制**
+ 有些 AWS 服務 完全無法使用 AWS 區域。如需區域可用性，請參閱[AWS 依區域的服務](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。如需特定端點，請參閱[服務端點和配額](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)，然後選擇服務的連結。

## Architecture
<a name="monitor-application-activity-by-using-cloudwatch-logs-insights-architecture"></a>

下圖顯示 CloudWatch Logs Insights 如何評估資源日誌，並將相關資料視覺化傳送至 CloudWatch 儀表板。

![\[CloudWatch Logs Insights 會評估資源日誌，並將資料視覺化傳送至儀表板。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/082ff4b6-9303-42e6-bc62-263e2254f232/images/b1cbb699-07cd-45e6-ac06-839159bafa6b.png)


該圖顯示以下工作流程：

1. 資源會將日誌發佈至 CloudWatch Logs。資源可以包含 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體或 Amazon Simple Storage Service (Amazon S3) 儲存貯體等 AWS 資源。另一個範例包括已安裝 CloudWatch Agent 的內部部署系統，可將日誌發佈至 CloudWatch。

1. 相關模式字串的 CloudWatch Logs Insights 篩選條件。搜尋模式字串的範例包括「錯誤」、「例外」或特定的規則表達式。

1. 一般而言，生產支援團隊或開發人員會將模式視覺化新增至 CloudWatch 儀表板。

**自動化和擴展**

開發人員可以使用 AWS Cloud Development Kit (AWS CDK)、 或 AWS SDKs 來處理多個字串模式 AWS CloudFormation，來自動化此模式的解決方案。團隊可以將此自動化納入其持續整合和部署 (CI/CD) DevOps 程序。

## 工具
<a name="monitor-application-activity-by-using-cloudwatch-logs-insights-tools"></a>

**AWS 服務**
+ [Amazon CloudWatch Logs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/WhatIsCloudWatchLogs.html) 可協助您集中所有系統、應用程式的日誌， AWS 服務 以便您可以監控日誌並將其安全地存檔。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 透過控制已驗證並獲授權使用的人員，協助您安全地管理對 AWS 資源的存取。
+ [AWS Key Management Service (AWS KMS)](https://docs.aws.amazon.com/kms/latest/developerguide/overview.html) 可協助您建立和控制密碼編譯金鑰，以協助保護您的資料。

## 最佳實務
<a name="monitor-application-activity-by-using-cloudwatch-logs-insights-best-practices"></a>

**查詢效率**
+ 定義和設定[日誌群組](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/Working-with-log-groups-and-streams.html)以分析相關日誌資料。
+ 使用欄位總管來了解日誌資料中可用的結構和欄位。
+ 使用 [CloudWatch Logs Insights 查詢語法撰寫有效率的查詢](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_AnalyzeLogData_LogsInsights.html)。
+ 根據您的特定需求調整[範例查詢](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax-examples.html)，以加快分析速度。
+ 限制查詢時間範圍，以減少掃描的資料並改善效能。
+ [儲存查詢](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_Insights-Saving-Queries.html)以供日後使用，以節省時間並確保分析一致。

**安全性**
+ 將適當的 IAM [政策](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/iam-access-control-overview-cwl.html)套用至 CloudWatch Logs Insights 和日誌群組。遵循最低權限原則，並授予執行任務所需的最低許可。如需詳細資訊，請參閱 IAM 文件中的[授予最低權限](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html#grant-least-priv)和[安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)。
+ 針對敏感[日誌資料使用 啟用日誌資料加密 AWS KMS](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CloudWatchLogs-Insights-Query-Encrypt.html)。

**成本最佳化**
+ 每個查詢掃描的資料每 GB CloudWatch Logs Insights 費用。縮小時間範圍並鎖定特定日誌群組以降低成本。
+ 設定適當的日誌保留政策來管理儲存成本。
+ 若要經常分析大型歷史資料集，請考慮將日誌匯出至 Amazon S3 並使用 Amazon Athena。
+ 檢閱 [CloudWatch 定價](https://aws.amazon.com/cloudwatch/pricing/)以了解使用案例的成本影響。

## 史詩
<a name="monitor-application-activity-by-using-cloudwatch-logs-insights-epics"></a>

### 建立日誌群組並設定日誌以在儀表板中檢視。
<a name="create-log-group-and-configure-logs-to-view-in-dashboard"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定 IAM 許可。 | 若要設定 IAM 許可，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-application-activity-by-using-cloudwatch-logs-insights.html)如需如何建立 IAM 政策或將許可新增至現有政策的詳細資訊，請參閱《[IAM 使用者指南》中的使用客戶受管政策定義自訂 IAM 許可](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html)和[編輯 IAM 政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-edit.html)。 **如需詳細資訊，請參閱《[Amazon CloudWatch Logs 使用者指南》中的 Amazon CloudWatch Logs 的 Identity and Access Management](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/auth-and-access-control-cwl.html) [和 CloudWatch Logs 許可參考](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/permissions-reference-cwl.html)。 *Amazon CloudWatch * | AWS 管理員、AWS DevOps、AWS 系統管理員、雲端管理員、雲端架構師、DevOps 工程師 | 
| 建立 日誌群組 | 若要建立日誌群組，請使用下列任一選項：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-application-activity-by-using-cloudwatch-logs-insights.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-application-activity-by-using-cloudwatch-logs-insights.html) | AWS 管理員、AWS DevOps、AWS 系統管理員、雲端管理員、雲端架構師、DevOps 工程師 | 
| 產生 CloudWatch Logs Insights 查詢。 | 若要建立和儲存 CloudWatch Logs Insights 查詢，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-application-activity-by-using-cloudwatch-logs-insights.html) | AWS 管理員、AWS DevOps、AWS 系統管理員、雲端管理員、雲端架構師、DevOps 工程師 | 
| 在 CloudWatch 儀表板中建立視覺化。 | 若要使用 CloudWatch 儀表板建立視覺化效果，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-application-activity-by-using-cloudwatch-logs-insights.html)如需儀表板選項和功能的詳細資訊，請參閱《[Amazon CloudWatch Logs 使用者指南》中的使用 Amazon CloudWatch 儀表板](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)和[使用儀表板變數建立靈活的 CloudWatch ](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_dashboard_variables.html) CloudWatch 儀表板。 *Amazon CloudWatch * | AWS 管理員、AWS DevOps、AWS 系統管理員、雲端管理員、雲端架構師、DevOps 工程師 | 

## 疑難排解
<a name="monitor-application-activity-by-using-cloudwatch-logs-insights-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| 找不到查詢結果或查詢似乎已中斷 | 從從[範例查詢修改的工作查詢](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax-examples.html)開始。對部分查詢 （例如篩選條件或欄位） 執行小型增量變更，並利用 CloudWatch Logs [查詢產生器功能](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CloudWatchLogs-Insights-Query-Assist.html)。 | 
| 日誌群組未建立日誌串流 | 在 IAM 政策中，請確定 [CreateLogStream](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_CreateLogStream.html) 和 [CreateLogGroup](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_CreateLogGroup.html) 操作的資源具有萬用字元`(*)`值。如果沒有此萬用字元許可，`create `操作將無法成功。 | 
| 查詢逾時或效能緩慢 | 減少時間範圍、鎖定特定日誌群組，或簡化查詢。複雜的規則表達式 (`regex`) 模式和大型時間範圍會增加查詢時間。 | 
| 沒有傳回有效時間範圍的資料 | 驗證日誌群組選擇，並檢查日誌是否正在擷取 （檢閱日誌串流），並確認篩選條件模式與您的日誌格式相符。 | 

## 相關資源
<a name="monitor-application-activity-by-using-cloudwatch-logs-insights-resources"></a>
+ [使用 CloudWatch Logs Insights 分析日誌資料](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AnalyzingLogData.html)
+ [Amazon CloudWatch 常見問答集](https://aws.amazon.com/cloudwatch/faqs/#topic-0)
+ [使用儀表板變數建立靈活的 CloudWatch 儀表板](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_dashboard_variables.html)
+ [Logs Insights QL 入門：查詢教學課程](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_AnalyzeLogData_Tutorials.html)
+ [使用自然語言來產生和更新 CloudWatch Logs Insights 查詢](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CloudWatchLogs-Insights-Query-Assist.html)
+ [搭配 AWS SDK 或 CLI 使用 PutDashboard ](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/example_cloudwatch_PutDashboard_section.html)
+ [使用日誌群組和日誌串流](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/Working-with-log-groups-and-streams.html)

# 跨多個 監控共用 Amazon Machine Image 的使用 AWS 帳戶
<a name="monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts"></a>

*Naveen Suthar 和 Sandeep Gawande，Amazon Web Services*

## 總結
<a name="monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts-summary"></a>

[Amazon Machine Image (AMIs)](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html) 用於在您的 Amazon Web Services () 環境中建立 Amazon Elastic Compute Cloud (Amazon EC2 AWS) 執行個體。您可以在單獨的集中AMIs，在此模式中稱為*建立者帳戶*。然後，您可以在相同 AWS 帳戶 中的多個 之間共用 AMI AWS 區域，在此模式中稱為*取用者帳戶*。從單一帳戶管理 AMIs 可提供可擴展性並簡化控管。在消費者帳戶中，您可以參考 Amazon EC2 Auto Scaling [啟動範本](https://docs.aws.amazon.com/autoscaling/ec2/userguide/create-asg-launch-template.html)和 Amazon Elastic Kubernetes Service (Amazon EKS) [節點群組](https://docs.aws.amazon.com/eks/latest/userguide/managed-node-groups.html)中的共用 AMI。

當共用 AMI [已棄用](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ami-deprecate.html)、[取消註冊](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/deregister-ami.html)[或未共用](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/sharingamis-explicit.html)時， AWS 服務 取用者帳戶中的 AMI 無法使用此 AMI 啟動新的執行個體。相同執行個體的任何自動擴展事件或重新啟動都會失敗。這可能會導致生產環境中的問題，例如應用程式停機時間或效能降低。當 AMI 共用和用量事件在多個 中發生時 AWS 帳戶，可能很難監控此活動。

此模式可協助您監控相同區域中各帳戶的共用 AMI 用量和狀態。它使用無伺服器 AWS 服務，例如 Amazon EventBridge、Amazon DynamoDB AWS Lambda和 Amazon Simple Email Service (Amazon SES)。您可以使用 HashiCorp Terraform 將基礎設施佈建為程式碼 (IaC)。此解決方案會在消費者帳戶中的服務參考已取消註冊或未共用的 AMI 時提供提醒。

## 先決條件和限制
<a name="monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts-prereqs"></a>

**先決條件**
+ 兩個或多個作用中 AWS 帳戶：一個建立者帳戶和一個或多個取用者帳戶
+ 從建立者帳戶與消費者帳戶共用的一或多個 AMIs 
+ Terraform CLI，[已安裝](https://developer.hashicorp.com/terraform/cli) (Terraform 文件）
+ Terraform AWS Provider，[已設定](https://hashicorp.github.io/terraform-provider-aws/) (Terraform 文件）
+ （選用，但建議） 已[設定的](https://developer.hashicorp.com/terraform/language/backend) Terraform 後端 (Terraform 文件）
+ Git，[已安裝](https://github.com/git-guides/install-git)

**限制**
+ 此模式會使用帳戶 ID 來監控已與特定帳戶共用的 AMIs。此模式不會監控已使用組織 ID 與組織共用的 AMIs。
+ AMIs只能與相同 內的帳戶共用 AWS 區域。此模式會監控單一目標區域內AMIs。若要監控多個區域中 AMIs 的使用，您可以在每個區域中部署此解決方案。
+ 此模式不會監控部署此解決方案之前共用的任何 AMIs。如果您想要監控先前共用AMIs，您可以取消共用 AMI，然後與消費者帳戶重新共用。

**產品版本**
+ Terraform 1.2.0 版或更新版本
+ Terraform AWS Provider 4.20 版或更新版本

## Architecture
<a name="monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts-architecture"></a>

**目標技術堆疊**

下列資源會透過 Terraform 佈建為 IaC：
+ Amazon DynamoDB 資料表
+ Amazon EventBridge 規則
+ AWS Identity and Access Management (IAM) 角色
+ AWS Lambda 函數
+ Amazon SES

**目標架構**

![\[監控共用 AMI 使用的架構，並在 AMI 未共用或取消註冊時提醒使用者\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/2d709249-0c68-47d7-be5d-46e8a73071ed/images/8c48c4dd-d681-4c32-9ba8-8f5ad2d66f64.png)


該圖顯示以下工作流程：

1. 建立者帳戶中的 AMI 會與相同 中的取用者帳戶共用 AWS 區域。

1. 共用 AMI 時，建立者帳戶中的 EventBridge 規則會擷取`ModifyImageAttribute`事件，並在建立者帳戶中啟動 Lambda 函數。

1. Lambda 函數會將與 AMI 相關的資料儲存在建立者帳戶中的 DynamoDB 資料表中。

1. 當取用 AWS 服務 者帳戶中的 使用共用 AMI 來啟動 Amazon EC2 執行個體，或當共用 AMI 與啟動範本相關聯時，取用者帳戶中的 EventBridge 規則會擷取共用 AMI 的使用。

1. EventBridge 規則會在取用者帳戶中啟動 Lambda 函數。Lambda 函數會執行下列動作：

   1. Lambda 函數會更新消費者帳戶中 DynamoDB 資料表中的 AMI 相關資料。

   1. Lambda 函數會擔任建立者帳戶中的 IAM 角色，並更新建立者帳戶中的 Lambda 資料表。在 `Mapping` 資料表中，它會建立將執行個體 ID 或啟動範本 ID 映射至其個別 AMI ID 的項目。

1. 在建立者帳戶中集中管理的 AMI 已棄用、取消註冊或未共用。

1. 建立者帳戶中的 EventBridge 規則會使用 `remove`動作擷取 `ModifyImageAttribute`或 `DeregisterImage`事件，並啟動 Lambda 函數。

1. Lambda 函數會檢查 DynamoDB 資料表，以判斷 AMI 是否在任何取用者帳戶中使用。如果`Mapping`資料表中沒有與 AMI 相關聯的執行個體 IDs 或啟動範本 IDs，表示程序已完成。

1. 如果任何執行個體 IDs或啟動範本 IDs 與 `Mapping` 資料表中的 AMI 相關聯，則 Lambda 函數會使用 Amazon SES 將電子郵件通知傳送給設定的訂閱者。

## 工具
<a name="monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts-tools"></a>

**AWS 服務**
+ [Amazon DynamoDB](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html) 是一項全受管 NoSQL 資料庫服務，可提供快速、可預期且可擴展的效能。
+ [Amazon EventBridge](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html) 是一種無伺服器事件匯流排服務，可協助您將應用程式與來自各種來源的即時資料連線。例如， AWS Lambda 函數、使用 API 目的地的 HTTP 呼叫端點，或其他事件匯流排 AWS 帳戶。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 透過控制已驗證並獲授權使用的人員，協助您安全地管理對 AWS 資源的存取。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一項運算服務，可協助您執行程式碼，無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。
+ [Amazon Simple Email Service (Amazon SES)](https://docs.aws.amazon.com/ses/latest/dg/Welcome.html) 可協助您使用自己的電子郵件地址和網域來傳送和接收電子郵件。

**其他工具**
+ [HashiCorp Terraform](https://www.terraform.io/docs) 是一種基礎設施即程式碼 (IaC) 工具，可協助您使用程式碼來佈建和管理雲端基礎設施和資源。
+ [Python](https://www.python.org/) 是一種一般用途的電腦程式設計語言。

**程式碼儲存庫**

此模式的程式碼可在 GitHub [cross-account-ami-monitoring-terraform-samples](https://github.com/aws-samples/cross-account-ami-monitoring-terraform-samples) 儲存庫中使用。

## 最佳實務
<a name="monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts-best-practices"></a>
+ 遵循[使用 AWS Lambda 函數的最佳實務](https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html)。
+ 遵循[建置 AMIs最佳實務](https://docs.aws.amazon.com/marketplace/latest/userguide/best-practices-for-building-your-amis.html)。
+ 建立 IAM 角色時，請遵循最低權限原則，並授予執行任務所需的最低許可。如需詳細資訊，請參閱 IAM 文件中的[授予最低權限](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html#grant-least-priv)和[安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/IAMBestPracticesAndUseCases.html)。
+ 設定 AWS Lambda 函數的監控和提醒。如需詳細資訊，請參閱[監控和疑難排解 Lambda 函數](https://docs.aws.amazon.com/lambda/latest/dg/lambda-monitoring.html)。

## 史詩
<a name="monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts-epics"></a>

### 自訂 Terraform 組態檔案
<a name="customize-the-terraform-configuration-files"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 AWS CLI 具名設定檔。 | 針對建立者帳戶和每個取用者帳戶，建立名為 的 AWS Command Line Interface (AWS CLI) 設定檔。如需說明，請參閱 AWS 《 入門資源中心》中的[設定 AWS CLI](https://aws.amazon.com/getting-started/guides/setup-environment/module-three/) 。 | DevOps 工程師 | 
| 複製儲存庫。 | 輸入以下命令。這會使用 SSH 從 GitHub 複製[cross-account-ami-monitoring-terraform-samples](https://github.com/aws-samples/cross-account-ami-monitoring-terraform-samples) 儲存庫。<pre>git clone git@github.com:aws-samples/cross-account-ami-monitoring-terraform-samples.git</pre> | DevOps 工程師 | 
| 更新 provider.tf 檔案。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts.html)如需設定提供者的詳細資訊，請參閱 Terraform 文件中的[多個提供者組態](https://developer.hashicorp.com/terraform/language/providers/configuration#alias-multiple-provider-configurations)。 | DevOps 工程師 | 
| 更新 terraform.tfvars 檔案。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts.html) | DevOps 工程師 | 
| 更新 main.tf 檔案。 | 只有在您將此解決方案部署到多個消費者帳戶時，才完成這些步驟。如果您只將此解決方案部署到一個取用者帳戶，則不需要修改此檔案。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts.html) | DevOps 工程師 | 

### 使用 Terraform 部署解決方案
<a name="deploy-the-solution-by-using-terraform"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署解決方案。 | 在 Terraform CLI 中，輸入下列命令來部署建立者和取用者帳戶中 AWS 的資源：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts.html) | DevOps 工程師 | 
| 驗證電子郵件地址身分。 | 當您部署 Terraform 計劃時，Terraform 會為 Amazon SES 中的每個消費者帳戶建立電子郵件地址身分。您必須先驗證電子郵件地址，才能將通知傳送到該電子郵件地址。如需說明，請參閱 [Amazon SES 文件中的驗證電子郵件地址身分](https://docs.aws.amazon.com/ses/latest/dg/creating-identities.html#just-verify-email-proc)。 Amazon SES  | 一般 AWS | 

### 驗證資源部署
<a name="validate-resource-deployment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 驗證建立者帳戶中的部署。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts.html) | DevOps 工程師 | 
| 驗證消費者帳戶中的部署。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts.html) | DevOps 工程師 | 

### 驗證監控
<a name="validate-monitoring"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在建立者帳戶中建立 AMI。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts.html) | DevOps 工程師 | 
| 使用消費者帳戶中的 AMI。 | 在消費者帳戶中，使用共用 AMI 來建立 Amazon EC2 執行個體或啟動範本。如需說明，請參閱[如何從自訂 AMI (re：Post 知識中心） 啟動 Amazon EC2 執行個體](https://repost.aws/knowledge-center/launch-instance-custom-ami)，或[為 Auto Scaling 群組建立啟動範本](https://docs.aws.amazon.com/autoscaling/ec2/userguide/create-launch-template.html) (Amazon EC2 Auto Scaling 文件）。AWS  | DevOps 工程師 | 
| 驗證監控和提醒。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts.html) | DevOps 工程師 | 

### （選用） 停止監控共用 AMIs
<a name="optional-stop-monitoring-shared-amis"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 刪除資源。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts.html) | DevOps 工程師 | 

## 疑難排解
<a name="monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| 我未收到電子郵件提醒。 | 未傳送 Amazon SES 電子郵件的原因可能有多種。請檢查以下內容：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts.html) | 

## 相關資源
<a name="monitor-use-of-a-shared-amazon-machine-image-across-multiple-aws-accounts-resources"></a>

**AWS 文件**
+ [使用 Python 建置 Lambda 函數](https://docs.aws.amazon.com/lambda/latest/dg/lambda-python.html) (Lambda 文件）
+ [建立 AMI](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/create-ami.html) (Amazon EC2 文件）
+ [與特定 (Amazon EC2 文件） 共用 AMI AWS 帳戶](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/sharingamis-explicit.html) Amazon EC2 
+ [取消註冊您的 AMI](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/deregister-ami.html) (Amazon EC2 文件）

**Terraform 文件**
+ [安裝 Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli)
+ [Terraform 後端組態](https://www.terraform.io/language/settings/backends/configuration)
+ [Terraform AWS 提供者](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)
+ [Terraform 二進位下載](https://developer.hashicorp.com/terraform/install)

# 檢視 AWS 帳戶或組織的 EBS 快照詳細資訊
<a name="view-ebs-snapshot-details-for-your-aws-account-or-organization"></a>

*Arun Chandapillai 和 Parag Nagwekar，Amazon Web Services*

## 總結
<a name="view-ebs-snapshot-details-for-your-aws-account-or-organization-summary"></a>

此模式說明如何自動產生 Amazon Web Services (AWS) 帳戶或 AWS Organizations 中的組織單位 (OU) 中所有 Amazon Elastic Block Store (Amazon EBS) 快照的隨需報告。 

Amazon EBS 是一種easy-to-use、可擴展、高效能的區塊儲存服務，專為 Amazon Elastic Compute Cloud (Amazon EC2) 而設計。EBS 磁碟區提供耐用且持久的儲存體，您可以連接到 EC2 執行個體。您可以使用 EBS 磁碟區做為資料的主要儲存體，並透過建立快照來取得 EBS 磁碟區的point-in-time備份。您可以使用 AWS 管理主控台或 AWS 命令列界面 (AWS CLI) 來檢視特定 EBS 快照的詳細資訊。此模式提供以程式設計方式擷取您 AWS 帳戶或 OU 中所有 EBS 快照的相關資訊。

您可以使用此模式提供的指令碼來產生逗號分隔值 (CSV) 檔案，其中包含每個快照的下列相關資訊：帳戶 ID、快照 ID、磁碟區 ID 和大小、拍攝快照的日期、執行個體 ID 和描述。如果您的 EBS 快照已標記，報告也會包含擁有者和團隊屬性。

## 先決條件和限制
<a name="view-ebs-snapshot-details-for-your-aws-account-or-organization-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ 安裝[https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html#getting-started-install-instructions](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html#getting-started-install-instructions)並[設定](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) AWS CLI 第 2 版
+ 具有適當許可的 AWS Identity and Access Management (IAM) 角色 （如果您打算從 AWS Organizations 執行指令碼，則為特定帳戶或 OU 中所有帳戶的存取許可）

## Architecture
<a name="view-ebs-snapshot-details-for-your-aws-account-or-organization-architecture"></a>

下圖顯示指令碼工作流程，該工作流程產生 EBS 快照的隨需報告，這些快照分散在 OU 中的多個 AWS 帳戶。

![\[跨 OUs 產生 EBS 快照的隨需報告。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/4e8b1812-2731-4f46-8385-0dd4d92f2d03/images/62d10408-7c85-46cf-a6a4-fe87a6e446f2.png)


## 工具
<a name="view-ebs-snapshot-details-for-your-aws-account-or-organization-tools"></a>

**AWS 服務**
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 是一種開放原始碼工具，可協助您透過命令列 shell 中的命令與 AWS 服務互動。
+ [Amazon Elastic Block Store (Amazon EBS)](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AmazonEBS.html) 提供區塊層級儲存體磁碟區，可與 EC2 執行個體搭配使用。 
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 可透過控制已驗證並獲授權使用的人員，協助您安全地管理對 AWS 資源的存取。
+ [AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_introduction.html) 是一種帳戶管理服務，可協助您將多個 AWS 帳戶合併到您建立並集中管理的組織。

**Code**

此模式中使用的範例應用程式的程式碼可在 [aws-ebs-snapshots-awsorganizations](https://github.com/aws-samples/aws-ebs-snapshots-awsorganizations) 儲存庫的 GitHub 上取得。請依照下一節中的指示使用範例檔案。

## 史詩
<a name="view-ebs-snapshot-details-for-your-aws-account-or-organization-epics"></a>

### 下載指令碼
<a name="download-the-script"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 下載 Python 指令碼。 | 從 [GitHub 儲存庫](https://github.com/aws-samples/aws-ebs-snapshots-awsorganizations)下載 [GetSnapshotDetailsAllAccountsOU.py](https://github.com/aws-samples/aws-ebs-snapshots-awsorganizations/blob/main/GetSnapshotDetailsAllAccountsOU.py)  | 一般 AWS | 

### 取得 AWS 帳戶的 EBS 快照詳細資訊
<a name="get-ebs-snapshot-details-for-an-aws-account"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 執行 Python 指令碼。 | 執行 命令：<pre>python3 getsnapshotinfo.py --file <output-file>.csv --region <region-name> </pre>其中 `<output-file>`是指您想要放置的 EBS 快照相關資訊的 CSV 輸出檔案，而 `<region-name>`是存放快照的 AWS 區域。例如：<pre>python3 getsnapshotinfo.py --file snapshots.csv --region us-east-1 </pre> | 一般 AWS | 

### 取得組織的 EBS 快照詳細資訊
<a name="get-ebs-snapshot-details-for-an-organization"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 執行 Python 指令碼。 | 執行 命令：<pre>python3 getsnapshotinfo.py --file <output-file>.csv --role <IAM-role> --region <region-name> </pre>其中 `<output-file>`是指您想要放置的 EBS 快照相關資訊的 CSV 輸出檔案、`<IAM-role>`提供存取 AWS Organizations 許可的角色，`<region-name>`以及存放快照的 AWS 區域。例如：<pre>python3 getsnapshotinfo.py --file snapshots.csv --role <IAM role> --region us-west-2</pre> | 一般 AWS | 

## 相關資源
<a name="view-ebs-snapshot-details-for-your-aws-account-or-organization-resources"></a>
+ [Amazon EBS 文件](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AmazonEBS.html)
+ [Amazon EBS 動作](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/OperationList-query-ebs.html)
+ [Amazon EBS API 參考](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ebs/index.html)
+ [改善 Amazon EBS 效能](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSPerformance.html)
+ [Amazon EBS 資源](https://aws.amazon.com/ebs/resources/)
+ [EBS 快照定價](https://aws.amazon.com/ebs/pricing/)

## 其他資訊
<a name="view-ebs-snapshot-details-for-your-aws-account-or-organization-additional"></a>

**EBS 快照類型**

Amazon EBS 根據擁有權和存取權提供三種類型的快照：
+ **由您擁有** – ****預設情況下，只有您可以從您擁有的快照建立磁碟區。
+ **公有快照** – 您可以與所有其他 AWS 帳戶公開共用快照。若要建立公有快照，您可以修改快照的許可，以與您指定的 AWS 帳戶共用快照。然後，您將授權的使用者可以透過建立自己的 EBS 磁碟區來使用您共用的快照，而您的原始快照仍然不受影響。您也可以將未加密的快照公開提供給所有 AWS 使用者。不過，基於安全考量，您無法公開您的加密快照。由於可能公開個人和敏感資料，公開快照會造成重大的安全風險。我們強烈建議不要與所有 AWS 帳戶共用您的 EBS 快照。如需共用快照的詳細資訊，請參閱 [AWS 文件](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-modifying-snapshot-permissions.html)。
+ **私有快照** – 您可以與您指定的個別 AWS 帳戶私下共用快照。若要私下與特定 AWS 帳戶共用快照，請遵循 AWS 文件中[的指示](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-modifying-snapshot-permissions.html#share-unencrypted-snapshot)，並為許可設定選擇**私有**。您授權的使用者可使用您共用的快照，以建立他們自己的 EBS 磁碟區，同時原始快照仍然不受影響。

**概觀和程序**

下表提供 EBS 快照詳細資訊的連結，包括如何透過尋找和刪除未使用的快照來降低 EBS 磁碟區成本，以及封存很少存取且不需要頻繁或快速擷取的快照。 


| 
| 
| 如需 的相關資訊 | 請參閱 | 
| --- |--- |
| **快照、其功能和限制** | [建立 Amazon EBS 快照](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-creating-snapshot.html) | 
| **如何建立快照** | 主控台：[建立快照](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-creating-snapshot.html#ebs-create-snapshot)AWS CLI：[create-snapshot 命令](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/create-snapshot.html)例如：<pre>aws ec2 create-snapshot --volume-id vol-1234567890abcdef0 --description " volume snapshot"</pre> | 
| **刪除快照 （一般資訊）** | [刪除 Amazon EBS 快照](https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ebs-deleting-snapshot.html) | 
| **如何刪除快照** | 主控台：[刪除快照](https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ebs-deleting-snapshot.html#ebs-delete-snapshot)AWS CLI：[Delete-snapshot 命令](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/delete-snapshot.html)例如：<pre>aws ec2 delete-snapshot --snapshot-id snap-1234567890abcdef0</pre> | 
| **封存快照 （一般資訊）** | [封存 Amazon EBS 快照](https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/snapshot-archive.html)[Amazon EBS 快照封存 ](https://aws.amazon.com/blogs/aws/new-amazon-ebs-snapshots-archive/)（部落格文章） | 
| **如何封存快照** | 主控台：[封存快照](https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/working-with-snapshot-archiving.html#archive-snapshot)AWS CLI： [modify-snapshot-tier 命令](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/modify-snapshot-tier.html) | 
| **如何擷取封存的快照** | 主控台：[還原封存的快照](https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/working-with-snapshot-archiving.html#restore-archived-snapshot)AWS CLI： [restore-snapshot-tier 命令](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/restore-snapshot-tier.html) | 
| **快照定價** | [Amazon EBS 定價](https://aws.amazon.com/ebs/pricing/) | 

**常見問答集**

**最短封存期間為何？**

最短封存期間為 90 天。

**還原封存快照需要多長時間？**

將封存的快照從封存層還原到標準層最多可能需要 72 小時，取決於快照的大小。

**封存的快照是否完整快照？**

封存的快照一律是完整快照。

**使用者可以封存哪些快照？**

您只能封存您在帳戶中擁有的快照。

**您可以封存已註冊 Amazon Machine Image (AMI) 的根裝置磁碟區的快照嗎？**

否，您無法封存已註冊 AMI 根裝置磁碟區的快照。

**共用快照的安全考量是什麼？**

當您共用快照時，您會讓其他人存取快照上的所有資料。僅與您信任資料的人員共用快照。

**如何與其他 AWS 區域共用快照？**

快照受限於其建立的區域。若要與另一個區域共用快照，請將該快照複製到該區域，然後共用。

**您可以共用已加密的快照嗎？**

您無法共用使用預設 AWS 受管金鑰加密的快照。您只能共用使用客戶受管金鑰加密的快照。當您共用加密快照時，也必須共用用來加密快照的客戶受管金鑰。

**未加密的快照呢？**

您可以公開共用未加密的快照。

# 更多模式
<a name="governance-more-patterns-pattern-list"></a>

**Topics**
+ [在 上使用登陸區域加速器自動建立帳戶 AWS](automate-account-creation-lza.md)
+ [使用 Amazon Bedrock 自動化 AWS 基礎設施操作](automate-aws-infrastructure-operations-by-using-amazon-bedrock.md)
+ [自動化 AWS 資源評估](automate-aws-resource-assessment.md)
+ [跨多個帳戶和區域自動清查 AWS 資源](automate-aws-resource-inventory.md)
+ [使用 AWS CDK 自動化 AWS Service Catalog 產品組合和產品部署](automate-aws-service-catalog-portfolio-and-product-deployment-by-using-aws-cdk.md)
+ [使用 AWS Service Catalog 和 自動化動態管道管理，以在 Gitflow 環境中部署 Hotfix 解決方案 AWS CodePipeline](automate-dynamic-pipeline-management-for-deploying-hotfix-solutions.md)
+ [使用 Terraform 在 Amazon Managed Grafana 上自動化 Amazon MWAA 自訂指標的擷取和視覺化](automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics.md)
+ [使用 Cloud Custodian 和 AWS CDK 將 Systems Manager 的 AWS 受管政策自動連接至 EC2 執行個體設定檔](automatically-attach-an-aws-managed-policy-for-systems-manager-to-ec2-instance-profiles-using-cloud-custodian-and-aws-cdk.md)
+ [自動加密現有和新的 Amazon EBS 磁碟區](automatically-encrypt-existing-and-new-amazon-ebs-volumes.md)
+ [建置包含 MongoDB Atlas 的 AWS 登陸區域](build-aws-landing-zone-that-includes-mongodb-atlas.md)
+ [使用 Amazon CloudWatch Observability Access Manager 集中監控](centralize-monitoring-by-using-amazon-cloudwatch-observability-access-manager.md)
+ [在啟動時檢查 EC2 執行個體是否有強制性標籤](check-ec2-instances-for-mandatory-tags-at-launch.md)
+ [在狀態檔案遺失後，安全地清除 AWS Account Factory for Terraform (AFT) 資源](clean-up-aft-resources-safely-after-state-file-loss.md)
+ [使用 Amazon EFS 在 EC2 執行個體上建立 Amazon ECS 任務定義並掛載檔案系統](create-an-amazon-ecs-task-definition-and-mount-a-file-system-on-ec2-instances-using-amazon-efs.md)
+ [使用 AWS CloudFormation Guard 政策建立 AWS Config 自訂規則](create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies.md)
+ [使用 AWS CDK 層面和逃生艙自訂預設角色名稱](customize-default-role-names-by-using-aws-cdk-aspects-and-escape-hatches.md)
+ [使用 和 AWS CDK CloudFormation 部署和管理 AWS Control Tower 控制項](deploy-and-manage-aws-control-tower-controls-by-using-aws-cdk-and-aws-cloudformation.md)
+ [使用 Terraform 部署和管理 AWS Control Tower 控制項](deploy-and-manage-aws-control-tower-controls-by-using-terraform.md)
+ [使用 AWS CodePipeline、AWS CodeCommit 和 AWS CodeBuild 在多個 AWS 區域中部署程式碼](deploy-code-in-multiple-aws-regions-using-aws-codepipeline-aws-codecommit-and-aws-codebuild.md)
+ [在做為 Docker 容器 AWS IoT Greengrass V2 執行時部署容器化應用程式](deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.md)
+ [使用 AWS CloudFormation 範本有條件地啟用 Amazon GuardDuty](enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates.md)
+ [在 IBM Db2 資料庫中啟用直接封存至 Amazon S3 的 DB2 日誌 Amazon S3 Db2](enable-db2-logarchive-directly-to-amazon-s3-in-ibm-db2-database.md)
+ [使用 PowerShell 匯出 AWS IAM Identity Center 身分及其指派的報告](export-a-report-of-aws-iam-identity-center-identities-and-their-assignments-by-using-powershell.md)
+ [使用 Troposphere 產生包含 AWS Config 受管規則的 AWS CloudFormation 範本](generate-an-aws-cloudformation-template-containing-aws-config-managed-rules-using-troposphere.md)
+ [讓 SageMaker 筆記本執行個體暫時存取另一個 AWS 帳戶中的 CodeCommit 儲存庫](give-sagemaker-notebook-instances-temporary-access-to-a-codecommit-repository-in-another-aws-account.md)
+ [將stonebranch 通用控制器與 AWS Mainframe Modernization 整合](integrate-stonebranch-universal-controller-with-aws-mainframe-modernization.md)
+ [使用 Step Functions 和 Lambda 代理函數跨 AWS 帳戶啟動 CodeBuild 專案](launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function.md)
+ [使用 Terraform 動態管理 AWS 許可集](manage-aws-permission-sets-dynamically-by-using-terraform.md)
+ [使用 appcmd.exe 將 IIS 託管應用程式遷移至 Amazon EC2](migrate-iis-hosted-applications-to-amazon-ec2-by-using-appcmd.md)
+ [使用 ACM 將 Windows SSL 憑證遷移至 Application Load Balancer](migrate-windows-ssl-certificates-to-an-application-load-balancer-using-acm.md)
+ [監控 IAM 根使用者活動](monitor-iam-root-user-activity.md)
+ [AWS 使用 Terraform 在 上建立階層式多區域 IPAM 架構](multi-region-ipam-architecture.md)
+ [使用 AWS CDK 和 GitHub Actions 工作流程最佳化多帳戶無伺服器部署](optimize-multi-account-serverless-deployments.md)
+ [在非工作負載子網路的多帳戶 VPC 設計中保留可路由 IP 空間](preserve-routable-ip-space-in-multi-account-vpc-designs-for-non-workload-subnets.md)
+ [透過部署角色販賣機解決方案來佈建最低權限的 IAM 角色](provision-least-privilege-iam-roles-by-deploying-a-role-vending-machine-solution.md)
+ [使用 Amazon SES AWS 帳戶 向單一電子郵件地址註冊多個](register-multiple-aws-accounts-with-a-single-email-address-by-using-amazon-ses.md)
+ [使用 AWS Lambda 自動化 AWS 帳戶 AWS Managed Microsoft AD 從 移除 Amazon EC2 項目](remove-amazon-ec2-entries-across-aws-accounts-from-aws-managed-microsoft-ad.md)
+ [使用 AWS Lambda 自動化 AWS 帳戶 從 移除相同 中的 Amazon EC2 AWS Managed Microsoft AD 項目](remove-amazon-ec2-entries-in-the-same-aws-account-from-aws-managed-microsoft-ad.md)
+ [使用 AWS Fargate 大規模執行事件驅動和排程工作負載](run-event-driven-and-scheduled-workloads-at-scale-with-aws-fargate.md)
+ [使用 Amazon Macie 保護 CloudWatch Logs 中的敏感資料](secure-cloudwatch-logs-using-macie.md)
+ [使用內部部署 SMTP 伺服器和 Database Mail 傳送 Amazon RDS for SQL Server 資料庫執行個體的通知](send-notifications-for-an-amazon-rds-for-sql-server-database-instance-by-using-an-on-premises-smtp-server-and-database-mail.md)
+ [設定 AWS ParallelCluster 的 Grafana 監控儀表板](set-up-a-grafana-monitoring-dashboard-for-aws-parallelcluster.md)
+ [使用 Terraform 設定企業規模的集中式記錄](set-up-centralized-logging-at-enterprise-scale-by-using-terraform.md)
+ [在 AWS 上設定 SAP on IBM Db2 的災難復原](set-up-disaster-recovery-for-sap-on-ibm-db2-on-aws.md)
+ [使用 Amazon Bedrock 代理程式和 簡化 Amazon EC2 合規管理 AWS Config](streamline-amazon-ec2-compliance-management-with-amazon-bedrock-agents-and-aws-config.md)
+ [使用 AWS Organizations 自動標記 Transit Gateway 連接](tag-transit-gateway-attachments-automatically-using-aws-organizations.md)
+ [使用 BMC Discovery 查詢來擷取遷移資料以進行遷移規劃](use-bmc-discovery-queries-to-extract-migration-data-for-migration-planning.md)
+ [使用 驗證 PCI DSS 4.0 的操作最佳實務 AWS Config](verify-ops-best-practices-pci-dss-4.md)
+ [使用 Splunk 檢視 AWS Network Firewall 日誌和指標](view-aws-network-firewall-logs-and-metrics-by-using-splunk.md)
+ [使用 Amazon Quick Sight 視覺化所有 AWS 帳戶的 IAM 登入資料報告](visualize-iam-credential-reports-for-all-aws-accounts-using-amazon-quicksight.md)

# 訊息與通訊
<a name="messagingandcommunications-pattern-list"></a>

**Topics**
+ [在 Amazon MQ 中自動化 RabbitMQ 組態 Amazon MQ](automate-rabbitmq-configuration-in-amazon-mq.md)
+ [改善 Amazon Connect 聯絡中心中客服人員工作站的通話品質](improve-call-quality-on-agent-workstations-in-amazon-connect-contact-centers.md)
+ [更多模式](messagingandcommunications-more-patterns-pattern-list.md)

# 在 Amazon MQ 中自動化 RabbitMQ 組態 Amazon MQ
<a name="automate-rabbitmq-configuration-in-amazon-mq"></a>

*Yogesh Bhatia 和 Afroz Khan，Amazon Web Services*

## 總結
<a name="automate-rabbitmq-configuration-in-amazon-mq-summary"></a>

[Amazon MQ](https://docs.aws.amazon.com/amazon-mq/) 是一種受管訊息代理程式服務，可與許多熱門訊息代理程式相容。搭配使用 Amazon MQ 與 RabbitMQ 提供強大的 RabbitMQ 叢集，在 Amazon Web Services (AWS) 雲端中受管，具有多個代理程式和組態選項。Amazon MQ 提供高可用性、安全和可擴展的基礎設施，並且可以輕鬆處理每秒大量訊息。多個應用程式可以使用具有不同虛擬主機、佇列和交換的基礎設施。不過，管理這些組態選項或手動建立基礎設施可能需要時間和精力。此模式描述透過單一檔案，在單一步驟中管理 RabbitMQ 組態的方法。您可以在任何持續整合 (CI) 工具中嵌入此模式提供的程式碼，例如 Jenkins 或 Bamboo。 

您可以使用此模式來設定任何 RabbitMQ 叢集。只需要連線至叢集。雖然管理 RabbitMQ 組態有許多其他方式，但此解決方案會在一個步驟中建立整個應用程式組態，因此您可以輕鬆管理佇列和其他詳細資訊。

## 先決條件和限制
<a name="automate-rabbitmq-configuration-in-amazon-mq-prereqs"></a>

**先決條件**
+ 安裝並設定 AWS Command Line Interface (AWS CLI) 以指向您的 AWS 帳戶 （如需說明，請參閱 [AWS CLI 文件](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-linux.html))
+ 已安裝 Ansible，因此您可以執行手冊來建立組態
+ 已安裝 **randommqadmin** （如需說明，請參閱 [RabbitMQ 文件](https://www.rabbitmq.com/management-cli.html))
+ Amazon MQ 中的 RabbitMQ 叢集，使用運作狀態良好的 Amazon CloudWatch 指標建立

**其他要求**
+ 請務必分別建立虛擬主機和使用者的組態，而不是做為 JSON 的一部分。
+ 請確定組態 JSON 是 儲存庫的一部分，且受版本控制。
+ 偵**錯工具 **CLI 的版本必須與 RabbitMQ 伺服器版本相同，因此最佳選項是從 RabbitMQ 主控台下載 CLI。
+ 作為管道的一部分，請確保在每次執行之前驗證 JSON 語法。

**產品版本**
+ AWS CLI 2.0 版
+ Ansible 2.9.13 版
+ **visitorsmqadmin** 3.9.13 版 （必須與 RabbitMQ 伺服器版本相同）

## Architecture
<a name="automate-rabbitmq-configuration-in-amazon-mq-architecture"></a>

**來源技術堆疊**
+ 在現有內部部署虛擬機器 (VM) 或 Kubernetes 叢集 （內部部署或雲端） 上執行的 RabbitMQ 叢集

**目標技術堆疊**
+ Amazon MQ for RabbitMQ 上的自動化 RabbitMQ 組態

**目標架構**

設定 RabbitMQ 的方法有很多種。此模式使用匯入組態功能，其中單一 JSON 檔案包含所有組態。此檔案會套用所有設定，並可由 Bitbucket 或 Git 等版本控制系統進行管理。此模式使用 Ansible 透過**附屬管理** CLI 實作組態。

![\[在 Amazon MQ 中自動化 RabbitMQ 組態 Amazon MQ\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/294120b6-c95f-4cc5-bf85-5ad7e2abdad5/images/292e1284-5c9e-4c82-bb41-010fa84d8d74.png)


## 工具
<a name="automate-rabbitmq-configuration-in-amazon-mq-tools"></a>

**工具**
+ [caughtmqadmin](https://www.rabbitmq.com/management-cli.html) 是 RabbitMQ HTTP 型 API 的命令列工具。它用於管理和監控 RabbitMQ 節點和叢集。
+ [Ansible](https://www.ansible.com/) 是用於自動化應用程式和 IT 基礎設施的開放原始碼工具。
+ [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 可讓您使用命令列 shell 中的命令與 AWS 服務互動。 

**AWS 服務**
+ [Amazon MQ](https://docs.aws.amazon.com/amazon-mq/) 是一種受管訊息中介裝置服務，可讓您輕鬆地在雲端中設定和操作訊息中介裝置。
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 可協助您設定 AWS 基礎設施，並使用基礎設施做為程式碼來加速雲端佈建。

**Code**

此模式中使用的 JSON 組態檔案和範例 Ansible 手冊提供於附件中。

## 史詩
<a name="automate-rabbitmq-configuration-in-amazon-mq-epics"></a>

### 建立您的 AWS 基礎設施
<a name="create-your-aws-infrastructure"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在 AWS 上建立 RabbitMQ 叢集。 | 如果您還沒有 RabbitMQ 叢集，您可以使用 [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 在 AWS 上建立堆疊。或者，您可以使用 [Ansible 中的 Cloudformation 模組](https://docs.ansible.com/ansible/latest/collections/amazon/aws/cloudformation_module.html)來建立堆疊。使用後者方法，您可以針對這兩個任務使用 Ansible： 來建立 RabbitMQ 基礎設施和管理組態。  | AWS CloudFormation、Ansible | 

### 建立 Amazon MQ for RabbitMQ 組態
<a name="create-the-amazon-mq-for-rabbitmq-configuration"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立屬性檔案。 | 在附件中下載 JSON 組態檔案 (`rabbitmqconfig.json`)，或從 RabbitMQ 主控台匯出。 修改它以設定佇列、交換和繫結。此組態檔案示範下列項目：- 建立兩個佇列： `sample-queue1` 和 `sample-queue2` - 建立兩個交換： `sample-exchange1` 和 `sample-exchange2`- 實作佇列和交換之間的繫結這些組態會視需要在 root (/) 虛擬主機下執行。 ****  | JSON | 
| 擷取 Amazon MQ for RabbitMQ 基礎設施的詳細資訊。 | 擷取 AWS 上 RabbitMQ 基礎設施的下列詳細資訊：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-rabbitmq-configuration-in-amazon-mq.html)您可以使用 AWS 管理主控台或 AWS CLI 來擷取此資訊。這些詳細資訊可讓 Ansible 手冊連線至您的 AWS 帳戶，並使用 RabbitMQ 叢集來執行命令。執行 Ansible 手冊的電腦必須能夠存取您的 AWS 帳戶，而且必須已設定 AWS CLI，如*先決條件*一節中所述。 | AWS CLI、Amazon MQ | 
| 建立 hosts\$1var 檔案。 | 為 Ansible 建立 `hosts_var` 檔案，並確定檔案中已定義所有變數。考慮使用 Ansible Vault 來存放密碼。您可以設定 `hosts_var` 檔案，如下所示 （以您的資訊取代星號）：<pre>RABBITMQ_HOST: "***********.mq.us-east-2.amazonaws.com"<br />RABBITMQ_VHOST: "/"<br />RABBITMQ_USERNAME: "admin"<br />RABBITMQ_PASSWORD: "*******"</pre> | Ansible | 
| 建立 Ansible 手冊。 | 如需範例手冊，請參閱附件`ansible-rabbit-config.yaml`中的 。下載並儲存此檔案。Ansible 手冊會匯入和管理應用程式所需的所有 RabbitMQ 組態，例如佇列、交換和繫結。 遵循 Ansible 手冊的最佳實務，例如保護密碼。使用 Ansible Vault 進行密碼加密，並從加密的檔案擷取 RabbitMQ 密碼。 | Ansible | 

### 部署組態
<a name="deploy-the-configuration"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 執行 手冊。 | 執行您在上一個史詩中所建立的 Ansible 手冊。<pre>ansible-playbook ansible-rabbit-config.yaml</pre>您可以在 RabbitMQ 主控台上驗證新組態。 | RabbitMQ、Amazon MQ、Ansible | 

## 相關資源
<a name="automate-rabbitmq-configuration-in-amazon-mq-resources"></a>
+ [從 RabbitMQ 遷移至 Amazon MQ](https://aws.amazon.com/blogs/compute/migrating-from-rabbitmq-to-amazon-mq/) (AWS 部落格文章）
+ [Management Command Line Tool](https://www.rabbitmq.com/management-cli.html) (RabbitMQ 文件）
+ [建立或刪除 AWS CloudFormation 堆疊](https://docs.ansible.com/ansible/latest/collections/amazon/aws/cloudformation_module.html) (Ansible 文件）
+ [將訊息驅動的應用程式遷移至 Amazon MQ for RabbitMQ](https://aws.amazon.com/blogs/compute/migrating-message-driven-applications-to-amazon-mq-for-rabbitmq/) (AWS 部落格文章）

## 附件
<a name="attachments-294120b6-c95f-4cc5-bf85-5ad7e2abdad5"></a>

若要存取與本文件相關聯的其他內容，請解壓縮下列檔案： [attachment.zip](samples/p-attach/294120b6-c95f-4cc5-bf85-5ad7e2abdad5/attachments/attachment.zip)

# 改善 Amazon Connect 聯絡中心中客服人員工作站的通話品質
<a name="improve-call-quality-on-agent-workstations-in-amazon-connect-contact-centers"></a>

*Ernest Ozdoba，Amazon Web Services*

## 摘要
<a name="improve-call-quality-on-agent-workstations-in-amazon-connect-contact-centers-summary"></a>

通話品質問題是聯絡中心疑難排解最困難的問題之一。若要避免語音品質問題和複雜的疑難排解程序，您必須最佳化客服人員的工作環境和工作站設定。此模式說明 Amazon Connect 聯絡中心中客服人員工作站的語音品質最佳化技術。它在以下領域提供建議：
+ 工作環境調整。客服人員的周圍環境不會影響語音透過網路傳輸的方式，但確實會影響通話品質。 
+ 客服人員工作站設定。聯絡中心工作站的硬體和網路組態會對通話品質產生重大影響。
+ 瀏覽器設定。客服人員使用 Web 瀏覽器存取 Amazon Connect 聯絡控制面板 (CCP) 網站並與客戶通訊，因此瀏覽器設定可能會影響通話品質。

下列元件也可能影響通話品質，但它們不在工作站範圍內，且不在此模式中涵蓋：
+ 透過 AWS Direct Connect、完整通道 VPN 或分割通道 VPN 流向 Amazon Web Services (AWS) 雲端的流量  
+ 在公司辦公室內外工作時的網路條件
+ 公有交換電話網路 (PSTN) 連線
+ 客戶的裝置和電話電信業者
+ 虛擬桌面基礎設施 (VDI) 設定

如需有關這些區域的詳細資訊，請參閱 Amazon Connect 文件中的[常見聯絡控制面板 (CCP) 問題](https://docs.aws.amazon.com/connect/latest/adminguide/common-ccp-issues.html)和[使用端點測試公用程式](https://docs.aws.amazon.com/connect/latest/adminguide/check-connectivity-tool.html)。

## 先決條件和限制
<a name="improve-call-quality-on-agent-workstations-in-amazon-connect-contact-centers-prereqs"></a>

**先決條件**
+ 耳機和工作站必須符合 [Amazon Connect 管理員指南](https://docs.aws.amazon.com/connect/latest/adminguide/ccp-agent-hardware.html)中指定的要求。 

**限制**
+ 此模式中的最佳化技術適用於軟電話語音品質。當您在桌面電話模式下設定 Amazon Connect CCP 時，它們不適用。不過，如果您的軟電話設定沒有為通話提供可接受的語音品質，您可以使用桌面電話模式。

**產品版本**
+ 如需支援的瀏覽器和版本，請參閱 [Amazon Connect 管理員指南](https://docs.aws.amazon.com/connect/latest/adminguide/browsers.html)。

## Architecture
<a name="improve-call-quality-on-agent-workstations-in-amazon-connect-contact-centers-architecture"></a>

此模式與架構無關，因為它以客服人員工作站設定為目標。如下圖所示，從客服人員到客戶的語音路徑會受到客服人員的耳機、瀏覽器、作業系統、工作站硬體和網路的影響。

![\[Amazon Connect 工作站呼叫中客服人員與客戶之間的語音路徑\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/04ac4c80-30c4-4a48-8411-e3aac7bc2887/images/04e94efc-39d1-424d-a299-89ea17d40153.png)


在 Amazon Connect 聯絡中心中，會使用 WebRTC 建立使用者的音訊連線。語音使用 [Opus 互動式音訊轉碼器](https://opus-codec.org/)進行編碼，並使用傳輸中的安全即時傳輸協定 (SRTP) 進行加密。其他網路架構是可行的，包括 VPN、私有 WAN/LAN 和 ISP 網路。

## 工具
<a name="improve-call-quality-on-agent-workstations-in-amazon-connect-contact-centers-tools"></a>
+ [Amazon Connect Endpoint Test Utility](https://tools.connect.aws/endpoint-test/) – 此公用程式會檢查網路連線和瀏覽器設定。
+ WebRTC 設定的瀏覽器組態編輯器：
  + 對於 Firefox：**關於：config**
  + 對於 Chrome：**chrome：//flags**
+ [CCP 日誌剖析器 ](https://tools.connect.aws/ccp-log-parser/index.html)– 此工具可協助您分析 CCP 日誌以進行故障診斷。

## 史詩
<a name="improve-call-quality-on-agent-workstations-in-amazon-connect-contact-centers-epics"></a>

### 調整工作環境
<a name="adjust-the-work-environment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 減少背景雜訊。 | 避免嘈雜的環境。如果無法做到這一點，請使用這些隔音秘訣來最佳化環境：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/improve-call-quality-on-agent-workstations-in-amazon-connect-contact-centers.html) | 客服人員、管理員 | 

### 最佳化客服人員工作站設定
<a name="optimize-agent-workstation-settings"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 選擇正確的耳機。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/improve-call-quality-on-agent-workstations-in-amazon-connect-contact-centers.html) | 客服人員、管理員 | 
| 如預期使用耳機。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/improve-call-quality-on-agent-workstations-in-amazon-connect-contact-centers.html) | 客服人員 | 
| 檢查工作站資源。 | 請確定客服人員的電腦具有效能。如果他們使用耗用資源的第三方應用程式，其電腦可能不符合執行 CCP 的最低[硬體需求](https://docs.aws.amazon.com/connect/latest/adminguide/ccp-agent-hardware.html)。如果客服人員遇到通話品質問題，請確定他們有足夠的處理能力 (CPU)、磁碟空間、網路頻寬和記憶體可供 CCP 使用。客服人員應關閉任何不必要的應用程式和索引標籤，以改善 CCP 效能和通話品質。 | 管理員 | 
| 設定作業系統的聲音設定。 | 麥克風關卡和提升的預設設定通常可以正常運作。如果您發現外撥語音是安靜的，或麥克風的接聽過多，可能有助於調整這些設定。您可以在電腦的系統音效組態中找到麥克風設定 (**音效**、[MacOS](https://support.apple.com/en-gb/guide/mac-help/mchlp2567/12.0/mac/12.0) 上的**輸入**、[Windows](https://support.microsoft.com/en-us/windows/fix-microphone-problems-5f230348-106d-bfa4-1db5-336f35576011) 中的**麥克風屬性**)。您可以透過系統工具或第三方應用程式存取可能影響語音品質的進階設定。以下是您可以檢查的一些設定：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/improve-call-quality-on-agent-workstations-in-amazon-connect-contact-centers.html)如果您遇到語音品質問題，請嘗試將這些值還原為其預設設定，然後再進一步調查。如需這些和其他可調整設定的詳細資訊，請參閱裝置手冊。 | 客服人員、管理員 | 
| 使用有線網路。 | 一般而言，有線乙太網路的延遲較低，因此更容易提供語音資料傳輸所需的一致傳輸品質。 我們建議每次呼叫至少 100 KB 頻寬。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/improve-call-quality-on-agent-workstations-in-amazon-connect-contact-centers.html) | 網路管理員、客服人員 | 
| 更新硬體驅動程式。 | 當您使用具有自己的韌體的 USB 或其他類型的耳機時，我們建議您使用最新版本來保持其最新狀態。使用輔助連接埠的簡單耳機使用電腦的內建音訊裝置，因此請確定作業系統硬體驅動程式是最新的。在極少數情況下，音訊驅動程式更新可能會導致音訊問題，您可能需要將其轉返。如需變更韌體和驅動程式版本的詳細資訊，請參閱裝置手冊。 | 管理員 | 
| 避免 USB 集線器和硬體鎖。 | 當您連接耳機時，請避免使用其他裝置，例如硬體鎖、連接埠類型轉換器、集線器和延長纜線。這些裝置可能會影響通話品質。請改為將裝置直接連接到電腦的連接埠。 | 客服人員 | 
| 檢查 CCP 日誌。 | CCP Log Parser 提供簡單的方式來檢查應用程式日誌。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/improve-call-quality-on-agent-workstations-in-amazon-connect-contact-centers.html) | 客服人員 （進階技能） | 

### 最佳化瀏覽器設定
<a name="optimize-browser-settings"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 還原預設 WebRTC 設定。 | 必須啟用 WebRTC，才能使用 CCP 進行軟性通話。我們建議您保留 WebRTC 相關功能的預設設定。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/improve-call-quality-on-agent-workstations-in-amazon-connect-contact-centers.html) | 管理員 | 
| 故障診斷時停用瀏覽器擴充功能。 | 有些瀏覽器擴充功能可能會影響通話品質，甚至防止通話正常連線。在瀏覽器中使用 incognito 視窗或私有模式，並停用所有擴充功能。如果這樣可以解決問題，請檢閱您的瀏覽器延伸模組並尋找可疑的附加元件，或個別停用。 | 客服人員、管理員 | 
| 檢查瀏覽器取樣率。 | 確認您的麥克風輸入已設定為最佳 48 kHz 取樣率。如需說明，請參閱 [Amazon Connect 管理員指南](https://docs.aws.amazon.com/connect/latest/adminguide/verify-sample-rate.html)。 | 客服人員、管理員 | 

## 相關資源
<a name="improve-call-quality-on-agent-workstations-in-amazon-connect-contact-centers-resources"></a>

如果您已遵循此模式中的步驟，但仍遇到通話品質的問題，請參閱下列資源以取得疑難排解秘訣。
+ 檢閱[常見的聯絡控制面板 (CCP) 問題](https://docs.aws.amazon.com/connect/latest/adminguide/common-ccp-issues.html)。
+ 檢查與 [Endpoint Test Utility](https://docs.aws.amazon.com/en_us/connect/latest/adminguide/check-connectivity-tool.html) 的連線。
+ 針對任何其他問題，請遵循[疑難排解指南 ](https://docs.aws.amazon.com/connect/latest/adminguide/troubleshooting.html)。 

如果您的疑難排解和調整無法解決呼叫品質問題，根本原因可能是工作站外部。如需進一步疑難排解，請聯絡您的 IT 支援團隊。 

# 更多模式
<a name="messagingandcommunications-more-patterns-pattern-list"></a>

**Topics**
+ [使用 CQRS 和事件來源將整體分解為微服務](decompose-monoliths-into-microservices-by-using-cqrs-and-event-sourcing.md)
+ [在聊天應用程式自訂動作和 中使用 Amazon Q Developer 部署 ChatOps 解決方案來管理 SAST 掃描結果 CloudFormation](deploy-chatops-solution-to-manage-sast-scan-results.md)
+ [將 Amazon API Gateway 與 Amazon SQS 整合，以處理非同步 REST APIs](integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis.md)
+ [使用自動化工作流程簡化 Amazon Lex 機器人開發和部署](streamline-amazon-lex-bot-development-and-deployment-using-an-automated-workflow.md)

# 多帳戶策略
<a name="multiaccountstrategy-pattern-list"></a>

**Topics**
+ [將 AWS 成員帳戶從 遷移 AWS Organizations 至 AWS Control Tower](migrate-an-aws-member-account-from-aws-organizations-to-aws-control-tower.md)
+ [在 AWS Organizations 中設定程式設計帳戶關閉提醒](set-up-alerts-for-programmatic-account-closures-in-aws-organizations.md)
+ [更多模式](multiaccountstrategy-more-patterns-pattern-list.md)

# 將 AWS 成員帳戶從 遷移 AWS Organizations 至 AWS Control Tower
<a name="migrate-an-aws-member-account-from-aws-organizations-to-aws-control-tower"></a>

*Rodolfo Jr. Cerrada，Amazon Web Services*

## 總結
<a name="migrate-an-aws-member-account-from-aws-organizations-to-aws-control-tower-summary"></a>

此模式說明如何 AWS 帳戶 從 遷移 AWS Organizations，其中 是受管理帳戶管理的成員帳戶 AWS Control Tower。透過在 中註冊帳戶 AWS Control Tower，您可以利用預防性和偵測性控制和功能來簡化您的帳戶控管。如果您的 AWS Organizations 管理帳戶遭到入侵，而且您想要將成員帳戶移至由 管理的新組織，您可能也會想要遷移您的成員帳戶 AWS Control TowerAWS Control Tower。 

AWS Control Tower 提供架構，可結合和整合其他多種功能 AWS Organizations， AWS 服務包括並確保跨多帳戶環境的一致性合規和管理。透過 AWS Control Tower，您可以遵循一組延伸 功能的規定規則和定義 AWS Organizations。例如，您可以使用控制項來確保安全日誌和必要的跨帳戶存取許可已建立，而不會變更。

## 先決條件和限制
<a name="migrate-an-aws-member-account-from-aws-organizations-to-aws-control-tower-prereqs"></a>

**先決條件**
+ 作用中 AWS 帳戶
+ AWS Control Tower 在目標組織中設定 AWS Organizations （如需說明，請參閱 AWS Control Tower 文件中的[設定](https://docs.aws.amazon.com/controltower/latest/userguide/setting-up.html))
+ 的管理員登入資料 AWS Control Tower (**AWSControlTowerAdmins** 群組的成員）
+ 來源的管理員登入資料 AWS 帳戶

**限制**
+ 中的來源管理帳戶 AWS Organizations 必須與 中的目標管理帳戶不同 AWS Control Tower。

**產品版本**
+ AWS Control Tower 2.3 版 (2020 年 2 月） 或更新版本 （請參閱[版本備註](https://docs.aws.amazon.com/controltower/latest/userguide/release-notes.html))

## Architecture
<a name="migrate-an-aws-member-account-from-aws-organizations-to-aws-control-tower-architecture"></a>

下圖說明遷移程序和參考架構。此模式會將 AWS 帳戶 從來源組織遷移到由 管理的目標組織 AWS Control Tower。 

![\[遷移至另一個組織並移至已註冊 OU 之 AWS 帳戶的 AWS Control Tower 註冊程序。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/1fc2c2f0-fa5d-4068-a2b2-9e57cea2aff5/images/0654d242-0faa-4810-9e53-40ef89305b5b.png)


註冊程序包含下列步驟：

1. 目標組織會傳送邀請給帳戶加入組織。 

1. 帳戶接受邀請，並成為目標組織的成員。

1. 帳戶已註冊 AWS Control Tower 並移至已註冊的組織單位 (OU)。（我們建議您檢查 AWS Control Tower 儀表板以確認註冊。) 此時，在註冊的 OU 中啟用的所有控制項都會生效。

## 工具
<a name="migrate-an-aws-member-account-from-aws-organizations-to-aws-control-tower-tools"></a>

**AWS 服務**
+ [AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_introduction.html) 是一種帳戶管理服務，可讓您將多個 合併 AWS 帳戶 到您建立並集中管理的單一實體 (*組織*)。
+ [AWS Control Tower](https://docs.aws.amazon.com/controltower/latest/userguide/what-is-control-tower.html) 整合了其他服務的功能 AWS Organizations，包括 AWS IAM Identity Center和 AWS Service Catalog，以協助您在 中的所有組織和帳戶中大規模強制執行和管理安全、操作和合規的控管規則 AWS 雲端。

## 史詩
<a name="migrate-an-aws-member-account-from-aws-organizations-to-aws-control-tower-epics"></a>

### 邀請帳戶加入新組織 AWS Control Tower
<a name="invite-the-account-to-join-the-new-organization-with-ctower"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 登入 AWS Control Tower。 | 以管理員身分登入 AWS Control Tower 主控台。 目前，無法直接將 AWS 帳戶 從來源組織移動到由 管理的 OU 中的組織 AWS Control Tower。不過，當您將其註冊到已受 管理的 OU AWS 帳戶 時，您可以將 AWS Control Tower 管控擴展到現有的 AWS Control Tower。這就是為什麼您必須在此步驟登入 AWS Control Tower 。 | AWS Control Tower 管理員 | 
| 邀請成員帳戶。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/migrate-an-aws-member-account-from-aws-organizations-to-aws-control-tower.html)確認帳戶轉移不會影響任何應用程式或網路連線。此動作會傳送邀請電子郵件，其中包含成員帳戶的連結。當帳戶管理員遵循連結並接受邀請時，成員帳戶會出現在 **AWS 帳戶**頁面中。如需詳細資訊，請參閱 AWS Organizations 文件中的[管理帳戶邀請](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_accounts_invites.html)。 | AWS Control Tower 管理員 | 
| 測試應用程式和連線。 | 當成員帳戶已註冊到新組織時，它會出現在根目錄中的 OU 中。它也會出現在[AWS Control Tower 主控台](https://console.aws.amazon.com/controltower)中，標記為未註冊帳戶，因為它尚未註冊到 AWS Control Tower 已註冊的 OU。請確認下列內容：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/migrate-an-aws-member-account-from-aws-organizations-to-aws-control-tower.html) | AWS Control Tower 管理員、成員帳戶管理員、應用程式擁有者 | 

### 準備帳戶以進行註冊
<a name="prepare-the-account-for-enrollment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 檢閱控制項並修正任何違規。 | 檢閱目標 OU 中定義的控制項，特別是預防性控制項，並修正任何違規。 設定 AWS Control Tower 登陸區域時，預設會啟用一些[強制性的預防性控制](https://docs.aws.amazon.com/controltower/latest/controlreference/preventive-controls.html)。這些無法停用。您必須先檢閱這些強制性控制項並修正成員帳戶 （手動或使用指令碼），才能註冊帳戶。預防性控制可保持 AWS Control Tower 已註冊帳戶的合規性，並防止違反政策。任何違反預防性控制可能會影響註冊。成功註冊後，偵測到的控制違規會顯示在 AWS Control Tower 儀表板中，如果偵測到的話。它們不會影響註冊程序。如需詳細資訊，請參閱 AWS Control Tower 文件中的[關於控制項](https://docs.aws.amazon.com/controltower/latest/controlreference/controls.html)。 | AWS Control Tower 管理員、成員帳戶管理員 | 
| 修正控制違規後檢查連線問題。 | 在某些情況下，您可能需要關閉特定連接埠或停用服務，以修正控制違規。在您註冊帳戶之前，請確定已修復使用這些連接埠和服務的應用程式。 | 應用程式擁有者 | 

### 將帳戶註冊至 AWS Control Tower
<a name="enroll-the-account-into-ctowerlong"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 登入 AWS Control Tower。 | 登入 [AWS Control Tower 主控台](https://console.aws.amazon.com/controltower)。使用具有管理許可的登入憑證 AWS Control Tower。請勿使用根使用者 （管理帳戶） 登入資料來註冊 AWS Organizations 帳戶。這會顯示錯誤訊息。 | AWS Control Tower 管理員 | 
| 註冊 帳戶。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/migrate-an-aws-member-account-from-aws-organizations-to-aws-control-tower.html)如需詳細資訊，請參閱 AWS Control Tower 文件中的[關於註冊現有帳戶](https://docs.aws.amazon.com/controltower/latest/userguide/enroll-account.html)。 | AWS Control Tower 管理員 | 

### 註冊後驗證帳戶
<a name="verify-the-account-after-enrollment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 驗證帳戶。 | 從 AWS Control Tower中選擇**帳戶**。您剛註冊的帳戶具有**初始註冊**狀態。註冊完成時，其狀態會變更為**已註冊**。 | AWS Control Tower 管理員、成員帳戶管理員 | 
| 檢查控制違規。 | OU 中定義的控制項會自動套用至已註冊的成員帳戶。監控 AWS Control Tower 儀表板是否有違規，並相應地修正。如需詳細資訊，請參閱 AWS Control Tower 文件中的[關於控制項](https://docs.aws.amazon.com/controltower/latest/controlreference/controls.html)。 | AWS Control Tower 管理員、成員帳戶管理員 | 

## 疑難排解
<a name="migrate-an-aws-member-account-from-aws-organizations-to-aws-control-tower-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| 您會收到錯誤訊息：**發生未知錯誤。請稍後再試，或聯絡 AWS Support。** | 當您在 中使用根使用者登入資料 （管理帳戶） AWS Control Tower 註冊新帳戶時，會發生此錯誤。 AWS Service Catalog 無法將帳戶工廠產品組合或產品對應至根使用者，這會導致錯誤訊息。若要修復此錯誤，請使用非根、完整存取的使用者 （管理員） 登入資料來註冊新帳戶。如需如何將管理存取權指派給管理使用者的詳細資訊，請參閱 IAM Identity Center 文件中的[入門](https://docs.aws.amazon.com/singlesignon/latest/userguide/getting-started.html)。 | 
|  AWS Control Tower **活動**頁面會顯示**取得災難性偏離**動作。 | 此動作會反映服務的偏離檢查，並不表示 AWS Control Tower 設定的任何問題。無需採取任何動作。 | 

## 相關資源
<a name="migrate-an-aws-member-account-from-aws-organizations-to-aws-control-tower-resources"></a>

**文件**
+ [術語和概念](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_getting-started_concepts.html) (AWS Organizations 文件）
+ [什麼是 AWS Control Tower？](https://docs.aws.amazon.com/controltower/latest/userguide/)(AWS Control Tower 文件）
+ [從組織移除成員帳戶 ](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_accounts_remove.html#leave-without-all-info)(AWS Organizations 文件）
+ [設定 ](https://docs.aws.amazon.com/controltower/latest/userguide/setting-up.html#setting-up-iam)(AWS Control Tower 文件）

**教學課程和影片**
+ [AWS Control Tower 研討會](https://catalog.workshops.aws/control-tower/) （自行安排進度的研討會）
+ [什麼是 AWS Control Tower？](https://www.youtube.com/watch?v=daLvEb44d5Q)（影片）
+ [在 中佈建使用者 AWS Control Tower](https://www.youtube.com/watch?v=y_n9xN5mg1g)（影片）

# 在 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*

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

適用於 [AWS Organizations](https://aws.amazon.com/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) 裝置的帳戶根電子郵件擁有者。 AWS Organizations 

此模式會在呼叫 `CloseAccount`和 `RemoveAccountFromOrganization` APIs時實作提醒，讓您可以監控這些活動。對於提醒，它使用 [Amazon Simple Notification Service](https://aws.amazon.com/sns/) (Amazon SNS) 主題。您也可以透過 [Webhook](https://api.slack.com/messaging/webhooks) 設定 Slack 通知。

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

**先決條件**
+ 作用中的 AWS 帳戶
+ AWS Organizations 中的組織
+ 在組織的根目錄下存取組織管理帳戶，以建立所需的資源

**限制**
+ 如 [AWS Organizations API 參考](https://docs.aws.amazon.com/organizations/latest/APIReference/API_CloseAccount.html)中所述，`CloseAccount`API 僅允許 10% 的作用中成員帳戶在 30 天的滾動期間內關閉。
+ 當 AWS 帳戶關閉時，其狀態會變更為 SUSPENDED。在此狀態轉換後 90 天內，AWS Support 可以重新開啟帳戶。90 天後，帳戶會永久刪除。
+ 有權存取 AWS Organizations 管理帳戶和 APIs，也可能具有停用這些提醒的許可。如果主要考量是惡意行為，而不是意外刪除，請考慮使用 [IAM 許可界限](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_boundaries.html)保護此模式建立的資源。
+ `CloseAccount `和 的 API 呼叫`RemoveAccountFromOrganization`會在美國東部 （維吉尼亞北部） 區域 (`us-east-1`) 處理。因此，您必須部署此解決方案 `us-east-1` ，才能觀察事件。

## Architecture
<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_tw/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. Amazon EventBridge 已與 AWS CloudTrail 整合，可將這些事件交付至預設事件匯流排。

1. 自訂 Amazon EventBridge 規則符合 AWS Organizations 請求並呼叫 AWS Lambda 函數。

1. Lambda 函數會傳送訊息至 SNS 主題，使用者可以訂閱該主題以進行電子郵件提醒或進一步處理。

1. 如果啟用 Slack 通知，Lambda 函數會傳送訊息至 Slack Webhook。

## 工具
<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 Organizations，您可以透過程式設計方式建立新的 AWS 帳戶並配置資源、分組帳戶來組織工作流程、將政策套用到帳戶或群組以進行控管，以及為所有帳戶使用單一付款方式來簡化計費。
+ [AWS CloudTrail](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-user-guide.html) 會監控和記錄 AWS 基礎設施的帳戶活動，並讓您控制儲存、分析和修復動作。
+ [Amazon Simple Notification Service (Amazon SNS)](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) 是一種全受管傳訊服務application-to-application(A2A) 和application-to-person(A2P) 通訊。

**其他工具**
+ [AWS Lambda Powertools for Python 程式庫](https://docs.powertools.aws.dev/lambda/python/latest/)是一組公用程式，可為 Lambda 函數提供追蹤、記錄、指標和事件處理功能。

**Code**

此模式的程式碼位於 GitHub [AWS Account Closer Notifier](https://github.com/aws-samples/aws-account-closure-notifier) 儲存庫中。

解決方案包含部署此模式架構的 CloudFormation 範本。它使用[適用於 Python 的 AWS Lambda Powertools 程式庫](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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 啟動解決方案堆疊的 CloudFormation 範本。 | 此模式的 CloudFormation 範本位於 [GitHub 儲存庫](https://github.com/aws-samples/aws-account-closure-notifier)的主分支中。它會部署 IAM 角色、EventBridge 規則、Lambda 函數和 SNS 主題。若要啟動範本：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-alerts-for-programmatic-account-closures-in-aws-organizations.html)如需啟動 CloudFormation 堆疊的詳細資訊，請參閱 [AWS 文件](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-create-stack.html)。 | AWS 管理員 | 
| 確認解決方案已成功啟動。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-alerts-for-programmatic-account-closures-in-aws-organizations.html) | AWS 管理員 | 
| 訂閱 SNS 主題。 | （選用） 如果您想要訂閱 SNS 主題：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-alerts-for-programmatic-account-closures-in-aws-organizations.html)如需設定 SNS 通知的詳細資訊，請參閱 [Amazon SNS 文件](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/US_SetupSNS.html)。 | AWS 管理員 | 

### 驗證解決方案
<a name="verify-the-solution"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 將測試事件傳送至預設事件匯流排。 | [GitHub 儲存庫](https://github.com/aws-samples/aws-account-closure-notifier)提供範例事件，您可以傳送到 EventBridge 預設事件匯流排進行測試。EventBridge 規則也會對使用自訂事件來源 的事件做出反應`account.closure.notifier`。您無法使用 CloudTrail 事件來源傳送此事件，因為無法以 AWS 服務傳送事件。若要傳送測試事件：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-alerts-for-programmatic-account-closures-in-aws-organizations.html) | AWS 管理員 | 
| 驗證是否已收到電子郵件通知。 | 檢查訂閱 SNS 主題的信箱以取得通知。您應該會收到一封電子郵件，其中包含已關閉帳戶的詳細資訊，以及執行 API 呼叫的委託人。 | AWS 管理員 | 
| 確認已收到 Slack 通知。 | （選用） 如果您在部署 CloudFormation 範本時為 `SlackWebhookEndpoint` 參數指定 Webhook URL，請檢查映射至 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 Organizations API 參考）
+ [RemoveAccountFromOrganization 動作](https://docs.aws.amazon.com/organizations/latest/APIReference/API_RemoveAccountFromOrganization.html) (AWS Organizations API 參考）
+ [適用於 Python 的 AWS Lambda Powertools](https://docs.powertools.aws.dev/lambda/python/latest/)

# 更多模式
<a name="multiaccountstrategy-more-patterns-pattern-list"></a>

**Topics**
+ [在 上使用登陸區域加速器自動建立帳戶 AWS](automate-account-creation-lza.md)
+ [自動化刪除 AWS CloudFormation 堆疊和相關聯的資源](automate-deletion-cloudformation-stacks-associated-resources.md)
+ [使用 AWS Service Catalog 和 自動化動態管道管理，以在 Gitflow 環境中部署 Hotfix 解決方案 AWS CodePipeline](automate-dynamic-pipeline-management-for-deploying-hotfix-solutions.md)
+ [使用 Amazon DataZone 建置企業資料網格 AWS CDK，以及 AWS CloudFormation](build-enterprise-data-mesh-amazon-data-zone.md)
+ [使用 Amazon CloudWatch Observability Access Manager 集中監控](centralize-monitoring-by-using-amazon-cloudwatch-observability-access-manager.md)
+ [使用 Account Factory for Terraform 管理多個帳戶的許可集](govern-permission-sets-aft.md)
+ [為多帳戶 DevOps 環境實作 Gitflow 分支策略](implement-a-gitflow-branching-strategy-for-multi-account-devops-environments.md)
+ [為多帳戶 DevOps 環境實作 GitHub 流程分支策略](implement-a-github-flow-branching-strategy-for-multi-account-devops-environments.md)
+ [實作多帳戶 DevOps 環境的主體分支策略](implement-a-trunk-branching-strategy-for-multi-account-devops-environments.md)
+ [使用 Terraform 動態管理 AWS 許可集](manage-aws-permission-sets-dynamically-by-using-terraform.md)
+ [AWS 使用 Terraform 在 上建立階層式多區域 IPAM 架構](multi-region-ipam-architecture.md)
+ [在多區域、多帳戶組織中設定 CloudFormation 偏離偵測](set-up-aws-cloudformation-drift-detection-in-a-multi-region-multi-account-organization.md)
+ [在多帳戶 AWS 環境中設定混合網路的 DNS 解析](set-up-dns-resolution-for-hybrid-networks-in-a-multi-account-aws-environment.md)