以编程方式向S3存储桶添加多个事件通知

6
我想使用下面的CloudFormation模板在一个已经存在的S3存储桶上创建多个事件通知。但是,即使我在BucketConfiguration资源下指定另一个"LambdaFunctionConfigurations",我仍然只能看到S3存储桶上创建了一个事件。我还尝试创建另一个BucketConfiguration资源,并配置另一个事件,但都没有成功。我正在寻找任何提示或建议,以帮助我找到正确的方向。据我所知,s3.putBucketNotification方法将清除任何现有的事件通知配置。请记住,CF模板确实会创建单个事件通知(我正在寻找创建多个事件通知)。
以下是模板的数据模型:使用CloudFormation在S3存储桶中创建Lambda通知
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个回答

6
您是正确的,不可能为同一“触发器”定义多个事件。
例如,在S3控制台中,我成功地定义了:
- 相同“事件”但不同“前缀”的多个规则 - 相同“前缀”但不同“事件”的多个规则
然而,我无法为相同事件和相同前缀定义多个规则。错误消息如下:
“配置定义不明确。如果前缀对于相同事件类型重叠,则两个规则中不能有重叠的后缀。”
根据您的问题,似乎您想在相同前缀(包括“整个存储桶”)内为相同事件触发多个Lambda函数。对此,我建议:
- 创建一个Amazon SNS主题 - 创建指向SNS主题的S3事件 - 将每个Lambda函数订阅到SNS主题

这个还是这样吗?我正在尝试使用 Terraform 做类似的事情,但我无法在同一个 S3 存储桶上通过 tf 创建多个事件,并使用不同的前缀来触发不同的 Lambda。 - Manas Jani
我建议您在另一个存储桶上手动尝试在Amazon S3管理控制台中进行测试。我想知道这是否是Terraform的限制? - John Rotenstein
我尝试了点击构建它,看起来我可以添加另一个事件。不知道为什么我的terraform代码不想追加,而只是一直覆盖第一个事件。我发布了另一个问题,https://stackoverflow.com/questions/63065323/multiple-lambda-triggered-based-on-multiple-s3-bucket-event-notifications?noredirect=1#comment111522725_63065323,如果您能看一下就太好了。 - Manas Jani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接