function generateStorageCFNForAdditionalLambda()

in packages/amplify-category-predictions/provider-utils/awscloudformation/assets/identifyCFNGenerate.js [148:342]


function generateStorageCFNForAdditionalLambda(storageCFNFile, functionName, prefixForAdminTrigger) {
  storageCFNFile.Parameters[`function${functionName}Arn`] = {
    Type: 'String',
    Default: `function${functionName}Arn`,
  };

  storageCFNFile.Parameters[`function${functionName}Name`] = {
    Type: 'String',
    Default: `function${functionName}Name`,
  };

  storageCFNFile.Parameters[`function${functionName}LambdaExecutionRole`] = {
    Type: 'String',
    Default: `function${functionName}LambdaExecutionRole`,
  };

  storageCFNFile.Parameters.triggerFunction = {
    Type: 'String',
  };

  storageCFNFile.Parameters.adminTriggerFunction = {
    Type: 'String',
  };

  storageCFNFile.Resources.S3Bucket.DependsOn.push('AdminTriggerPermissions');

  // Modify existing notification configuration here//

  const lambdaConfigurations = [];
  storageCFNFile.Resources.S3Bucket.Properties.NotificationConfiguration.LambdaConfigurations.forEach(triggers => {
    if (!triggers.Filter) {
      lambdaConfigurations.push(
        addObjectKeys(triggers, {
          Filter: {
            S3Key: {
              Rules: [
                {
                  Name: 'prefix',
                  Value: {
                    'Fn::Join': [
                      '',
                      [
                        'protected/',
                        {
                          Ref: 'AWS::Region',
                        },
                      ],
                    ],
                  },
                },
              ],
            },
          },
        }),
      );
      lambdaConfigurations.push(
        addObjectKeys(triggers, {
          Filter: {
            S3Key: {
              Rules: [
                {
                  Name: 'prefix',
                  Value: {
                    'Fn::Join': [
                      '',
                      [
                        'private/',
                        {
                          Ref: 'AWS::Region',
                        },
                      ],
                    ],
                  },
                },
              ],
            },
          },
        }),
      );
      lambdaConfigurations.push(
        addObjectKeys(triggers, {
          Filter: {
            S3Key: {
              Rules: [
                {
                  Name: 'prefix',
                  Value: {
                    'Fn::Join': [
                      '',
                      [
                        'public/',
                        {
                          Ref: 'AWS::Region',
                        },
                      ],
                    ],
                  },
                },
              ],
            },
          },
        }),
      );
    } else {
      lambdaConfigurations.push(triggers);
    }
  });

  lambdaConfigurations.push(
    {
      Event: 's3:ObjectCreated:*',
      Filter: {
        S3Key: {
          Rules: [
            {
              Name: 'prefix',
              Value: prefixForAdminTrigger,
            },
          ],
        },
      },
      Function: {
        Ref: `function${functionName}Arn`,
      },
    },
    {
      Event: 's3:ObjectRemoved:*',
      Filter: {
        S3Key: {
          Rules: [
            {
              Name: 'prefix',
              Value: prefixForAdminTrigger,
            },
          ],
        },
      },
      Function: {
        Ref: `function${functionName}Arn`,
      },
    },
  );

  storageCFNFile.Resources.S3Bucket.Properties.NotificationConfiguration.LambdaConfigurations = lambdaConfigurations;

  storageCFNFile.Resources.AdminTriggerPermissions = {
    Type: 'AWS::Lambda::Permission',
    Properties: {
      Action: 'lambda:InvokeFunction',
      FunctionName: {
        Ref: `function${functionName}Name`,
      },
      Principal: 's3.amazonaws.com',
      SourceAccount: {
        Ref: 'AWS::AccountId',
      },
      SourceArn: {
        'Fn::Join': [
          '',
          [
            'arn:aws:s3:::',
            {
              'Fn::If': [
                'ShouldNotCreateEnvResources',
                {
                  Ref: 'bucketName',
                },
                {
                  'Fn::Join': [
                    '',
                    [
                      {
                        Ref: 'bucketName',
                      },
                      '-',
                      {
                        Ref: 'env',
                      },
                    ],
                  ],
                },
              ],
            },
          ],
        ],
      },
    },
  };

  storageCFNFile.Resources.S3TriggerBucketPolicy.Properties.Roles.push({
    Ref: `function${functionName}LambdaExecutionRole`,
  });

  return storageCFNFile;
}