本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 自動化區域間對等互連的設定 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)。
限制
Architecture
此模式中描述的代理式 AI 開發方法涉及以下步驟:
定義自動化提示 – Kiro 會收到自然語言提示,詳細說明對等互連需求。
產生自動化指令碼 – Kiro 會根據提供的提示產生 CloudFormation 和 Lambda 指令碼。
部署堆疊 – Kiro 使用 CloudFormation 部署所需的資源。
設定對等 – Kiro 執行 Step Functions 工作流程,其會呼叫 Lambda 函數來建立對等連線並修改路由表。
下圖顯示 Step Functions 工作流程:
工作流程包含下列步驟:
Step Functions 工作流程會呼叫傳輸閘道對等的 Lambda 函數。
工作流程會等待一分鐘。
工作流程會擷取對等狀態,並將其傳送至條件區塊。區塊負責循環。
如果不符合成功條件,工作流程會編碼為進入計時器階段。
如果符合成功條件,Lambda 函數會修改路由表。
Step Functions 工作流程結束。
史詩
| 任務 | Description | 所需的技能 |
|---|
使用特定詳細資訊填入提示預留位置 | 透過取代 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.
將此儲存為 Markdown 檔案。 將此 Markdown 檔案新增至 Kiro 專案以取得內容。
或者,您可以將此新增為參考上述變數的內嵌提示,而無需連接 檔案以取得內容。 | 一般 AWS、網路管理員 |
建立可建立對等附件的 Lambda 函數。 | 在 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.
儲存此檔案並命名為 peer-transit-gateway.py。
| 一般 AWS、網路管理員、提示詞工程 |
建立 Lambda 函數來輪詢對等連接狀態。 | 在 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".
儲存此檔案並命名為 get-transit-gateway-peering-status.py。
| 一般 AWS、網路管理員、提示詞工程 |
建立 Lambda 函數,將靜態路由新增至兩個區域。 | 在 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.
將此檔案儲存並命名為 modify-transit-gateway-routes.py。
| 一般 AWS、網路管理員 |
建立 CloudFormation 範本。 | 輸入下列協同運作提示,以建立 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).
儲存並命名檔案 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、提示詞工程 |
驗證部署。 | | 一般 AWS |
相關資源