Skip to content

Instantly share code, notes, and snippets.

@AntoniusGolly
Last active May 20, 2022 01:39
Show Gist options
  • Save AntoniusGolly/d9c062dd0cc7849480f7eb4a2f6c509a to your computer and use it in GitHub Desktop.
Save AntoniusGolly/d9c062dd0cc7849480f7eb4a2f6c509a to your computer and use it in GitHub Desktop.
LambdaIntegration
import * as iam from 'aws-cdk-lib/aws-iam';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
export interface LambdaIntegrationOnePermissionOnlyOptions extends apigateway.LambdaIntegrationOptions {
restApi: apigateway.IRestApi
}
export class LambdaIntegration extends apigateway.LambdaIntegration {
constructor(handler: lambda.IFunction, options: LambdaIntegrationOnePermissionOnlyOptions) {
super(handler, options);
handler.addPermission('RoutesHandler_ApiGatewayPermissions', {
principal: new iam.ServicePrincipal('apigateway.amazonaws.com'),
action: 'lambda:InvokeFunction',
sourceArn: options.restApi.arnForExecuteApi()
});
}
bind(method: apigateway.Method): apigateway.IntegrationConfig {
const integrationConfig = super.bind(method);
// Remove all AWS::Lambda::Permission on methods
const permissions = method.node.children.filter(c => c instanceof lambda.CfnPermission);
permissions.forEach(p => method.node.tryRemoveChild(p.node.id));
return integrationConfig;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment