lib/plugins/aws/package/compile/events/cognitoUserPool/index.js/AwsCompileCognitoUserPoolEvents/compileCognitoUserPoolEvents
class AwsCompileCognitoUserPoolEvents:  compileCognitoUserPoolEvents() {
    const result = this.findUserPoolsAndFunctions();
    const cognitoUserPoolTriggerFunctions = result.cognitoUserPoolTriggerFunctions;
    const userPools = result.userPools;

    // Generate CloudFormation templates for Cognito User Pool changes
    _.forEach(userPools, (poolName) => {
      const currentPoolTriggerFunctions = _.filter(cognitoUserPoolTriggerFunctions, { poolName });
      const userPoolCFResource = this.generateTemplateForPool(
        poolName,
        currentPoolTriggerFunctions
      );

      _.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
        userPoolCFResource);
    });

    // Generate CloudFormation templates for IAM permissions to allow Cognito to trigger Lambda
    _.forEach(cognitoUserPoolTriggerFunctions, (cognitoUserPoolTriggerFunction) => {
      const userPoolLogicalId = this.provider.naming
        .getCognitoUserPoolLogicalId(cognitoUserPoolTriggerFunction.poolName);
      const lambdaLogicalId = this.provider.naming
        .getLambdaLogicalId(cognitoUserPoolTriggerFunction.functionName);

      const permissionTemplate = {
        Type: 'AWS::Lambda::Permission',
        Properties: {
          FunctionName: {
            'Fn::GetAtt': [
              lambdaLogicalId,
              'Arn',
            ],
          },
          Action: 'lambda:InvokeFunction',
          Principal: { 'Fn::Join': ['', ['cognito-idp.', { Ref: 'AWS::URLSuffix' }]] },
          SourceArn: {
            'Fn::GetAtt': [
              userPoolLogicalId,
              'Arn',
            ],
          },
        },
      };
      const lambdaPermissionLogicalId = this.provider.naming
        .getLambdaCognitoUserPoolPermissionLogicalId(cognitoUserPoolTriggerFunction.functionName,
        cognitoUserPoolTriggerFunction.poolName, cognitoUserPoolTriggerFunction.triggerSource);
      const permissionCFResource = {
        [lambdaPermissionLogicalId]: permissionTemplate,
      };
      _.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
        permissionCFResource);
    });
  }