【发布时间】:2021-01-30 12:53:01
【问题描述】:
我如何才能拥有/spaces 的标准端点,但针对不同的 HTTP 方法使用不同的 lambda 函数?
我希望我能够使用辅助函数
addResource(path: string, method: HttpMethods, lambda: lambda.Function) {
const lambdaIntegration: LambdaIntegration = new LambdaIntegration(lambda, { proxy: true });
const resource: Resource = this.restApi.root.addResource(path);
resource.addMethod(method, lambdaIntegration, {
authorizationType: AuthorizationType.IAM,
});
return resource;
}
只需为我要添加的每个资源调用它
// POST - spaces
gateway.addResource('spaces', HttpMethods.POST, new Function(this, 'droppixel-space-post', {
runtime: Runtime.NODEJS_12_X,
code: Code.fromAsset(`${__dirname}/../lambda-fns/space-api/src`),
handler: 'create.handler',
environment: {
TABLE: table.table.tableArn
}
}))
// GET - spaces
gateway.addResource('spaces', HttpMethods.GET, new Function(this, 'droppixel-space-get', {
runtime: Runtime.NODEJS_12_X,
code: Code.fromAsset(`${__dirname}/../lambda-fns/space-api/src`),
handler: 'list.handler',
environment: {
TABLE: table.table.tableArn
}
}))
这仅适用于一个资源,但添加其他资源将失败
There is already a Construct with name 'spaces' in RootResource [Default]
是否有适当的模式可以很好地做到这一点?
【问题讨论】:
标签: typescript aws-cdk