serverless package
serverless package builds everything a deployment needs, the code packages and the CloudFormation templates, into .serverless without deploying it. Use it to inspect what would be deployed, or to build once in CI and deploy the same package later with serverless deploy --package. serverless deploy runs the same packaging step itself.
serverless package
Options
serverless package accepts these options:
| Option | Alias | Value | Default | Required | Description |
|---|---|---|---|---|---|
--stage | -s | string | provider.stage, else dev | No | Stage to package for. |
--region | -r | string | provider.region, else us-east-1 | No | AWS Region to package for. |
--aws-profile | string | provider.profile | No | AWS profile whose credentials the command uses. | |
--config | -c | file path | serverless.yml | No | Service configuration file to use. |
--package | -p | directory | package.path, else .serverless | No | Directory to write the package to, relative to the current working directory. Replaced on every run. |
--minify-template | flag | off | No | Write the compiled CloudFormation template without whitespace. |
serverless package also accepts the options every command takes: --param="key=value" (repeatable), --org, --app, --verbose, --debug and --help (-h).
The --package directory is deleted and written again on every run, so the command rejects the service directory itself and any directory that contains it. Use a dedicated directory inside the service, such as .serverless-package, or outside it, such as ../artifacts.
Files that serverless package writes
serverless package empties .serverless (keeping a Dev Mode session's dev/ folder) and writes:
- the code packages:
<service>.zip, or<function>.zipper function withpackage.individually, and<layer>.zipper layer; cloudformation-template-create-stack.json, the template that creates the stack;cloudformation-template-update-stack.json, the full compiled template;serverless-state.json, the resolved service thatserverless deploy --packagereads.
With --package or package.path, these files are moved from .serverless to that directory. To change what goes into the code packages, see Packaging.
AWS access during serverless package
serverless package needs AWS credentials, though it deploys nothing. Unless the service sets provider.deploymentBucket or uses the legacy in-stack deployment bucket, it looks up the region's shared deployment bucket, and creates the bucket and its SSM parameter when they don't exist yet. See AWS Credentials Requirement for Packaging and Deployment bucket.
serverless package is safe to re-run, and has no prompts of its own. In CI, sign in with SERVERLESS_ACCESS_KEY or SERVERLESS_LICENSE_KEY; see Non-interactive use.
Output of serverless package
Packaging "my-service" for stage "dev" (us-east-1)
✔ Service packaged (2s)
Provided lifecycle events
package:cleanuppackage:initializepackage:setupProviderConfigurationpackage:createDeploymentArtifactspackage:compileLayerspackage:compileFunctionspackage:compileEventspackage:finalize
Examples
Package for a specific stage and region
serverless package --stage prod --region eu-central-1
Build a package in CI and deploy it in a later step
serverless package --stage prod --region eu-central-1 --package ./artifacts
serverless deploy --stage prod --region eu-central-1 --package ./artifacts
The first command writes the package to ./artifacts; the second, run later from the same service directory with the same stage, region and AWS account, deploys it without packaging again. See Run serverless deploy in CI.
Related commands
serverless deploypackages and deploys the service.serverless printshows the resolvedserverless.ymlwithout packaging.serverless diffcompares a package with the deployed stack.