S3 Bucket Events
The s3 event invokes a Lambda function when objects change in an Amazon S3 bucket, through the bucket's event notifications. By default, the Framework creates the bucket with the notification configuration. With existing: true, it adds the notifications to a bucket that already exists.
This serverless.yml creates a bucket and invokes resize for every object created in it:
# serverless.yml
service: my-service
provider:
name: aws
runtime: nodejs24.x
functions:
resize:
handler: handler.resize
events:
- s3: my-service-${sls:stage}-photos
S3 invokes the function asynchronously. Each record names the bucket and the object key; the key is URL-encoded, so a space arrives as +:
// handler.mjs
export const resize = async (event) => {
for (const record of event.Records) {
const bucket = record.s3.bucket.name
const key = decodeURIComponent(record.s3.object.key.replace(/\+/g, ' '))
console.log(record.eventName, bucket, key)
}
}
Bucket names are unique across all AWS accounts, so include the service and stage in the name, as in the example. A name must follow the S3 bucket naming rules; the Framework stops packaging on a name that doesn't. A function that writes to the bucket that triggers it can invoke itself in a loop; write to another bucket, or limit the trigger with a prefix rule.
Setting the specific trigger event
The s3 event's event sets the S3 event type that invokes the function. Without it, the Framework uses s3:ObjectCreated:*, which covers every way an object is created, including overwriting an existing object. This function runs whenever an object is deleted:
functions:
users:
handler: handler.users
events:
- s3:
bucket: my-service-${sls:stage}-photos
event: s3:ObjectRemoved:*
event takes one S3 event type, starting with s3:, such as s3:ObjectCreated:Put, s3:ObjectRemoved:Delete, s3:ObjectRestore:Completed or s3:ObjectTagging:*. For the full list, see Supported event types. To react to several event types, add one s3 event for each.
Setting filter rules
The s3 event's rules limits the notification to object keys with a given prefix, suffix, or both. This function runs for .jpg objects created under uploads/:
functions:
users:
handler: handler.users
events:
- s3:
bucket: my-service-${sls:stage}-photos
event: s3:ObjectCreated:*
rules:
- prefix: uploads/
- suffix: .jpg
Each item in rules has exactly one key, prefix or suffix, with a string or a CloudFormation intrinsic function as its value. S3 doesn't accept a * wildcard in a prefix or suffix, and other special characters must be URL-encoded. See Configuring event notifications using object key name filtering.
Several functions or events on one bucket
A bucket can invoke several functions, and one function can have several s3 events on the same bucket. The Framework combines them into the bucket's notification configuration:
functions:
users:
handler: handler.users
events:
- s3:
bucket: my-service-${sls:stage}-photos
event: s3:ObjectCreated:*
rules:
- prefix: uploads/
- s3:
bucket: my-service-${sls:stage}-photos
event: s3:ObjectRemoved:*
thumbnails:
handler: handler.thumbnails
events:
- s3:
bucket: my-service-${sls:stage}-photos
event: s3:ObjectCreated:*
rules:
- prefix: images/
S3 rejects two configurations on one bucket when their event types overlap (for example s3:ObjectCreated:* and s3:ObjectCreated:Put) and their prefix and suffix rules overlap as well; a configuration without rules overlaps every other one. In the example, users and thumbnails both use s3:ObjectCreated:* and stay valid because their prefixes, uploads/ and images/, don't overlap.
Configure the bucket with provider.s3
To set properties of a bucket the Framework creates, such as versioning or encryption, define it under provider.s3 and use its key as the s3 event's bucket:
provider:
name: aws
s3:
photos:
name: my-service-${sls:stage}-photos
versioningConfiguration:
Status: Enabled
bucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
functions:
resize:
handler: handler.resize
events:
- s3: photos
namesets the bucket name. Withoutname, the key (photos) is the bucket name, so it must follow the bucket naming rules. Set the name withnamerather thanbucketName: the Framework builds the Lambda permission fromnameor the key.- The other keys are the properties of AWS::S3::Bucket, with the first letter in lowercase (
bucketEncryption,versioningConfiguration). Values below them keep CloudFormation's PascalCase (Status,ServerSideEncryptionConfiguration). - The bucket's CloudFormation logical ID is
S3Bucketfollowed by the key without non-alphanumeric characters and with its first letter in uppercase (S3BucketPhotos).
Use an existing bucket
To add a notification to a bucket that already exists, outside this service's stack, set the s3 event's existing to true:
functions:
users:
handler: handler.users
events:
- s3:
bucket: legacy-photos
event: s3:ObjectCreated:*
rules:
- prefix: uploads/
- suffix: .jpg
existing: true
- The Framework adds a custom resource, backed by an extra Lambda function and IAM role in the stack, that writes the function's notification configurations to the bucket and adds the Lambda permission for S3. It keeps the bucket's other notification configurations, and removes the function's own configurations when the event or the service is removed.
- With
existing: true,bucketcan also be a CloudFormation intrinsic function, such as!Refto a parameter or!ImportValue. Withoutexisting: true,bucketmust be a name. - One function can use one existing bucket.
s3events of one function on two different existing buckets stop packaging withOnly one S3 Bucket can be configured per function.; use a separate function for each bucket. eventandruleswork as for a new bucket.
Force an update of existing bucket notifications
The custom resource updates the bucket's notification configuration only when the s3 event's settings change. If the configuration on the bucket was changed outside the Framework, set forceDeploy: true to rewrite it on every deploy:
functions:
users:
handler: handler.users
events:
- s3:
bucket: legacy-photos
event: s3:ObjectCreated:*
existing: true
forceDeploy: true
forceDeploy takes effect only together with existing: true. The Framework reads forceDeploy from the function's first s3 event with existing: true, and it rewrites all of that function's notification configurations on the bucket; set it on that event.
s3 event options
The s3 event takes a bucket name, or an object with these keys:
| Key | Type | Default | Allowed values |
|---|---|---|---|
bucket | String or intrinsic | Required | A bucket name or a provider.s3 key; an intrinsic function only with existing: true |
event | String | s3:ObjectCreated:* | An S3 event type starting with s3: |
rules | List of one-key objects | Not set | prefix or suffix, each a string or intrinsic function |
existing | Boolean | false | true adds the notification to an existing bucket |
forceDeploy | Boolean | false | true rewrites the notification configuration on every deploy; with existing: true |
Permissions for S3 events
For each function and bucket, the Framework adds an AWS::Lambda::Permission that lets s3.amazonaws.com invoke the function, limited to the bucket's ARN and the service's AWS account. The bucket and the function must be in the same AWS Region.
The function's execution role gets no access to the bucket. To read or write objects, add a statement to provider.iam.role.statements, and build the bucket ARN from its name: a !GetAtt or !Ref to a bucket the Framework creates makes the execution role depend on the bucket, which already depends on the function, and CloudFormation reports a circular dependency.
provider:
iam:
role:
statements:
- Effect: Allow
Action: s3:GetObject
Resource: arn:aws:s3:::my-service-${sls:stage}-photos/*
See IAM permissions.
Related
- EventBridge events, to receive S3 events through EventBridge with content-based filtering
serverless.ymlreference: S3- Process Amazon S3 event notifications with Lambda