• 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. Plugins
  3. Custom Commands

Custom commands

A Serverless Framework plugin can add its own CLI commands, such as serverless greet. The plugin defines the command, its options and its lifecycle events in the commands object, and runs code for those events in hooks. Use a custom command for a task that isn't part of package or deploy; to add steps to those commands, hook into their events instead (Create a plugin).

This plugin adds serverless greet with a --name option:

// greet-plugin.mjs
export default class GreetPlugin {
  constructor(serverless, options, { log }) {
    this.options = options
    this.log = log

    this.commands = {
      greet: {
        usage: 'Print a greeting',
        lifecycleEvents: ['run'],
        options: {
          name: {
            usage: 'Name to greet (for example "--name Ada")',
            shortcut: 'n',
            type: 'string',
            default: 'world',
          },
        },
      },
    }

    this.hooks = {
      'greet:run': () => this.run(),
    }
  }

  run() {
    this.log.notice(`Hello, ${this.options.name}!`)
  }
}

List the plugin in serverless.yml:

# serverless.yml
service: my-service

provider:
  name: aws
  runtime: nodejs24.x

plugins:
  - ./greet-plugin.mjs

Run the command in the service directory:

serverless greet -n Ada
Hello, Ada!

Plugin commands exist only in a service directory whose serverless.yml lists the plugin. Elsewhere, the CLI stops with No configuration file found.

Command definition

Each key of commands is a command name, and its value defines the command:

PropertyTypeDescription
usagestringHelp text shown by serverless --help and serverless <command> --help
lifecycleEventsstring[]Events the command runs, in order. A command without lifecycle events does nothing
optionsobjectThe command's CLI options (Command options)
commandsobjectSubcommands, defined the same way (Subcommands)
typestringentrypoint for a command only other plugins can start; container for a command that only groups subcommands

Lifecycle events of a custom command

A command has no logic of its own: the plugin adds it with hooks on the command's lifecycle events. The event name is <command>:<event>, and the Framework adds a before: and an after: event for each:

// greet-plugin.mjs, inside the constructor
this.commands = {
  greet: {
    usage: 'Print a greeting',
    lifecycleEvents: ['prepare', 'run'],
  },
}

this.hooks = {
  'before:greet:prepare': () => this.log.verbose('Before prepare'),
  'greet:prepare': () => this.prepare(),
  'greet:run': () => this.run(),
  'after:greet:run': () => this.log.verbose('Done'),
}

serverless greet runs before:greet:prepare, greet:prepare, after:greet:prepare, before:greet:run, greet:run and after:greet:run, awaiting each hook. Other plugins can hook into the same events.

Command options

Options are passed with two dashes (--name Ada) or, with a shortcut, with one dash (-n Ada). The plugin reads them from the options constructor parameter, under the option's full name.

PropertyTypeDescription
usagestringHelp text for the option
typestringRequired. string, boolean or multiple. A multiple option collects repeated values into an array (--tag a --tag b)
shortcutstringOne-letter alias used with a single dash
requiredbooleanWhen true, the command stops if the option is missing
defaultanyValue used when the option is missing. A boolean option passed without a value also gets it

The Framework applies default values when the command runs, after plugin constructors, so read options in hooks.

The Framework checks options before it runs the command. These are the errors it stops with:

Option "name" is required for the "greet" command. Run "serverless greet --help" for more info.
Option "name" is of type "string" but expected type "undefined".
Unrecognized option "nmae". Run "serverless greet --help" for a list of all available options.

The second error appears when an option without type is passed, so give every option a type.

Every command also accepts the Framework's common options: --stage (-s), --region (-r), --aws-profile, --config (-c), --param, --org, --app, --verbose, --debug, --help (-h) and --version (-v). The common option replaces a command option with the same name.

Subcommands

A command can group subcommands under commands. The subcommand's events join the command names with colons:

// tools-plugin.mjs
export default class ToolsPlugin {
  constructor(serverless, options, { log }) {
    this.commands = {
      tools: {
        type: 'container',
        commands: {
          report: {
            usage: 'Print a report',
            lifecycleEvents: ['run'],
          },
        },
      },
    }

    this.hooks = {
      'tools:report:run': () => log.notice('Report'),
    }
  }
}

serverless tools report runs tools:report:run. A container command such as tools has no lifecycle events of its own.

Commands that only plugins start

A command with type: 'entrypoint' is hidden from the CLI. A plugin starts it from a hook with serverless.pluginManager.spawn(), which runs the command's whole lifecycle and waits for it:

// tools-plugin.mjs
export default class ToolsPlugin {
  constructor(serverless, options, { log }) {
    this.serverless = serverless

    this.commands = {
      tools: {
        usage: 'Run tools',
        lifecycleEvents: ['run'],
        commands: {
          cleanup: {
            type: 'entrypoint',
            lifecycleEvents: ['run'],
          },
        },
      },
    }

    this.hooks = {
      'tools:run': () => this.serverless.pluginManager.spawn('tools:cleanup'),
      'tools:cleanup:run': () => log.notice('Cleaned up'),
    }
  }
}

spawn() also starts core commands, such as spawn('package').

Command names shared by several plugins

When two plugins, or a plugin and the Framework, define the same command, the Framework merges the definitions:

  • Options from both are available. When both define an option with the same name, the definition loaded first is kept.
  • The lifecycle events of the plugin loaded last replace the earlier ones when it defines any.
  • Hooks of both plugins run for the events in the merged lifecycle. A hook on an event that was replaced stays registered but never runs: when the first plugin defines the lifecycle event run and the second defines clean, the merged command has only clean, and hooks on run never run.

A plugin can use this to add an option to a core command:

// my-plugin.mjs, inside the constructor
this.commands = {
  deploy: {
    options: {
      notify: { usage: 'Send a notification after deploying', type: 'boolean' },
    },
  },
}

To add a separate command, give it a distinct name, such as my-company-deploy instead of deploy.

Related pages

  • Create a plugin: the plugin class, lifecycle hooks and configuration
  • Plugins: install, load and order plugins
  • CLI output: logs, progress and text output
Edit this page
Prev CLI OutputNextCustom Variables

Contents

  • Custom commands
  • Command definition
  • Lifecycle events of a custom command
  • Command options
  • Subcommands
  • Commands that only plugins start
  • Command names shared by several plugins
  • Related pages

Related

GuidesPluginsExamplesSlack CommunitySupport