我想创建一个s3 bucket,每当有文件上传到bucket中的"uploads"文件夹时,就会触发lambda函数。我想在aws中使用无服务器框架来创建这些资源。
我已经在"provider.s3"下定义了我的s3 bucket配置,然后我试图在functions.hello.events.bucket 下引用该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".
serverless.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/
我的下一个尝试是在"resources"下定义s3 bucket,并在lambda触发器中使用bucket的引用。我仍然收到的警告信息
Serverless: Configuration warning at 'functions.hello.events[0].s3.bucket': should be string
serverless.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}
您应该使用您的bucket名称,而不是MyS3Bucket
:
events:
- s3:
bucket: ${env:MY_BUCKET_NAME}