• 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
DeployingPackagingBuildingTestingDev ModeServicesFunctions
OverviewHTTP (API Gateway v2)REST (API Gateway v1)ActiveMQApplication Load BalancerAlexa SkillAlexa Smart HomeCloudWatch EventCloudWatch LogCloudFrontCognito User PoolEventBridge EventIoTIoT Fleet ProvisioningKafkaKinesis & DynamoDBMSKRabbitMQS3ScheduleSNSSQSWebsocket
LayersManaged 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. Dashboard
  2. CI CD
  3. Custom Scripts

Custom scripts in Serverless CI/CD

A Serverless CI/CD build runs three steps on your repository: it installs npm packages with npm install, runs the configured test command (npm test by default), and deploys the service with serverless deploy. To run your own commands around these steps, use npm lifecycle scripts in package.json for the install and test steps, and Framework lifecycle hooks in serverless.yml for the deploy step.

To run a commandSet
Before npm installscripts.preinstall in package.json
After npm installscripts.postinstall in package.json
Before npm testscripts.pretest in package.json
After npm testscripts.posttest in package.json
After packaging, before the stack updatea before:deploy:deploy hook in serverless.yml
After serverless deploy finishesa deploy:finalize hook in serverless.yml

Run commands around the install and test steps

npm runs the lifecycle scripts of the package.json in the service directory: preinstall and postinstall around the install, pretest and posttest around npm test. For example, to generate code after the install and seed test data before the tests:

{
  "name": "my-service",
  "version": "1.0.0",
  "scripts": {
    "postinstall": "node scripts/generate.mjs",
    "pretest": "node scripts/seed-test-data.mjs",
    "test": "node --test"
  }
}

npm runs pretest and posttest only when it runs the test script, with npm test or npm run test. When the service's CI/CD settings set a test command that doesn't run the test script, put the extra commands in that command instead. npm documents every lifecycle script in npm scripts.

Run commands around the deploy step

The community plugin serverless-plugin-scripts runs shell commands at Framework lifecycle events. Its README marks it as in maintenance mode. Install it in the service directory, so the build's npm install restores it:

npm install --save-dev serverless-plugin-scripts

Then add the plugin and the hooks to serverless.yml. This example runs a script after the service is packaged and before the stack is updated, and another after the deploy completes:

plugins:
  - serverless-plugin-scripts

custom:
  scripts:
    hooks:
      'before:deploy:deploy': node scripts/check-config.mjs
      'deploy:finalize': node scripts/smoke-test.mjs

serverless deploy has two lifecycle events, deploy and finalize, and the plugin accepts before: and after: hooks on each. Unless --package or package.path is set, the Framework packages the service in its own before:deploy:deploy hook, which runs before a plugin's hook on the same event. Each command runs in the directory where serverless was started. A command that exits with a non-zero code ends serverless deploy with an error: in a before:deploy:deploy hook the stack isn't updated, and in a deploy:finalize hook the stack is already updated. The hooks run on every serverless deploy, from Serverless CI/CD or from your own machine.

Related

  • Run tests in Serverless CI/CD: the test step and its command.
  • Plugins: install and configure Framework plugins.
  • Serverless CI/CD: what each build runs.
Edit this page
Prev Preview DeploymentsNextTesting

Contents

  • Custom scripts in Serverless CI/CD
  • Run commands around the install and test steps
  • Run commands around the deploy step
  • Related

Related

GuidesPluginsExamplesSlack CommunitySupport