Hello, from the example for an s3 event https://serverless.com/framework/docs/providers/aws/events/s3/ we have:
functions:
resize:
handler: resize.handler
events:
- s3: photos
resources:
Resources:
S3BucketPhotos:
Type: AWS::S3::Bucket
Properties:
BucketName: my-custom-bucket-name
# add additional custom bucket configuration here
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"
this works, but how do you specify a a specific function per event, as per https://serverless.com/framework/docs/providers/aws/events/s3#triggering-separate-functions-from-the-same-bucket, but with custom bucket configuration
so i want to trigger a function on
event: s3:ObjectCreated:*
and a different function on
event: s3:ObjectRemoved:*
any advice is much appreciated
It seems like you would need to have different function definitions. Like:
functions:
```` handler: resize.handler
```` events:
```` - s3:
```` bucket: photos
```` event: s3:ObjectCreated:*
```` otherfunc:
```` handler: resize.handler
```` events:
```` - s3:
```` bucket: photos
```` event: s3:ObjectRemoved:*
And then continue with the bucket customization as you already have.
Adding event in the function does not work, you have to add it to the AWS::S3::Bucket, as:
NotificationConfiguration:
LambdaConfigurations:
- Event: 's3:ObjectCreated:*'
Function:
"Fn::GetAtt":
- ProcessLambdaFunction
- Arn
- Event: 's3:ObjectRemoved:*'
Function:
"Fn::GetAtt":
- ReprocessLambdaFunction
- Arn
here is the complete Resource, note that we also have to create the LambdaPermission
for each of the functions, specifically the Naming conventions are XxxLambdaPermissionYyy
where Xxx
is the lambda function and Yyy
is the bucket reference in the function
process:
handler: bin/process
description: Process CSV files function when file has been added to S3 bucket
package:
individually: true
include:
- ./bin/process
memorySize: 128
timeout: 30
events:
- s3: csv
reprocess:
handler: bin/reprocess
description: Reprocess CSV files function when files have been deleted
package:
individually: true
include:
- ./bin/reprocess
memorySize: 128
timeout: 30
events:
- s3: csv
resources:
Resources:
S3BucketCsv:
Type: AWS::S3::Bucket
Properties:
BucketName: ${file(./config.yml):${opt:stage}.CSV_BUCKET}
# add additional custom bucket configuration here
NotificationConfiguration:
LambdaConfigurations:
- Event: 's3:ObjectCreated:*'
Function:
"Fn::GetAtt":
- ProcessLambdaFunction
- Arn
- Event: 's3:ObjectRemoved:*'
Function:
"Fn::GetAtt":
- ReprocessLambdaFunction
- Arn
LifecycleConfiguration:
Rules:
- Id: "Delayed csv expiration"
Status: "Enabled"
TagFilters:
- Key: "deleted-csv"
Value: "true"
ExpirationInDays: 1
CsvBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: ${file(./config.yml):${opt:stage}.CSV_BUCKET}
PolicyDocument:
Statement:
- Action:
- s3:putObject
Effect: "Allow"
Principal:
"AWS":
- "arn:aws:iam::#{AWS::AccountId}:root"
Resource:
- "arn:aws:s3:::${file(./config.yml):${opt:stage}.CSV_BUCKET}/*.xls"
- "arn:aws:s3:::${file(./config.yml):${opt:stage}.CSV_BUCKET}/*.csv"
- "arn:aws:s3:::${file(./config.yml):${opt:stage}.CSV_BUCKET}/*.tsv"
- "arn:aws:s3:::${file(./config.yml):${opt:stage}.CSV_BUCKET}/*.xlsx"
ProcessLambdaPermissionCsvS3:
Type: "AWS::Lambda::Permission"
Properties:
FunctionName:
"Fn::GetAtt":
- ProcessLambdaFunction
- Arn
Principal: "s3.amazonaws.com"
Action: "lambda:InvokeFunction"
SourceAccount:
Ref: AWS::AccountId
SourceArn: "arn:aws:s3:::${file(./config.yml):${opt:stage}.CSV_BUCKET}"
ReprocessLambdaPermissionCsvS3:
Type: "AWS::Lambda::Permission"
Properties:
FunctionName:
"Fn::GetAtt":
- ReprocessLambdaFunction
- Arn
Principal: "s3.amazonaws.com"
Action: "lambda:InvokeFunction"
SourceAccount:
Ref: AWS::AccountId
SourceArn: "arn:aws:s3:::${file(./config.yml):${opt:stage}.CSV_BUCKET}"
Hope this helps others