Is there a way to have a shared endpoint at /spaces
with different lambda functions for each HTTP method?
I thought about using a helper function
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;
}
And then calling it for each individual 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
}
}))
This approach works fine for one resource, but when adding more, it fails with
There is already a Construct with name 'spaces' in RootResource [Default]
Is there a recommended pattern for handling this situation elegantly?