View a markdown version of this page

使用 自動化區域間對等互連的設定 AWS Transit Gateway - AWS 方案指引

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

使用 自動化區域間對等互連的設定 AWS Transit Gateway

Ram Kandaswamy,Amazon Web Services

摘要

AWS Transit Gateway 透過中央中樞連接虛擬私有雲端 (VPCs) 和內部部署網路。Transit Gateway 流量不會周遊公有網際網路,這可減少威脅向量,例如常見的入侵和分散式阻斷服務 (DDoS) 攻擊。

如果您需要在兩個或多個 之間通訊 AWS 區域,您可以使用區域間傳輸閘道對等互連,在不同區域中的傳輸閘道之間建立對等互連。不過,使用 Transit Gateway 手動設定區域間對等可能既複雜又耗時。此模式提供使用基礎設施做為程式碼 (IaC) 來設定對等互連的指引。如果您必須重複設定多個區域,並為 AWS 帳戶 多區域組織設定使用此方法。

此模式會設定AWS CloudFormation堆疊,其中包含 Amazon CloudWatch Logs 中的 AWS Step Functions anworkflow、 AWS Lambda functions、 AWS Identity and Access Management (IAM) 角色日誌群組。然後,您可以執行 Step Functions 工作流程,為您的傳輸閘道建立區域間對等互連。

先決條件和限制

先決條件

  • 作用中 AWS 帳戶。

  • 具有程式碼產生功能的 IDE,例如 Kiro

  • Amazon Simple Storage Service (Amazon S3) 儲存貯體及其上傳物件的許可。

  • 在請求和接受區域中建立的傳輸閘道。

  • 在請求和接受區域中建立的 VPCs。使用值為 的addToTransitGateway金鑰標記 VPCstrue

  • 根據您的需求為您的 VPCs設定的安全群組

  • 根據您的需求為您的 VPCs 設定的網路存取控制清單 (ACLs)

限制

  • 只有部分 AWS 區域 支援區域間對等互連。如需支援區域間對等互連的區域完整清單,請參閱AWS Transit Gateway FAQs

Architecture

此模式中描述的代理式 AI 開發方法涉及以下步驟:

  1. 定義自動化提示 – Kiro 會收到自然語言提示,詳細說明對等互連需求。

  2. 產生自動化指令碼 – Kiro 會根據提供的提示產生 CloudFormation 和 Lambda 指令碼。

  3. 部署堆疊 – Kiro 使用 CloudFormation 部署所需的資源。

  4. 設定對等 – Kiro 執行 Step Functions 工作流程,其會呼叫 Lambda 函數來建立對等連線並修改路由表。

下圖顯示 Step Functions 工作流程:

Step Functions 工作流程可呼叫 Lambda 函數來修改傳輸閘道對等的路由表。

工作流程包含下列步驟:

  1. Step Functions 工作流程會呼叫傳輸閘道對等的 Lambda 函數。 

  2. 工作流程會等待一分鐘。

  3. 工作流程會擷取對等狀態,並將其傳送至條件區塊。區塊負責循環。 

  4. 如果不符合成功條件,工作流程會編碼為進入計時器階段。 

  5. 如果符合成功條件,Lambda 函數會修改路由表。

  6. Step Functions 工作流程結束。

工具

  • AWS CloudFormation 可協助您設定 AWS 資源、快速且一致地佈建資源,以及在整個 AWS 帳戶 和 生命週期中管理資源 AWS 區域。 

  • Amazon CloudWatch Logs 可協助您集中所有系統、應用程式的日誌, AWS 服務 以便您可以監控日誌並將其安全地存檔。

  • AWS Identity and Access Management (IAM) 透過控制已驗證並獲授權使用的人員,協助您安全地管理對 AWS 資源的存取。

  • Kiro 是一種代理式 AI 開發工具,可協助您透過規格驅動的開發來建置生產就緒的應用程式。 

  • AWS Lambda 是一項運算服務,可協助您執行程式碼,無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展,因此您只需按使用的運算時間付費。

  • AWS Step Functions 是一種無伺服器協同運作服務,可協助您結合 AWS Lambda 函數和其他 AWS 服務 來建置業務關鍵型應用程式。  

史詩

任務Description所需的技能

使用特定詳細資訊填入提示預留位置

  1. 透過取代 AWS 區域 和 儲存貯體名稱來更新下列提示:

    - Active region: ACTIVE_REGION - Passive region: PASSIVE_REGION - S3_BUCKET : my-lambda-packages-bucket - STACK_NAME : transit-gateway-peering - All Lambda functions use two boto3 EC2 clients at module level (one per region), each with signature_version v4 and retries max_attempts=1 mode=standard. - Print all API responses for CloudWatch logging.
  2. 將此儲存為 Markdown 檔案。

  3. 將此 Markdown 檔案新增至 Kiro 專案以取得內容。

