

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

# 在 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/)