public addResource()

in lib/resource-handler.ts [40:107]


    public addResource(
        resourceName: string,
        verb: string,
        requireAuth: boolean, 
        grantAccess: (f: lambda.Function) => any) {

        let resourceId: string | undefined;

        // Perform *very* basic path handling
        if (resourceName.indexOf('/') > -1) {
            const tokens = resourceName.split('/');
            if (tokens.length > 2) {
                throw Error('Multiple parameters are not supported');
                // TODO - This could be improved with {proxy+}
            }
            resourceName = tokens[0];
            resourceId = tokens[1];
        } else {
            resourceId = undefined;
        }

        const lambdaName = `${resourceName}-${verb}`;

        const lf = new lambda.Function(this.parent, lambdaName, {
            runtime: lambda.Runtime.NODEJS_12_X,
            code: lambda.Code.fromAsset(this.lambdaFunctionDirectory),
            handler: `${resourceName}-${verb}.handler`,
            memorySize: 1536,
            timeout: cdk.Duration.minutes(5),
            description: `${this.stackName} ${resourceName} ${verb}`,
            environment: this.envVars
        });

        grantAccess(lf);

        let resource = this.api.root.getResource(resourceName);
        if (!resource) {
            resource = this.api.root.addResource(resourceName);
        }

        // If the resource is something like /foo/{fooId}
        let idResource;
        if (resourceId) {
            idResource = resource.getResource(resourceId);
        }

        if (idResource) {
            // We already defined this resource, for example, 
            // defined /foo/{fooId} GET and now we are defining DELETE 
            resource = idResource;
        } else {
            // We have not yet defined the {fooId} part of the path
            if (resourceId !== undefined) {
                resource = resource.addResource(resourceId);
            }
        }

        let options = {};
        if (requireAuth) {
            options = {
                authorizer: { authorizerId: this.cfnAuthorizer.ref },
                authorizationType: AuthorizationType.COGNITO,
            };
        }

        resource.addMethod(verb, new apigw.LambdaIntegration(lf), options);

    }