aws-api-gateway - 使用无服务器将多个服务部署到具有共享路径的 Apigateway
问题描述
文档地址共享路径:
service: service-b
provider:
apiGateway:
restApiId: xxxxxxxxxx
restApiRootResourceId: xxxxxxxxxx
restApiResources:
/reports: xxxxxxxxxx
functions:
...
但是,如何引用资源的 ID(即路径)?在第一项服务中,我有:
Outputs:
apiGatewayRestApiId:
Value:
Ref: ApiGatewayRestApi
Export:
Name: ${self:service}-${opt:stage, 'dev'}-apiGateway-restApiId
apiGatewayRestApiRootResourceId:
Value:
Fn::GetAtt:
- ApiGatewayRestApi
- RootResourceId
Export:
Name: ${self:service}-${opt:stage, 'dev'}-apiGateway-rootResourceId
apiGatewayResourceReports:
Value: !Ref ApiGatewayResource/reports
Export:
Name: ${self:service}-${opt:stage, 'dev'}-apiGateway-reportPath
前两个工作,可以FN::ImportValue
在第二个服务中引用。但是,第三个不起作用。我认为问题在于我必须显式创建资源ApiGatewayResource/reports
,而不是将其创建为第一个服务中函数定义的副作用。但是我该怎么做呢?它不会与函数定义冲突吗?
解决方案
经过一番挣扎,我想到了以下几点:第一个服务应该定义资源路径,但保留网关定义的其余部分。它应该输出相关的ID:
provider:
apiGateway:
restApiResources:
/reports: !Ref ReportPath
...
resources:
Resources:
ReportPath:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId: !Ref ApiGatewayRestApi
ParentId:
Fn::GetAtt: [ ApiGatewayRestApi, RootResourceId ]
PathPart: 'reports'
Outputs:
apiGatewayRestApiId:
Value:
Ref: ApiGatewayRestApi
Export:
Name: ${self:service}-${opt:stage, 'dev'}-apiGateway-restApiId
apiGatewayRestApiRootResourceId:
Value:
Fn::GetAtt:
- ApiGatewayRestApi
- RootResourceId
Export:
Name: ${self:service}-${opt:stage, 'dev'}-apiGateway-rootResourceId
apiGatewayResourceReports:
Value: !Ref ReportPath
Export:
Name: ${self:service}-${opt:stage, 'dev'}-apiGateway-reportPath
第二个服务可以引用所有三个 id:
provider:
apiGateway:
restApiId:
'Fn::ImportValue': crane-mg-reports-${opt:stage, 'dev'}-apiGateway-restApiId
restApiRootResourceId:
'Fn::ImportValue': crane-mg-reports-${opt:stage, 'dev'}-apiGateway-rootResourceId
restApiResources:
/reports:
'Fn::ImportValue': crane-mg-reports-${opt:stage, 'dev'}-apiGateway-reportPath
在这两种情况下,函数定义都可以使用 /reports 前缀定义路径而不会发生冲突。
推荐阅读
- python - pandas 数据框中的 sort_values 函数无法正常工作
- mysql - 如果列中有一个相同的值并更改列名,如何将两行合并为一
- javascript - 纽曼无法读取属性并抛出错误,但邮递员收集运行正常
- google-apps-script - 用于在列中搜索值并返回要在变量中使用的行号的脚本
- c - 需要一种算法来检测振荡数据中的大尖峰
- typescript - 无法使用交互式消息在 slack 中呈现机器人框架提示
- c - 编译linux内核时无法识别list_head
- xcode - 链接器命令失败,退出代码为 1——Newrelic 问题
- r - 将变量名传递给 fre 命令
- aws-glue - 确定 aws 粘合作业内的 aws 区域