简体   繁体   English

如何使用无服务器框架在 AWS Lambda 函数上添加 S3 触发事件?

[英]How to add S3 trigger event on AWS Lambda function using Serverless framework?

I want to add trigger event on a Lambda function on an already existing bucket and for that I am using below configuration:我想在现有存储桶上的 Lambda 函数上添加触发事件,为此我使用以下配置:

 events:
      - s3:
          bucket: serverlesstest
          event: s3:ObjectCreated:*
          rules:
            - prefix: uploads/
            - suffix: .pdf

where bucket serverlesstest is already existing on S3.其中存储桶serverlesstest已存在于 S3 上。

This configurations is throwing the error:此配置引发错误:

An error occurred while provisioning your stack: S3BucketServerlesstest - serverlesstest already exists.配置堆栈时出错:S3BucketServerlesstest - serverlesstest 已存在。

How can I resolve this error using Serverless Framework?如何使用无服务器框架解决此错误?

It's not currently possible in the core framework because of CloudFormation behavior.由于 CloudFormation 行为,目前在核心框架中无法实现。 maybe.也许。

But you can use this plugin.但是你可以使用这个插件。

https://github.com/matt-filion/serverless-external-s3-event https://github.com/matt-filion/serverless-external-s3-event

After installing serverless-plugin-existing-s3 by npm install serverless-plugin-existing-s3 .通过npm install serverless-plugin-existing-s3

And add plugins to serverless.yml并将插件添加到 serverless.yml

plugins:
  serverless-plugin-existing-s3

Give your deploy permission to access the bucket.授予您的部署权限以访问存储桶。

provider:
  name: aws
  runtime: nodejs4.3
  iamRoleStatements:
    ...
    -  Effect: "Allow"
       Action:
         - "s3:PutBucketNotification"
       Resource:
         Fn::Join:
           - ""
       - - "arn:aws:s3:::BUCKET_NAME or *"

And use existingS3 event, it is not just s3 .并使用existingS3事件,它不仅仅是s3

functions:
  someFunction:
    handler: index.handler
    events:
      - existingS3:
          bucket: BUCKET_NAME
          events:
            - s3:ObjectCreated:*
          rules:
            - prefix: images/
            - suffix: .jpg

After sls deploy command, You can attach event by using sls s3deploy command.sls deploy命令之后,您可以使用sls s3deploy命令附加事件。

Feature Proposal功能提案

it will be added someday in the future.它会在未来的某一天添加​​。

https://github.com/serverless/serverless/issues/4241 https://github.com/serverless/serverless/issues/4241

Unfortunately, you can't specify an existing S3 bucket to trigger the Lambda function because the Serverless Framework* can't change existing infrastructure using Cloud Formation.遗憾的是,您无法指定现有 S3 存储桶来触发 Lambda 函数,因为无服务器框架*无法使用 Cloud Formation 更改现有基础设施。 This configuration requires that you create a new bucket.此配置要求您创建一个存储桶。

You can read more in the following issues that were open on GitHub:您可以在 GitHub 上打开的以下问题中阅读更多信息:

* I would try to configure this trigger using AWS Console or the SDK instead of the Serverelss Framework. * 我会尝试使用 AWS 控制台或 SDK 而不是 Serverelss 框架来配置此触发器。

This is possible as of serverless version v1.47.0, by adding the existing: true flag to your event configuration: https://serverless.com/framework/docs/providers/aws/events/s3/从无服务器版本 v1.47.0 开始,通过将existing: true标志添加到您的事件配置中,这是可能的: https existing: true //serverless.com/framework/docs/providers/aws/events/s3/

example from the source:来源示例:

functions:
  users:
    handler: users.handler
    events:
      - s3:
          bucket: legacy-photos
          event: s3:ObjectCreated:*
          rules:
            - prefix: uploads/
            - suffix: .jpg
          existing: true # <- this makes it work with existing objects

The source provides the following caveats:来源提供了以下警告:

IMPORTANT: You can only attach 1 existing S3 bucket per function.重要提示:每个函数只能附加 1 个现有的 S3 存储桶。

NOTE: Using the existing config will add an additional Lambda function and IAM Role to your stack.注意:使用现有配置将向您的堆栈添加额外的 Lambda 函数和 IAM 角色。 The Lambda function backs-up the Custom S3 Resource which is used to support existing S3 buckets. Lambda 函数备份用于支持现有 S3 存储桶的自定义 S3 资源。

serverless.yml seems to be very sensitive to spaces. serverless.yml似乎对空格非常敏感。 For me this advice was helpful.对我来说, 这个建议很有帮助。

If config looks like this如果配置看起来像这样

functions:
  hello:
    handler: handler.main
    events:
      - s3: 
        bucket: codepipeline-us-east-1-213458767560
        event: s3:ObjectCreated:*
        rules:
          - prefix: test/MyAppBuild

you need to add 2 more spaces to the indent of bucket, event & rules:您需要在存储桶、事件和规则的缩进中再添加 2 个空格:

functions:
  hello:
    handler: handler.main
    events:
      - s3: 
          bucket: codepipeline-us-east-1-213458767560
          event: s3:ObjectCreated:*
          rules:
            - prefix: test/MyAppBuild

如果存储桶是在堆栈中的其他地方使用无服务器创建的,那么您可以使用- s3: Bucket: { Ref: serverlesstest }否则您必须自己构建名称或 ARN。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Java ZF20E3C5E54C0AB3D375D660B3F896F 将触发器添加到 AWS Lambda Function - Add trigger to AWS Lambda Function using Java SDK for s3 使用无服务器将AWS sqs添加为AWS Lambda函数触发器 - Add aws sqs as aws lambda function trigger using serverless 如何在无服务器框架中将iamRoleStatements添加到S3触发器存储桶 - How to Add iamRoleStatements to S3 Trigger Bucket in Serverless Framework 如何使用nodejs使用无服务器框架使用lambda函数将图像/文件上传到s3存储桶? - How to upload images/files to s3 bucket using lambda function using serverless framework using nodejs? 如何在lambda函数中添加s3触发器? - How to add s3 trigger to lambda function? aws lambda - 如果手动创建 s3 存储桶,如何使用 cloudformation 添加 s3 触发器 - aws lambda - How to add s3 trigger using cloudformation if the s3 bucket is created manually 在 AWS Lambda 中使用来自 S3 触发器的事件数据 - Using Event Data from S3 Trigger in AWS Lambda 无服务器框架:S3 Lambda 使用资源中的存储桶触发 - Serverless Framework: S3 Lambda Trigger with bucket from resources AWS Lambda和S3触发事件数据 - AWS Lambda and S3 trigger event data 使用无服务器框架时如何在aws lambda函数中打包可执行文件? - How to package executables in aws lambda function when using serverless framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM