You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python版AWS CDK创建带动态URI的API Gateway端点?

动态URI配置实现方案

你需要同时配置方法请求参数、集成请求参数映射两个部分,即可完成动态路径参数的自动替换,具体配置如下:

核心配置逻辑

  • 先在API方法的配置中声明需要接收my_dynamic_endpoint路径参数
  • 在Integration配置中添加参数映射规则,将API侧接收的路径参数绑定到集成URI的对应变量上

完整代码示例

import aws_cdk.aws_apigateway as _apigw

# 此处省略你已有的API实例、my_uri变量等初始化逻辑
# 1. 定义带动态路径参数的API资源
my_service_res = api.root.add_resource("my_service")
dynamic_endpoint_res = my_service_res.add_resource("{my_dynamic_endpoint}")

# 2. 配置HTTP集成
http_integration = _apigw.Integration(
    type=_apigw.IntegrationType.HTTP,
    integration_http_method="GET",
    # URI中直接写入API Gateway识别的占位变量,Python侧保留{}符号即可
    uri=my_uri + "/my_service/{my_dynamic_endpoint}",
    # 配置参数映射:将API侧路径参数映射到集成URI的变量
    request_parameters={
        "integration.request.path.my_dynamic_endpoint": "method.request.path.my_dynamic_endpoint"
    }
)

# 3. 给动态资源绑定GET方法,声明参数为必填
dynamic_endpoint_res.add_method(
    "GET",
    http_integration,
    request_parameters={
        "method.request.path.my_dynamic_endpoint": True
    }
)

效果说明

配置完成后,当你请求API的/my_service/football、/my_service/baseball路径时,API Gateway会自动将集成请求的URI替换为对应的{my_uri}/my_service/football、{my_uri}/my_service/baseball,无需额外修改代码即可支持任意符合规则的动态参数值。

内容的提问来源于stack exchange,提问作者Balu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 13:15:05