以编程方式将多个事件通知添加到s3存储桶

时间:2019-03-09 17:14:41

标签: amazon-web-services amazon-s3 aws-lambda amazon-cloudformation

我想使用以下cloudformation模板在单个现有S3存储桶上创建多个事件通知。但是,即使我在BucketConfiguration资源下指定了另一个“ LambdaFunctionConfigurations”,我也只会看到在S3存储桶上创建的一个事件。我也尝试过创建另一个BucketConfiguration资源,该资源配置为单独的事件,没有运气。我正在寻找任何提示或建议,以帮助我指出正确的方向。从我收集的内容中,s3.putBucketNotification方法将清除所有现有的事件通知配置。请记住,CF模板确实会创建一个事件通知(我正在尝试创建多个事件通知)。

以下模板的模型: Create a Lambda notification in an S3 bucket with CloudFormation

Description: >-
  Create an event notification for an existing S3 bucket
Parameters:
  BucketName:
    Description: S3 Bucket name (must already exist)
    Type: String
Resources:
  BucketConfiguration:
    Type: 'Custom::S3BucketConfiguration'
    DependsOn:
      - BucketPermission
      - NotificationBucketPolicy
    Properties:
      ServiceToken: !GetAtt S3BucketConfiguration.Arn
      Bucket: !Ref BucketName
      NotificationConfiguration:
        LambdaFunctionConfigurations:
          - Events:
              - 's3:ObjectCreated:*'
            LambdaFunctionArn: MyLambdaArn
    Metadata:
      'AWS::CloudFormation::Designer':
        id: 606b322f-42fa-4d20-bae4-53374d7ad7ba
  S3BucketConfiguration:
    Type: 'AWS::Lambda::Function'
    Properties:
      Description: S3 Object Custom Resource
      Handler: index.handler
      Role: !GetAtt LambdaExecutionRole.Arn
      Code:
        ZipFile: !Sub |
          var response = require('cfn-response');
          var AWS = require('aws-sdk');
          var s3 = new AWS.S3();
          exports.handler = function(event, context) {
            var respond = (e) => response.send(event, context, e ? response.FAILED : response.SUCCESS, e ? e : {});
            process.on('uncaughtException', e=>failed(e));
            var params = event.ResourceProperties;
            delete params.ServiceToken;
            if (event.RequestType === 'Delete') {
              params.NotificationConfiguration = {};
              s3.putBucketNotificationConfiguration(params).promise()
                .then((data)=>respond())
                .catch((e)=>respond());
            } else {
              s3.putBucketNotificationConfiguration(params).promise()
                .then((data)=>respond())
                .catch((e)=>respond(e));
            }
          };
      Timeout: 30
      Runtime: nodejs6.10
    Metadata:
      'AWS::CloudFormation::Designer':
        id: 1cefd553-f888-4b3b-8184-d96932a29227
  BucketPermission:
    Type: 'AWS::Lambda::Permission'
    Properties:
      Action: 'lambda:InvokeFunction'
      Principal: s3.amazonaws.com
      SourceAccount: !Ref 'AWS::AccountId'
      SourceArn: !Sub 'arn:aws:s3:::${BucketName}'
    Metadata:
      'AWS::CloudFormation::Designer':
        id: 29f90f84-cfd0-43d2-8c2b-c173ec96c409
  LambdaExecutionRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      Path: /
      ManagedPolicyArns:
        - 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
      Policies:
        - PolicyName: S3Policy
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - 's3:PutObject'
                  - 'S3:DeleteObject'
                Resource: !Sub 'arn:aws:s3:::${BucketName}'
    Metadata:
      'AWS::CloudFormation::Designer':
        id: 3556f770-b7cd-4ac1-8afa-62a0319721b8
  NotificationBucketPolicy:
    Type: 'AWS::S3::BucketPolicy'
    Properties:
      Bucket: !Ref BucketName
      PolicyDocument:
        Statement:
          - Effect: Allow
            Action:
              - 's3:PutBucketNotification'
            Resource: !Sub 'arn:aws:s3:::${BucketName}'
            Principal:
              AWS: !GetAtt LambdaExecutionRole.Arn
    Metadata:
      'AWS::CloudFormation::Designer':
        id: 1f82086e-0d71-4731-8173-e3b8ee0da4dd
Metadata:
  'AWS::CloudFormation::Designer':
    3556f770-b7cd-4ac1-8afa-62a0319721b8:
      size:
        width: 60
        height: 60
      position:
        x: 60
        'y': 90
      z: 1
      embeds: []
    1f82086e-0d71-4731-8173-e3b8ee0da4dd:
      size:
        width: 60
        height: 60
      position:
        x: 180
        'y': 90
      z: 1
      embeds: []
    29f90f84-cfd0-43d2-8c2b-c173ec96c409:
      size:
        width: 60
        height: 60
      position:
        x: 410
        'y': 90
      z: 1
      embeds: []
    1cefd553-f888-4b3b-8184-d96932a29227:
      size:
        width: 60
        height: 60
      position:
        x: 300
        'y': 190
      z: 1
      embeds: []
    606b322f-42fa-4d20-bae4-53374d7ad7ba:
      size:
        width: 60
        height: 60
      position:
        x: 300
        'y': 90
      z: 1
      embeds: []
      dependson:
        - 29f90f84-cfd0-43d2-8c2b-c173ec96c409
        - 1f82086e-0d71-4731-8173-e3b8ee0da4dd

1 个答案:

答案 0 :(得分:2)

您是正确的,不可能为同一“触发器”定义多个事件。

例如,在S3控制台中,我成功定义了:

  • 同一事件的多个规则,但前缀不同
  • 相同前缀不同事件
  • 的多个规则

但是,我不能相同事件相同前缀定义多个规则。错误消息是:

  

配置定义不明确。如果同一事件类型的前缀重叠,则两个规则中的后缀不能重叠。

根据您的问题,您似乎想为同一事件(在其中包括“整个存储桶”)中的同一事件触发多个Lambda函数。为此,我建议:

  • 创建 Amazon SNS主题
  • 创建一个指向SNS主题的 S3事件
  • 订阅每个Lambda函数到SNS主题