This will create a photos bucket which fires the resize function when an object is added or modified inside the bucket. A hardcoded bucket name can lead to issues as a bucket name can only be used once in S3. For that you can use the Serverless Variable syntax and add dynamic elements to the bucket name.
functions:
resize:
handler: resize.handler
events:
- s3: photos
This will create a bucket photos. The users function is called whenever an object is removed from the bucket. Check out the AWS documentation to learn more about all the different event types that can be configured.
functions:
users:
handler: users.handler
events:
- s3:
bucket: photos
event: s3:ObjectRemoved:*
This will create a bucket photos. The users function is called whenever an image with .jpg extension is uploaded to folder uploads in the bucket. Check out the AWS documentation to learn more about all the different filter types that can be configured.
functions:
users:
handler: users.handler
events:
- s3:
bucket: photos
event: s3:ObjectCreated:*
rules:
- prefix: uploads/
- suffix: .jpg
You're able to repeat the S3 event configuration in the same or separate functions so one bucket can call these functions. One caveat though is that you can't repeat the same configuration in both functions, e.g. the event type has to be different.
The following example will work:
functions:
users:
handler: users.handler
events:
- s3:
bucket: photos
event: s3:ObjectCreated:*
- s3:
bucket: photos
event: s3:ObjectRemoved:*