[英]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
命令附加事件。
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.