• Documentation
  • Pricing
© 2026 Serverless, Inc. All rights reserved.

Framework

  • Overview
  • Documentation
  • Plugins360
  • Pricing

Learn

  • Blog
  • GuidesUpdated
  • Examples240
  • Courses

Resources

  • Support
  • Security
  • Trust Center
  • Status

Community

  • Slack
  • GitHub47k
  • Forum
  • Meetups

Company

  • About
  • Careers
  • Contact
  • Partners

Legal

  • Terms of Service
  • Privacy Policy
  • Trademark
  • DMCA
Serverless Framework Logo

Serverless Framework

Intro
SetupUpgrading To V4ConceptsTutorialAWS CredentialsLicense KeysVerifying Downloads
DeployingPackagingBuildingTestingDev ModeServicesFunctions
OverviewHTTP (API Gateway v2)REST (API Gateway v1)ActiveMQApplication Load BalancerAlexa SkillAlexa Smart HomeCloudWatch EventCloudWatch LogCloudFrontCognito User PoolEventBridge EventIoTIoT Fleet ProvisioningKafkaKinesis & DynamoDBMSKRabbitMQS3ScheduleSNSSQSWebsocket
Layers
OverviewTroubleshooting
Managed InstancesWeb Apps (Lambda Web Functions)Sandboxes (Lambda MicroVMs)MCP ServersAlertsVersion PruningDomainsIAM Function PermissionsParameters
OverviewSelf-reference serverless.ymlServerless CoreEnvironment VariablesCLI OptionsExternal YAML/JSON FilesJavascript propertiesGitDoppler
OverviewS3 ObjectsSSM Parameter Store & Secrets ManagerCloudFormation Stack Outputs
OverviewVaultTerraform State Output
ResourcesComposing ServicesDeployment BucketStateAgent SkillsPython support
OverviewRuntimeGatewayMemoryBrowserCode InterpreterDev Mode
API Gateway Proxy
OverviewGeneral ConfigurationAuthenticationAPI KeysData SourcesResolversPipeline FunctionsSubstitutionsCachingDelta SyncCustom DomainWAFCLI Commands
Deploying SAM/CFN TemplatesWorkflow Tips
OverviewCreating PluginsCLI OutputCustom CommandsCustom VariablesExtending the Configuration schemaExtending and overriding configuration
OverviewDashboardAxiom
Overviewagent docsagent inspectagent setupagent skills installagent skills list / readdeploydeploy functiondeploy listdevdiffinfoinvokeinvoke localloginlogin awslogin aws ssologsmetricspackageplugin installplugin uninstallprintprunereconcileremoverollbackrollback functionusage
OverviewConcepts
OverviewMetricsTracesTroubleshoot
OverviewNode.jsPython
OutputsProviders
OverviewBranch DeploymentsPreview DeploymentsCustom ScriptsTestingPrivate PackagesNotificationsMono ReposDeploy in your own CI/CDBest PracticesTroubleshootingFAQ
OverviewSetupToolsAWS Integration
Serverless.yml Reference
Examples and TutorialsConfiguration Validation
  1. Usage
  2. Events
  3. S3

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
  • name sets the bucket name. Without name, the key (photos) is the bucket name, so it must follow the bucket naming rules. Set the name with name rather than bucketName: the Framework builds the Lambda permission from name or 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 S3Bucket followed 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, bucket can also be a CloudFormation intrinsic function, such as !Ref to a parameter or !ImportValue. Without existing: true, bucket must be a name.
  • One function can use one existing bucket. s3 events of one function on two different existing buckets stop packaging with Only one S3 Bucket can be configured per function.; use a separate function for each bucket.
  • event and rules work 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:

KeyTypeDefaultAllowed values
bucketString or intrinsicRequiredA bucket name or a provider.s3 key; an intrinsic function only with existing: true
eventStrings3:ObjectCreated:*An S3 event type starting with s3:
rulesList of one-key objectsNot setprefix or suffix, each a string or intrinsic function
existingBooleanfalsetrue adds the notification to an existing bucket
forceDeployBooleanfalsetrue 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.yml reference: S3
  • Process Amazon S3 event notifications with Lambda
Edit this page
Prev RabbitMQNextSchedule

Contents

  • S3 Bucket Events
  • Setting the specific trigger event
  • Setting filter rules
  • Several functions or events on one bucket
  • Configure the bucket with provider.s3
  • Use an existing bucket
  • Force an update of existing bucket notifications
  • s3 event options
  • Permissions for S3 events
  • Related

Related

GuidesPluginsExamplesSlack CommunitySupport