• 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 Keys
DeployingPackagingBuildingTestingServicesFunctions
OverviewHTTP (API Gateway v2)REST (API Gateway v1)ActiveMQApplication Load BalancerAlexa SkillAlexa Smart HomeCloudWatch EventCloudWatch LogCloudFrontCognito User PoolEventBridge EventIoTIoT Fleet ProvisioningKafkaKinesis & DynamoDBMSKRabbitMQS3ScheduleSNSSQSWebsocket
LayersManaged InstancesAlertsVersion 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 BucketStatePython support
OverviewRuntimeGatewayMemoryBrowserCode InterpreterDev Mode
API Gateway Proxy
OverviewGeneral ConfigurationAuthenticationAPI KeysData SourcesResolversPipeline FunctionsCachingDelta SyncCustom DomainWAFCLI Commands
Deploying SAM/CFN TemplatesWorkflow Tips
OverviewCreating PluginsCLI OutputCustom CommandsCustom VariablesExtending the Configuration schemaExtending and overriding configuration
OverviewDashboardAxiom
Overviewpackagedevdeploydeploy functiondeploy listinvokeinvoke locallogsloginlogin awslogin aws ssometricsinforollbackrollback functionremoveplugin installplugin uninstallprintprunesupportusagereconcile
Overview
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. Dashboard
  2. CI CD
  3. Custom Scripts

Custom scripts

Serverless Framework runs three primary operations on your repository when you have CI/CD configured: (1) install NPM packages via npm install, (2) run tests, if present, with npm test, and (3) deploy your service using sls deploy. You can run custom scripts before or after each of these steps if you need to customize the pipeline further.

To run custom scripts before & after NPM install and running tests, use the lifecycle hooks built into scripts of your package.json file. The preinstall, postinstall, pretest, and posttest, scripts are run automatically at each of these steps.

To run custom scripts before or after deployment, you can use the serverless-plugin-scripts plugin to run the scripts at various points of the serverless deploy lifecycle, including before deployment and on finalize.

Before npm install

To run a script before npm install, set the script in preinstall in your package.json.

{
  "name": "demo-serverless",
  "version": "1.0.0",
  "scripts": {
    "preinstall": "<your script>"
  }
}

After npm install

To run a script after npm install, set the script in postinstall in your package.json.

{
  "name": "demo-serverless",
  "version": "1.0.0",
  "scripts": {
    "postinstall": "<your script>"
  }
}

Before npm test

To run a script before npm test, set the script in pretest in your package.json.

{
  "name": "demo-serverless",
  "version": "1.0.0",
  "scripts": {
    "pretest": "<your script>"
  }
}

After npm test

To run a script after npm test, set the script in posttest in your package.json.

{
  "name": "demo-serverless",
  "version": "1.0.0",
  "scripts": {
    "posttest": "<your script>"
  }
}

Before serverless deploy

To run a script before serverless deploy starts the deployment add this to your serverless.yml.

plugins:
  - serverless-plugin-scripts
custom:
  scripts:
    hooks:
      'before:deploy:deploy': <your script>

After serverless deploy

To run a script after serverless deploy completes a deployment add this to your serverless.yml.

plugins:
  - serverless-plugin-scripts
custom:
  scripts:
    hooks:
      'deploy:finalize': <your script>

Additional lifecycle hooks

NPM provide additional lifecycle hooks you can run as well, additional documentation can be found here, https://docs.npmjs.com/misc/scripts.

Serverless Framework provides additional lifecycle hooks as “serverless deploy” is running, you can find more information about additional hooks in the serverless-plugin-scripts docs.

Edit this page
Prev Preview DeploymentsNextTesting

Contents

  • Custom scripts
  • Additional lifecycle hooks

Related

GuidesPluginsExamplesSlack CommunitySupport