

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

# 使用 AWS Organizations 自動標記 Transit Gateway 連接
<a name="tag-transit-gateway-attachments-automatically-using-aws-organizations"></a>

*Richard Milner-Watts、Haris Bin Ayub 和 John Capps，Amazon Web Services*

## 總結
<a name="tag-transit-gateway-attachments-automatically-using-aws-organizations-summary"></a>

在 Amazon Web Services (AWS) 上，您可以使用 [AWS Resource Access Manager](https://aws.amazon.com/ram/) 跨[AWS Transit Gateway](https://aws.amazon.com/transit-gateway/) AWS 帳戶 邊界共用。不過，當您跨帳戶邊界建立 Transit Gateway 連接時，會建立沒有名稱標籤的連接。這可能會讓識別連接耗時。 

此解決方案提供自動化機制，可收集由 管理之組織內帳戶的每個 Transit Gateway 連接的相關資訊[AWS Organizations](https://aws.amazon.com/organizations/)。程序包括從 Transit Gateway [路由表中查詢無類別網域間](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing)路由 (CIDR) 範圍。解決方案接著會將 形式的 Name 標籤`<CIDR-range>-<AccountName>`套用至擁有傳輸閘道之帳戶內的附件。

此解決方案可與解決方案程式庫中的 [Serverless Transit Network Orchestrator](https://aws.amazon.com/solutions/implementations/serverless-transit-network-orchestrator/) 等 AWS 解決方案搭配使用。Serverless Transit Network Orchestrator 可大規模自動建立 Transit Gateway 附件。

## 先決條件和限制
<a name="tag-transit-gateway-attachments-automatically-using-aws-organizations-prereqs"></a>

**先決條件**
+ 作用中 AWS 帳戶
+ 包含所有相關帳戶 AWS Organizations 的組織
+ 在組織的根目錄下存取組織管理帳戶，以建立 required AWS Identity and Access Management (IAM) 角色
+ 共用網路成員帳戶，其中包含與組織共用並具有附件的一或多個傳輸閘道

## Architecture
<a name="tag-transit-gateway-attachments-automatically-using-aws-organizations-architecture"></a>

的下列螢幕擷取畫面 AWS 管理主控台 顯示沒有相關聯名稱標籤的 Transit Gateway 附件範例，以及具有此解決方案產生之名稱標籤的兩個 Transit Gateway 附件範例。產生的名稱標籤結構為 `<CIDR-range>-<AccountName>`。

![\[主控台顯示不含名稱標籤的附件，以及兩個含名稱標籤的附件。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/4b10dfec-43be-4337-9945-c64df921934a/images/7e7d4a47-f07a-4708-8022-a1d22855bb5d.png)


 

此解決方案使用 [AWS CloudFormation](https://aws.amazon.com/cloudformation/) 部署[AWS Step Functions](https://aws.amazon.com/step-functions/)工作流程，管理所有已設定之 Transit Gateway Name 標籤的建立 AWS 區域。工作流程會叫用 [AWS Lambda](https://aws.amazon.com/lambda/)函數，以執行基礎任務。

解決方案從中取得帳戶名稱後 AWS Organizations，Step Functions 狀態機器會取得所有 Transit Gateway 連接 IDs。這些是由區域平行處理。此處理包括查詢每個附件的 CIDR 範圍。CIDR 範圍是透過搜尋區域內的 Transit Gateway 路由表來取得相符的 Transit Gateway 連接 ID。如果所有必要資訊都可用，解決方案會將名稱標籤套用至附件。解決方案不會覆寫任何現有的名稱標籤。

解決方案會按照由 [Amazon EventBridge](https://aws.amazon.com/eventbridge/) 事件控制的排程執行。事件會在 UTC 每天上午 6：00 啟動解決方案。

**目標技術堆疊**
+ Amazon EventBridge
+ AWS Lambda
+ AWS Organizations
+ AWS Transit Gateway
+ Amazon Virtual Private Cloud (Amazon VPC)
+ AWS X-Ray

**目標架構**

下圖顯示解決方案架構和工作流程。

![\[跨共用聯網和組織管理帳戶的九個步驟程序。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/4b10dfec-43be-4337-9945-c64df921934a/images/873cc89f-c6e3-43cd-94ed-59b6ea2b8d49.png)


1. 排程事件會啟動規則。

1. EventBridge 規則會啟動 Step Functions 狀態機器。

1. 狀態機器會叫用 `tgw-tagger-organizations-account-query` Lambda 函數。

1. `tgw-tagger-organizations-account-query` Lambda 函數會擔任組織管理帳戶中的角色。

1. `tgw-tagger-organizations-account-query` Lambda 函數會呼叫 Organizations API 來傳回 AWS 帳戶 中繼資料。

1. 狀態機器會叫用 `tgw-tagger-attachment-query` Lambda 函數。

1. 對於每個區域，狀態機器會並行叫用 `tgw-tagger-rtb-query` Lambda 函數來讀取每個附件的 CIDR 範圍。

1. 對於每個區域，狀態機器會平行叫用 `tgw-tagger-attachment-tagger`****Lambda 函數。

1. 系統會為共用網路帳戶中的 Transit Gateway 附件建立名稱標籤。

**自動化和擴展**

解決方案會平行處理每個區域，以減少執行的總持續時間。

## 工具
<a name="tag-transit-gateway-attachments-automatically-using-aws-organizations-tools"></a>

**AWS 服務**
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 透過將基礎設施視為程式碼，提供建立相關 AWS 和第三方資源集合模型、快速一致地佈建它們，以及在整個生命週期中管理它們的方法。
+ [Amazon CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html) 可協助您 AWS 即時監控 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 Step Functions](https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html) 是一種低程式碼視覺化工作流程服務，用於協調 AWS 服務、自動化業務流程和建置無伺服器應用程式。工作流程會管理故障、重試、平行化、服務整合和可觀測性，讓開發人員可以專注於更高價值的商業邏輯。
+ [AWS Transit Gateway](https://aws.amazon.com/transit-gateway/) 透過中央中樞連接 VPCs 和內部部署網路。這可簡化您的網路，並結束複雜的互連關係。它充當雲端路由器，因此每個新連線只會進行一次。
+ [Amazon Virtual Private Cloud (Amazon VPC)](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) 是一種在您定義的邏輯隔離虛擬網路中啟動 AWS 資源的服務。
+ [AWS X-Ray](https://docs.aws.amazon.com/xray/latest/devguide/aws-xray.html) 會收集應用程式提供的請求相關資料，並提供可用來檢視、篩選和深入了解該資料的工具，以識別問題和最佳化的機會。

**Code**

此解決方案的原始程式碼可在 [Transit Gateway Attachment Tagger](https://github.com/aws-samples/tgw-attachment-tagger) GitHub 儲存庫中使用。儲存庫包含下列檔案：
+ `tgw-attachment-tagger-main-stack.yaml` 會在共用網路帳戶中建立所有資源以支援此解決方案。
+ `tgw-attachment-tagger-organizations-stack.yaml`****在組織的管理帳戶中建立角色。

## 史詩
<a name="tag-transit-gateway-attachments-automatically-using-aws-organizations-epics"></a>

### 部署主要解決方案堆疊
<a name="deploy-the-main-solution-stack"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 收集必要的先決條件資訊。 | 若要設定從 Lambda 函數到 AWS Organizations API 的跨帳戶存取權，您需要組織管理帳戶的帳戶 ID。****建立兩個 CloudFormation 堆疊的順序很重要。您必須先將資源部署到共用網路帳戶。在將資源部署到組織的管理帳戶中之前，共用網路帳戶中的角色必須已存在。如需詳細資訊，請參閱 [AWS 文件](https://docs.amazonaws.cn/en_us/IAM/latest/UserGuide/id_roles_create_for-user.html)。 | DevOps 工程師 | 
| 啟動主要解決方案堆疊的 CloudFormation 範本。 | 主要解決方案堆疊的範本將部署 IAM 角色、Step Functions 工作流程、Lambda 函數和 Amazon CloudWatch 事件。開啟 AWS 管理主控台 共用網路帳戶的 ，然後開啟 ：&CFN 主控台。 使用`tgw-attachment-tagger-main-stack.yaml` 範本和下列值建立堆疊： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/tag-transit-gateway-attachments-automatically-using-aws-organizations.html)如需啟動 CloudFormation 堆疊的詳細資訊，請參閱 [AWS 文件](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-create-stack.html)。 | DevOps 工程師 | 
| 確認解決方案已成功啟動。 | 等待 CloudFormation 堆疊達到 **CREATE\$1COMPLETE** 狀態。這應該需要不到一分鐘的時間。開啟 Step Functions 主控台，並確認已建立名為 **tgw-attachment-tagger-state-machine** 的新狀態機器。 | DevOps 工程師 | 

### 部署 AWS Organizations 堆疊
<a name="deploy-the-aws-organizations-stack"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 收集必要的先決條件資訊。 | 若要設定從 Lambda 函數到 AWS Organizations API 的跨帳戶存取權，您需要共用網路帳戶的帳戶 ID。 | DevOps 工程師 | 
| 啟動 Organizations 堆疊的 CloudFormation 範本 | AWS Organizations 堆疊的範本將在組織的管理帳戶中部署 IAM 角色。 存取組織管理帳戶的 AWS 主控台。然後開啟 CloudFormation 主控台。 使用`tgw-attachment-tagger-organizations-stack.yaml` 範本和下列值建立堆疊：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/tag-transit-gateway-attachments-automatically-using-aws-organizations.html)對於其他堆疊建立選項，請使用預設值。 | DevOps 工程師 | 
| 確認解決方案已成功啟動。 | 等待 CloudFormation 堆疊達到 **CREATE\$1COMPLETE** 狀態。這應該需要不到一分鐘的時間。開啟 AWS Identity and Access Management (IAM) 主控台，並確認已建立名為 **tgw-attachment-tagger-organization-query-role** 的新角色。 | DevOps 工程師 | 

### 驗證解決方案
<a name="verify-the-solution"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 執行狀態機器。 | 開啟共用網路帳戶的 Step Functions 主控台，然後在導覽窗格中選擇**狀態機器**。選取狀態機器 **tgw-attachment-tagger-state-machine**，然後選擇**開始執行**。 由於解決方案不會使用此狀態機器的輸入，因此您可以使用預設值。<pre>{<br />    "Comment": "Insert your JSON here"<br />}</pre>選擇 **Start Execution (開始執行)**。 | DevOps 工程師 | 
| 觀看狀態機器直到完成。 | 在開啟的新頁面上，您可以觀看狀態機器執行。持續時間取決於要處理的 Transit Gateway 附件數量。在此頁面上，您可以檢查狀態機器的每個步驟。您可以在狀態機器中檢視各種任務，並遵循 Lambda 函數的 CloudWatch 日誌連結。對於在映射中平行執行的任務，您可以使用**索引**下拉式清單來檢視每個區域的特定實作。 | DevOps 工程師 | 
| 驗證 Transit Gateway 連接標籤。 | 開啟共用網路帳戶的 VPC 主控台，然後選擇**傳輸閘道附件**。 在 主控台上，為符合條件的附件提供名稱標籤 （附件會傳播到 Transit Gateway 路由表，而資源擁有者是組織的成員）。 | DevOps 工程師 | 
| 驗證 CloudWatch 事件啟動。 | 等待 CloudWatch 事件啟動。這是排程為 06：00 UTC。 然後開啟共用網路帳戶的 Step Functions 主控台，然後在導覽窗格中選擇**狀態機器**。選取狀態機器 **tgw-attachment-tagger-state-machine**。確認解決方案在 UTC 的 06：00 執行。 | DevOps 工程師 | 

## 相關資源
<a name="tag-transit-gateway-attachments-automatically-using-aws-organizations-resources"></a>
+ [AWS Organizations](https://aws.amazon.com/organizations/)
+ [AWS Resource Access Manager](https://aws.amazon.com/ram/)
+ [無伺服器傳輸網路協調器](https://aws.amazon.com/solutions/implementations/serverless-transit-network-orchestrator/)
+ [建立 IAM 角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html)
+ [在 AWS CloudFormation 主控台上建立堆疊](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-create-stack.html)