Last active
May 20, 2022 01:39
-
-
Save AntoniusGolly/d9c062dd0cc7849480f7eb4a2f6c509a to your computer and use it in GitHub Desktop.
LambdaIntegration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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