View a markdown version of this page

使用自动设置区域间对等互连 AWS Transit Gateway - AWS 规范指引

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用自动设置区域间对等互连 AWS Transit Gateway

Ram Kandaswamy,Amazon Web Services

Summary

AWS Transit Gateway通过中央集线器连接虚拟私有云 (VPCs) 和本地网络。Transit Gateway 流量不会通过公共互联网,从而减少了威胁载体,例如常见漏洞利用和分布式拒绝服务攻击。DDo

如果您需要在两个或多个公交网关之间进行通信 AWS 区域,则可以使用区域间 Transit Gateway 对等互连在不同区域的公交网关之间建立对等连接。但是,使用 Transit Gateway 手动配置区域间对等互连可能既复杂又耗时。此模式为使用基础设施即代码 (IaC) 设置对等互连提供了指导。如果您必须重复配置多个区域以及 AWS 账户 多区域组织设置,则可以使用这种方法。

此模式在 Amazon CloudWatch Logs 中设置了一个包含 AWS Step Functions 工作流程、 AWS Lambda 函数、 AWS Identity and Access Management (IAM) 角色日志组AWS CloudFormation堆栈。然后,您可以运行 Step Functions 工作流程,为您的公交网关创建区域间对等连接。

先决条件和限制

先决条件

限制

  • 只有部分 AWS 区域 支持跨区域对等。有关支持区域间对等互连的区域的完整列表,请参阅。AWS Transit Gateway FAQs

架构

此模式中描述的代理人工智能开发方法包括以下步骤:

  1. 定义自动化提示 — Kiro 会收到自然语言提示,其中详细说明了对等互连要求。

  2. 生成自动化脚本 — Kiro 根据提供的提示生成 CloudFormation 和 Lambda 脚本。

  3. 部署堆栈 — Kiro CloudFormation 用于部署所需的资源。

  4. 设置对等 — Kiro 运行 Step Functions 工作流程,该工作流程调用 Lambda 函数来创建对等连接和修改路由表。

下图显示了 Step Functions 的工作流程:

Step Functions 工作流调用 Lambda 函数来修改中转网关对等互连的路由表。

工作流包含以下步骤:

  1. Step Functions 工作流程为 Transit Gateway 对等互连调用 Lambda 函数。 

  2. 工作流程等待一分钟。

  3. 工作流程检索对等互连状态并将其发送到条件块。数据块负责循环。 

  4. 如未满足成功条件,则对工作流进行编码以进入计时器阶段。 

  5. 如果满足成功条件,Lambda 函数将修改路由表。

  6. Step Functions 工作流程结束。

工具

  • AWS CloudFormation帮助您设置 AWS 资源,快速一致地配置资源,并在和的整个 AWS 账户 生命周期中对其进行管理 AWS 区域。 

  • Amazon CloudWatch Lo gs 可帮助您集中所有系统和应用程序的日志, AWS 服务 这样您就可以监控它们并安全地将其存档。

  • AWS Identity and Access Management(IAM) 通过控制谁经过身份验证并有权使用 AWS 资源,从而帮助您安全地管理对资源的访问权限。

  • Kiro 是一款代理人工智能开发工具,可帮助您通过规范驱动的开发来构建生产就绪型应用程序。 

  • AWS Lambda 是一项计算服务,可帮助您运行代码,无需预调配或管理服务器。它只在需要时运行您的代码,并自动进行扩展,因此您只需为使用的计算时间付费。

  • AWS Step Functions是一项无服务器编排服务,可帮助您组合 AWS Lambda 功能和其他功能 AWS 服务 来构建关键业务应用程序。  

操作说明

Task说明所需技能

用具体细节填充提示占位符

  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、网络管理员、Prompt 工程

创建用于轮询对等连接状态的 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、网络管理员、Prompt 工程

创建一个 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、General AWS、Prompt 工程设计
Task说明所需技能

使用提示部署 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、Prompt 工程

验证部署。

  1. 监控 Step Functions 工作流程是否成功完成。有关说明,请参阅 Ste p Functions 文档

  2. 验证 Transit Gateway 附件对等互连状态是否可用。有关说明,请参阅 T ransit Gateway 文档

常规 AWS

相关资源