DEV Community

Discussion on: Adding a Cognito authorizer to API Gateway with the AWS CDK

Collapse
 
johnnyclutch profile image
Glenn A Bullock

Any idea how to add an Authorizer to a "proxied" LambdaRestApi object? One that refers all the calling paths to the lambda and allows the lambda to handle the "routing". (I'm using Nestjs in the lambda.).

I tried:

this._authLambda = new Function(this, ${ options.stackName }-auth-lambda,
{
runtime: Runtime.NODEJS_12_X,
code: Code.fromAsset("./lambdas/auth"),
handler: "main.handler",
logRetention: 7,
timeout: Duration.seconds(20),
layers: [ options.nestJsLayer ]
}
);

this._gatewayRestAPI = new LambdaRestApi(this, ${ options.stackName }-auth-api, {
handler: this._authLambda,
});

const authIntegration = new LambdaIntegration(this._authLambda);

const path = this._gatewayRestAPI.root.addResource('*');
path.addMethod("POST", authIntegration,{
authorizer: options.customAuthorizer
});

But the cdk says: "Cannot call 'addResource' on a proxying LambdaRestApi; set 'proxy' to false". I guess I could go through and add the routes of my nestjs app to the rest api, but I hate having two places to maintain them.

Collapse
 
ara225 profile image
Anna Aitchison

Hmm, been a while since I've dealt with the CDK, but I'd think there'd be another method on the REST API to do that. Alternatively, I'd suggest using the CDK to generate the cloudformation template without the offending piece, compare that to a cloudformation template doing what you want to do and use add_property_override to correct the CDK version