AWS API Gateway集成URI中引用资源动态参数的CDK配置问题
实现方案
你需要配置API Gateway的请求参数映射规则,完成路径参数到集成URI占位符的关联,具体实现代码如下:
endpt = rest_api.root.add_resource(path_part='endpoint') sport_endpt = endpt.add_resource("{sport}") players_endpt = sport_endpt.add_resource("players") players_endpt.add_method( http_method='GET', # 声明方法需要接收路径中的sport参数 request_parameters={ 'method.request.path.sport': True }, integration=apigw.Integration( type=_apigw.IntegrationType.HTTP, integration_http_method='GET', # URI中直接使用{sport}作为API Gateway运行时占位符,不要用f-string解析 uri=uri + '/service-{sport}/players', # 建立参数映射关系 request_parameters={ 'integration.request.path.sport': 'method.request.path.sport' } ) )
配置说明
- 方法级的
request_parameters配置:声明method.request.path.sport为必填参数,告知API Gateway该方法需要读取路径中的sport动态参数 - 集成级的
request_parameters配置:建立参数映射关系,将用户请求携带的sport参数值传递给集成请求的路径参数 - URI中的
{sport}是API Gateway的运行时占位符,不需要提前赋值,请求到达API Gateway时会自动替换为实际的参数值
后续如果新增其他运动类别的后端服务,只需要部署对应service-xxx的后端服务即可,不需要修改API Gateway的配置,完全符合扩展需求。
内容的提问来源于stack exchange,提问作者Balu
相关产品推荐
相关产品推荐

