Updated on 2023-11-22 GMT+08:00

Package

Packaging CLI Command

Using the Serverless CLI tool, you can package your project without deploying it to Huawei Cloud. This is best used with CI/CD workflows to ensure consistent deployable artifacts.

Running the following command will build and save all of the deployment artifacts in the .serverless directory of the service:
serverless package

Packaging Configuration

If you want to have more control over function artifacts and package methods.

You can use the patterns configuration.

  • Patterns

    Patterns allow you to define globs that will be excluded/included from the resulting artifact. If you want to exclude files, you can use a global pattern prefixed with !, such as !exclude-me/**. Serverless Framework will run the global patterns so that you can always re-include previously excluded files and directories.

    Examples

    Exclude all node_modules but then re-include a specific module (node-fetch in this case) using exclude
    package:
      patterns:
        - '!node_modules/**'
        - 'node_modules/node-fetch/**'
    Exclude all files but handler.js
    package:
      patterns:
        - '!src/**'
        - src/function/handler.js
    If you want to exclude directories, use the correct global syntax. The following is an example:
    package:
      patterns:
        - '!tmp/**'
        - '!.git/**'
  • Artifact

    For complete control over the packaging process, you can specify your own artifact ZIP file.

    Serverless will not zip your service if this is configured and therefore patterns will be ignored. Either you use artifact or patterns.

    The artifact option is especially useful if your development environment allows you to generate a deployable artifact like Maven does for Java.

    Example
    service: my-service
    package:
      patterns:
        - '!tmp/**'
        - '!.git/**'
        - some-file
      artifact: path/to/my-artifact.zip
  • Package functions separately

    If you want even more control over your functions for deployment, you can configure them to be packaged separately. This allows for more control to optimize your deployment. To enable individual packaging, set individually to true in the packaging settings of the service or the function.

    Then for every function you can use the same patterns or artifact configuration options. The patterns options will be merged with the service options to create a patterns configuration for each function during packaging.
    service: my-service
    package:
      individually: true
      patterns:
        - '!excluded-by-default.json'
    functions:
      hello:
        handler: handler.hello
        package:
          # We're including this file so it will be in the final package of this function only
          patterns:
            - excluded-by-default.json
      world:
        handler: handler.hello
        package:
          patterns:
            - '!some-file.js'
    You can also select which functions to be packaged separately, and have the rest use the service package by setting the individually to true.
    service: my-service
    functions:
      hello:
        handler: handler.hello
      world:
        handler: handler.hello
        package:
          individually: true
  • Development Dependencies

    Serverless will auto-detect and exclude development dependencies based on the runtime your service is using to ensure that only the production relevant packages and modules are included in your ZIP file. This drastically reduces the overall size of the deployment package which will be uploaded to the cloud provider.

    You can opt out of automatic exclusion of development dependency by setting the excludeDevDependencies to false:
    package:
      excludeDevDependencies: false