gpt4 book ai didi

amazon-web-services - 在同一个无服务器框架中创建存储桶和 lambda 触发器

转载 作者:行者123 更新时间:2023-12-03 07:34:22 24 4
gpt4 key购买 nike

我想创建一个 s3 存储桶,并在每当某些文件上传到存储桶中的“uploads”文件夹时触发 lambda 函数。我想使用 aws 中的无服务器框架创建这些资源。

我已经在“provider.s3”下定义了我的 s3 存储桶配置,然后我尝试在functions.hello.events.bucket 下引用该存储桶

但是,当我运行 sls package

时,出现以下错误
 Serverless Error ----------------------------------------

MyS3Bucket - Bucket name must conform to pattern (?!^(\d{1,3}\.){3}\d{1,3}$)(^(([a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])\.)*([a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])$). Please check provider.s3.MyS3Bucket and/or s3 events of function "hello".

无服务器.yml

service: some-service
frameworkVersion: '2'
useDotenv: true

provider:
name: aws
runtime: python3.8
lambdaHashingVersion: 20201221
s3:
MyS3Bucket:
bucketName: ${env:MY_BUCKET_NAME}
accessControl: Private
lifecycleConfiguration:
Rules:
- Id: ExpireRule
Status: Enabled
ExpirationInDays: '7'
package:
individually: true

functions:
hello:
name: my-lambda-function
handler: function.handler
memorySize: 128
timeout: 900
events:
- s3:
bucket: MyS3Bucket
event: s3:ObjectCreated:*
rules:
- prefix: uploads/

我的下一次尝试是在“资源”下定义 s3 存储桶,并在 lambda 触发器中使用该存储桶的引用。我仍然收到警告消息

Serverless: Configuration warning at 'functions.hello.events[0].s3.bucket': should be string

无服务器.yml

service: some-service
frameworkVersion: '2'
useDotenv: true

provider:
name: aws
runtime: python3.8
lambdaHashingVersion: 20201221

package:
individually: true

functions:
hello:
name: my-lambda-function
handler: handler.handler
memorySize: 128
timeout: 900
events:
- s3:
bucket:
Ref: MyS3Bucket
event: s3:ObjectCreated:*
rules:
- prefix: uploads/
existing: true

resources:
Resources:
MyS3Bucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: Private
BucketName: 'test.bucket'
OwnershipControls:
Rules:
- ObjectOwnership: ObjectWriter
LifecycleConfiguration:
Rules:
- Id: ExpireRule
Status: Enabled
ExpirationInDays: '7'

最佳答案

创建自定义 s3 存储桶名称变量,例如

custom:
bucket: foo-thumbnails

并使用它的事件

events:
- s3:
bucket: ${self:custom.bucket}

关于amazon-web-services - 在同一个无服务器框架中创建存储桶和 lambda 触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68252134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com