addLambda()

in lib/api-construct.js [136:187]


    addLambda(name, method, options) {

        // Create the function
        const f = new lambda.Function(this, name, {
            runtime: lambda.Runtime.NODEJS_14_X,
            code: lambda.Code.fromAsset("lambda/dist"),
            handler: name + ".handler",
            vpc: options.vpc, 
            timeout: cdk.Duration.seconds(30),
            memorySize: 1536,
            environment: options.envVars,
            securityGroups: [options.lambdaSecurityGroup],
        })

        const methodOptions = {}
        if (options.auth) {
            methodOptions.authorizer = options.auth
            methodOptions.authorizationType = apigw.AuthorizationType.COGNITO
        }

        // Add the resource to the API
        const r = options.api.root.addResource(name)
        let resourceId
        switch (method) {
            case "any":
                r.addProxy({
                    defaultIntegration: new apigw.LambdaIntegration(f),
                    anyMethod: true,
                    defaultMethodOptions: methodOptions,
                })
                break
            case "delete": // e.g. DELETE /node/{id}
                resourceId = r.addResource("{id}")
                resourceId.addMethod("DELETE", new apigw.LambdaIntegration(f), methodOptions)
                break
            case "get": // e.g. GET /node/id
                resourceId = r.addResource("{id}")
                resourceId.addMethod("GET", new apigw.LambdaIntegration(f), methodOptions)
                break
            case "get-no-id": // special case for gets that don't have an id
                r.addMethod("get", new apigw.LambdaIntegration(f), methodOptions)
                break
            default:
                r.addMethod(method, new apigw.LambdaIntegration(f), methodOptions)
                break
        }

        // Allow the lambda function to access the Neptune cluster
        options.cluster.grantConnect(f)

        return f
    }