This topic describes how to configure a custom startup command and the related parameters for a custom runtime function in Function Compute. In this example, the function runs in a custom runtime of Python 3.7.

Background information

When you deploy a custom runtime function in Function Compute (FC), the function code is decompressed to the /code directory by default. The bootstrap file that you provide is used as the function handler.

The custom startup command feature extends the capabilities of custom runtimes. You can configure a custom startup command to specify a file other than bootstrap as the function handler and pass parameters to the command. For more information about how custom runtimes work, see Overview.

Prerequisites

Procedure

  1. Initiate a project.
    s init start-fc-custom-samples -d start-fc-custom-samples
  2. Go to the directory of the initiated project.
    cd start-fc-custom-samples/python37/event-demo/
  3. Run the following command to install dependencies:
    s build -d
  4. Go to the /code directory and modify related files.
    1. Run the following command to go to the /code directory:
      cd code
    2. Modify the server.py file.

      Replace the end of the invoke handler with the following code:

      import os
      return os.environ["AA"] + os.environ["BB"]

      The following code provides an example of the modified file:

      from flask.logging import default_handler
      import time
      from flask import Flask
      from flask import request
      import json
      import sys
      import traceback
      import logging
      
      
      log = logging.getLogger('werkzeug')
      log.setLevel(logging.ERROR)
      
      
      app = Flask(__name__)
      
      
      REQUEST_ID_HEADER = 'x-fc-request-id'
      
      
      @app.route('/initialize', methods=['POST'])
      def init_invoke():
          rid = request.headers.get(REQUEST_ID_HEADER)
          print("FC Initialize Start RequestId: " + rid)
          # do your things
      
          print("FC Initialize End RequestId: " + rid)
          return "OK"
      
      
      @app.route('/invoke', methods=['POST'])
      def event_invoke():
          rid = request.headers.get(REQUEST_ID_HEADER)
          print("FC Invoke Start RequestId: " + rid)
      
          data = request.stream.read()
          print(data)
      
          try:
              # do your things, for example:
              evt = json.loads(data)
              print(evt)
          except Exception as e:
              exc_info = sys.exc_info()
              trace = traceback.format_tb(exc_info[2])
              errRet = {
                  "message": str(e),
                  "stack": trace
              }
              print(errRet)
              print("FC Invoke End RequestId: " + rid)
              return errRet, 404, [("x-fc-status", "404")]
      
          print("FC Invoke End RequestId: " + rid)
      
          import os
          return os.environ["AA"] + os.environ["BB"]
      
      
      if __name__ == "__main__":
          app.run(host='0.0.0.0', port=9000)
                              
    3. Run the following command to rename the bootstrap file:
      mv bootstrap bootcmd
    4. Modify the bootcmd file.

      The following code provides an example of the modified file:

      #!/bin/bash
      
      export AA=$1
      export BB=$2
      gunicorn -c gunicorn_conf.py server:app
  5. Go to the parent directory of the /code directory and deploy the function.
    1. Run the following command to go to the parent directory:
      cd ..
    2. Run the following command to deploy the function:
      s deploy -y
  6. Use one of the following methods to configure a custom startup command and the related parameters.
    • Use Serverless Devs

      Modify the customRuntimeConfig parameter in the s.yml file. The following code provides an example:

      function:
        customRuntimeConfig:
          command:
            - /code/bootcmd
          args:
            - aa
            - 11
    • Use the Function Compute console

      Log on to the Function Compute console and modify the Startup Command and Startup Parameter parameters in the Environment Information section on the details page of the function. The following figure provides a configuration example.

      bootcmd-config-example
  7. Use one of the following methods to invoke the function.
    • Use Serverless Devs
      s invoke --event '{}'

      The following code provides a sample invocation result:

      ========= FC invoke Logs begin =========
      
      Duration: 2.86 ms, Billed Duration: 3 ms, Memory Size: 1536 MB, Max Memory Used: 58.93 MB
      ========= FC invoke Logs end =========
      
      FC Invoke Result:
      aa11
      
      
      End of method: invoke
    • Use the Function Compute console

      Log on to the Function Compute console. Go to the details page of the function and click Test Function on the Code tab.

      The following figure provides a sample invocation result.

      console-invoke-bootcmd