无服务器S3存储桶事件可在模板中启用默认加密

时间:2018-09-21 04:55:23

标签: amazon-web-services amazon-s3 serverless-framework serverless

根据https://serverless.com/framework/docs/providers/aws/events/s3/ 我可以在无服务器模板中创建一个S3存储桶,当jpg文件被放入上载目录时,该存储桶会触发事件。太好了!

functions:
  users:
    handler: users.handler
    events:
      - s3:
          bucket: photos
          event: s3:ObjectCreated:*
          rules:
            - prefix: uploads/
            - suffix: .jpg

关于要添加自定义S3存储桶属性的另一个示例。也很棒!

functions:
  resize:
    handler: resize.handler
    events:
      - s3: photos

resources:
  Resources:
    S3BucketPhotos:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: my-custom-bucket-name
    ResizeLambdaPermissionPhotosS3:
      Type: "AWS::Lambda::Permission"
      Properties:
        FunctionName:
          "Fn::GetAtt":
            - ResizeLambdaFunction
            - Arn
        Principal: "s3.amazonaws.com"
        Action: "lambda:InvokeFunction"
        SourceAccount:
          Ref: AWS::AccountId
        SourceArn: "arn:aws:s3:::my-custom-bucket-name"

如果要向该存储桶添加默认加密,则需要在模板的“资源”部分中创建存储桶,并在事件处理程序中引用它,好了,我可以做到这一点。

functions:
  resize:
    handler: resize.handler
    events:
      - s3: photos

resources:
  Resources:
    S3BucketPhotos:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: my-custom-bucket-name
        BucketEncryption:
          ServerSideEncryptionConfiguration:
            - ServerSideEncryptionByDefault:
                SSEAlgorithm: AES256

但是现在我停留在如何指定要触发处理程序的事件和规则(前缀和文件类型)上。我无法将它们添加到调整大小处理程序块中,并且在搜索文档时遇到了麻烦。

1 个答案:

答案 0 :(得分:0)

  

我无法将它们添加到调整大小处理程序块中

我相信你可以。在resources部分中声明存储桶并不会消除您在处理程序的S3事件中指定规则的能力。

用于测试的完整示例:

functions:
  resize:
    handler: handler.s3EventProcessor
    events:
      - s3: 
          bucket: photos
          event: s3:ObjectCreated:*
          rules:
            - prefix: uploads/
            - suffix: .jpg          
resources:
  Resources:
    S3BucketPhotos:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: ${self:service}-${self:provider.stage}
        BucketEncryption:
          ServerSideEncryptionConfiguration:
            - ServerSideEncryptionByDefault:
                SSEAlgorithm: AES256        
    ResizeLambdaPermissionPhotosS3:
      Type: "AWS::Lambda::Permission"
      Properties:
        FunctionName:
          "Fn::GetAtt":
            - ResizeLambdaFunction
            - Arn
        Principal: "s3.amazonaws.com"
        Action: "lambda:InvokeFunction"
        SourceAccount:
          Ref: AWS::AccountId
        SourceArn: "arn:aws:s3:::${self:service}-${self:provider.stage}"