注意

或者,您可以將此新增為參考上述變數的內嵌提示,而無需連接 檔案以取得內容。

一般 AWS、網路管理員

建立可建立對等附件的 Lambda 函數。

  1. 在 Kiro 專案中,輸入下列提示:

    Write a Python Lambda that creates a transit gateway peering attachment from active region to passive region. Both of these regions will be read as environment variables of the Lambda function. Use two boto3 EC2 clients at the module level, one per Region. The handler should describe available transit gateways in both Regions. Then check if a peering attachment already exists on the active side by filtering describe_transit_gateway_attachments for resource-type "peering" and states: available, initiatingRequest, modifying, pendingAcceptance, pending, rejected, and rejecting. Only if zero results, create the peering attachment with tags for both Transit Gateway IDs, then wait 60 seconds (AWS needs this before the passive side can accept), then call accept_transit_gateway_peering_attachment on the passive client. Print all API responses.
  2. 儲存此檔案並命名為 peer-transit-gateway.py

一般 AWS、網路管理員、提示詞工程

建立 Lambda 函數來輪詢對等連接狀態。

  1. 在 Kiro 專案中,輸入下列提示:

    Using the shared context above, write a Python Lambda that polls peering attachment status. Describe available transit gateways in both regions, then call describe_transit_gateway_attachments on the active client filtered by the active Transit Gateway ID only (no resource-type filter). Return exactly `{'status': status}` where status is the State field from the first attachment. Do not return statusCode or body — the Step Function Choice state reads `$.Payload.status` and compares to "available".
  2. 儲存此檔案並命名為 get-transit-gateway-peering-status.py

一般 AWS、網路管理員、提示詞工程

建立 Lambda 函數,將靜態路由新增至兩個區域。

  1. 在 Kiro 專案中,輸入下列提示:

    Using the shared context, write a Python Lambda that adds static routes to both regions' TGW route tables. Describe available transit gateways in both Regions, extract each transit gateway's AssociationDefaultRouteTableId. Discover routable VPCs by filtering describe_vpcs with tag `addtotransitgateway=true` in each region, collecting their CIDRs. Get the peering attachment ID from the active side by filtering on the active route table ID. For each passive CIDR, search the active route table using `route-search.exact-match` — only create the route if none found. For each active CIDR, search the passive route table using `route-search.supernet-of-match` (not exact-match — passive side may have supernet routes) — only create if none found. Both sides use the same peering attachment ID.
  2. 將此檔案儲存並命名為 modify-transit-gateway-routes.py

一般 AWS、網路管理員

建立 CloudFormation 範本。

  1. 輸入下列協同運作提示,以建立 CloudFormation 範本:

    Write a CloudFormation JSON template that deploys: three Lambda functions (peer-transit-gateway with 600s timeout, get-transit-gateway-peering-status with 30s timeout, modify-transit-gateway-routes with 600s timeout), Lambda code from an S3 bucket parameter (no default value — user must supply the bucket name at deploy time), a Step Functions state machine, and CloudWatch log groups with 90-day retention. The state machine flow is: 1. Invoke peer-transit-gateway 2. Wait 20 seconds (attachment needs time after the Lambda's internal 20s sleep + acceptance) 3. Invoke get-transit-gateway-peering-status 4. Choice: if `$.Payload.status` equals "available" → go to step 5, otherwise loop back to step 2 5. Invoke modify-transit-gateway-routes → End Use `Fn::Sub` with Lambda ARN references like `${PeerTransitGateways.Arn}` in the DefinitionString. The polling loop has no max retry — it loops until "available" (typically 3-5 minutes total).
  2. 儲存並命名檔案 transit-gateway-peering.json

AWS DevOps、一般 AWS、提示工程
任務Description所需的技能

使用提示部署 CloudFormation 堆疊。

輸入下列提示:

Using the outputs from Prompts 1-4, package and deploy the full stack. Steps: 1. For each of the three Python files from Prompts 1-3, create a zip named after the file (e.g. peer-transit-gateway.zip that contains peer-transit-gateway.py). 2. Upload all three zips to S3_BUCKET. 3. Deploy the CloudFormation template from Prompt 4 to ACTIVE_REGION with S3BucketName=S3_BUCKET and CAPABILITY_NAMED_IAM. 4. Initiate the Step Function from the deployed stack. Zip file names must match the S3Key values in the template exactly.
AWS DevOps、雲端管理員、一般 AWS、提示詞工程

驗證部署。

  1. 監控 Step Functions 工作流程是否成功完成。如需說明,請參閱 Step Functions 文件

  2. 確認 Transit Gateway 連接對等狀態可用。如需說明,請參閱 Transit Gateway 文件

一般 AWS

相關資源