This topic uses Python as an example to describe how to use the Serverless Devs development tool to deploy sample code to the Function Compute console and install related dependencies.

Background information

By uploading a photo of a cat or dog, identify whether the animal in the photo is a cat or a dog. dog and cat

For more information about this example, see Sample project.



  1. Run the following command to clone the project:
    git clone
  2. Install dependencies.
    1. Run the following command to go to the project directory in which the project data is stored:
      cd cat-dog-classify
    2. Run the following command to install dependencies:
      s build
      The following output is returned:
      [2021-12-09 07:26:39] [INFO] [S-CLI] - Start ...
      [2021-12-09 07:26:40] [INFO] [FC-BUILD] - Build artifact start...
      [2021-12-09 07:26:40] [INFO] [FC-BUILD] - Use docker for building.
      [2021-12-09 07:26:40] [INFO] [FC-BUILD] - Build function using image: registry.<regionId>
      [2021-12-09 07:26:40] [INFO] [FC-BUILD] - begin pulling image registry.<regionId>, you can also use docker pull to pull image by yourself.
      build-1.9.20: Pulling from aliyunfc/runtime-python3.6
      f49cf87b52c1: Already exists
      01ce50b4eb85: Already exists
      02b807385deb: Pull complete
      9b9fdb8de506: Pull complete
      Digest: sha256:a9a6dab2d6319df741ee135d9749a90b2bb834fd11ee265d1fb106053890****
      Status: Downloaded newer image for registry.<regionId>
       builder begin to build
      [2021-12-09 07:27:57] [INFO] [FC-BUILD] - Build artifact successfully.
      Tips for next step
      * Invoke Event Function: s local invoke
      * Invoke Http Function: s local start
      * Deploy Resources: s deploy
      End of method: build
    After you run the command to install dependencies, Serverless Devs automatically installs the related dependency package and downloads the third-party libraries to the .s/build/artifacts/cat-dog/classify/.s/python directory.
  3. Upload dependencies to NAS.
    When you install dependencies, the code package referenced by the Function Compute may be larger than the code package limit after decompression. To reduce the size of the code package, you can store the dependencies with large memory and relatively large model parameter files in NAS.
    1. Run the following command to initialize the NAS file system.
      s nas init
      The following output is returned:
      [2021-12-09 07:29:58] [INFO] [S-CLI] - Start ...
      [2021-12-09 07:29:59] [INFO] [FC-DEPLOY] - Using region: cn-shenzhen
      [2021-12-09 07:29:59] [INFO] [FC-DEPLOY] - Using access alias: default
      [2021-12-09 07:29:59] [INFO] [FC-DEPLOY] - Using accessKeyID: LTAI4G4cwJkK4Rza6xd9****
      [2021-12-09 07:29:59] [INFO] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN****
      [2021-12-09 07:30:01] [INFO] [FC-DEPLOY] - Generated vpcConfig:
      securityGroupId: sg-wz90u1syk2h1f14b****
      vSwitchId: vsw-wz9qnuult4q5g4f7n****
      vpcId: vpc-wz9x9bzs0wtvjgt6n****
      [2021-12-09 07:30:15] [INFO] [FC-DEPLOY] - Checking Trigger httpTrigger exists
       Make service _FC_NAS_cat-dog success.
       Make function _FC_NAS_cat-dog/nas_dir_checker success.
       Make trigger _FC_NAS_cat-dog/nas_dir_checker/httpTrigger success.
      [2021-12-09 07:30:25] [INFO] [FC-DEPLOY] - Checking Service _FC_NAS_cat-dog exists
      [2021-12-09 07:30:25] [INFO] [FC-DEPLOY] - Checking Function nas_dir_checker exists
      [2021-12-09 07:30:26] [INFO] [FC-DEPLOY] - Checking Trigger httpTrigger exists
      There is auto config in the service: _FC_NAS_cat-dog
      [2021-12-09 07:30:26] [INFO] [FC-DEPLOY] - Generated nasConfig:
      groupId: 10003
        - fcDir: /mnt/auto
          nasDir: /cat-dog
          serverAddr: 2bfb748****
      userId: 10003
        userId:      10003
        groupId:     10003
            serverAddr: 2bfb748****
            nasDir:     /cat-dog
            fcDir:      /mnt/auto
    2. Run the following command to upload the dependencies to NAS:
      s nas upload -r .s/build/artifacts/cat-dog/classify/.s/python/ /mnt/auto/python
      The following output is returned:
      [2021-12-09 07:33:14] [INFO] [S-CLI] - Start ...
      Packing ...
      Package complete.
       Upload done
      Tips for next step
      * Invoke remote function: s invoke
      End of method: nas
    3. Run the following command to upload the model to NAS:
      s nas upload -r src/model/ /mnt/auto/model
      The following output is returned:
      [2021-12-09 07:52:26] [INFO] [S-CLI] - Start ...
      Packing ...
      Package complete.
       Upload done
      Tips for next step
      * Invoke remote function: s invoke
      End of method: nas
    4. Run the following command to view the NAS directory:
      s nas command ls /mnt/auto/
      The following output is returned:
      [2021-12-09 07:53:01] [INFO] [S-CLI] - Start ...
      Tips for next step
      * Invoke remote function: s invoke
      End of method: nas
  4. Run the following command to deploy the project:
    s deploy
    The following output is returned:
    [2021-12-09 07:56:15] [INFO] [S-CLI] - Start ...
    [2021-12-09 07:56:16] [INFO] [FC-DEPLOY] - Using region: cn-shenzhen
    [2021-12-09 07:56:16] [INFO] [FC-DEPLOY] - Using access alias: default
    [2021-12-09 07:56:16] [INFO] [FC-DEPLOY] - Using accessKeyID: LTAI4G4cwJkK4Rza6xd9****
    [2021-12-09 07:56:16] [INFO] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN****
    [2021-12-09 07:56:19] [INFO] [FC-DEPLOY] - Generated logConfig:
    enableInstanceMetrics: true
    enableRequestMetrics: true
    logBeginRule: ~
    logstore: fc-service-cat-dog-logstore
    project: 188077086902****-cn-shenzhen-logproject
    There is auto config in the service: cat-dog
    Tips for next step
    * Display information of the deployed resource: s info
    * Display metrics: s metrics
    * Display logs: s logs
    * Invoke remote function: s invoke
    * Remove Service: s remove service
    * Remove Function: s remove function
    * Remove Trigger: s remove trigger
    * Remove CustomDomain: s remove domain
      region:   cn-shenzhen
        name: cat-dog
        name:       classify
        runtime:    python3
        handler:    predict.handler
        memorySize: 1024
        timeout:    120
        system_url:    https://188077086902****
          type: http
          name: httpTrigger
    After the project is successfully deployed, you can view the temporary domain name generated by the Function Compute in the execution output. You can use this domain name to access the function you just deployed. For example, you can use a browser to access the domain name and then select the target image to identify the animals in the graph:cat-dogserverless devs
    Note The temporary domain name is used only for demonstration and development purposes. It is valid for a limited period of time. You must use a domain name that has obtained an Internet Content Provider (ICP) filing from Alibaba Cloud for production. For more information, see Configure a custom domain name.

Use reservations to eliminate cold start glitches

The Function Compute features dynamic scaling and automatically scales out the execution environment based on the number of concurrent requests. In this typical deep learning example, it takes a long time to load dependencies and model parameters. In a function of the 1 GB specification that you set, the time for concurrent access is about 10s, and sometimes it may be greater than 20s.

Therefore, it is inevitable that the function call glitch will occur, that is, the cold start time is greater than 10s. In this case, you can use the set reservation method to avoid cold start. You can run the following command in the project directory to remove the cold start glitch:
s provision put --target 10 --qualifier LATEST
At the same time, when you need to understand the maximum capacity of the server for better running and development, you can use the stress test command of Serverless Devs to perform stress tests on the specified functions.
Notice After you complete the stress test, run the following command to cancel the reservation:
s provision put --target 0 --qualifier LATEST