You can use the custom runtime of Function Compute to write functions in Function Compute by using Python 3.7. This topic describes how to deploy and invoke a Python function. You can modify the Python sample code as needed for custom development.

Procedure

  1. Run the following command in the specified directory to initialize a project:
    s init fc-custom-python37-event
  2. Run the following command to go to the project directory:
    cd fc-custom-python37-event
  3. Optional: Modify the sample code as needed for custom development.
  4. Run the following command to install dependencies:
    s build -d
    Sample command output:
    [2021-12-24 09:42:25] [INFO] [S-CLI] - Start ...
    [2021-12-24 09:42:26] [INFO] [FC-BUILD] - Build artifact start...
    [2021-12-24 09:42:26] [INFO] [FC-BUILD] - Use docker for building.
    [2021-12-24 09:42:26] [INFO] [FC-BUILD] - Build function using image: registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.21
    [2021-12-24 09:42:27] [INFO] [FC-BUILD] - begin pulling image registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.21, you can also use docker pull registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.21 to pull image by yourself.
    build-1.9.21: Pulling from aliyunfc/runtime-custom
    ......
    Digest: sha256:29057f611f84f43a38fae4e69ea3f8ef6acbc077fb08b603f1626621bf79****
    Status: Downloaded newer image for registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.21
    [2021-12-24 09:44:26] [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
  5. Run the following command to deploy the project:
    s deploy -y
    Sample command output:
    [2021-12-24 09:49:31] [INFO] [S-CLI] - Start ...
    [2021-12-24 09:49:31] [INFO] [S-CLI] - Start the pre-action
    [2021-12-24 09:49:31] [INFO] [S-CLI] - Action: s build --use-docker
    [2021-12-24 09:49:31] [INFO] [S-CLI] - Start ...
    [2021-12-24 09:49:32] [INFO] [FC-BUILD] - Build artifact start...
    [2021-12-24 09:49:32] [INFO] [FC-BUILD] - Use docker for building.
    [2021-12-24 09:49:32] [INFO] [FC-BUILD] - Build function using image: registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.21
    [2021-12-24 09:49:32] [INFO] [FC-BUILD] - skip pulling image registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.21...
    [2021-12-24 09:50:02] [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
    [2021-12-24 09:50:02] [INFO] [S-CLI] - End the pre-action
     Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
     devsapp_fc-plan.zip file decompression completed
     Checking Function pyEventFunc exists
    Detail:
    
    updated:
      LD_LIBRARY_PATH: /code/.s/root/usr/local/lib:/code/.s/root/usr/lib:/code/.s/root/usr/lib/x86_64-linux-gnu:/code/.s/root/usr/lib64:/code/.s/root/lib:/code/.s/root/lib/x86_64-linux-gnu:/code/.s/root/python/lib/python2.7/site-packages:/code/.s/root/python/lib/python3.6/site-packages:/code:/code/lib:/usr/local/lib
      PATH:            /code/.s/root/usr/local/bin:/code/.s/root/usr/local/sbin:/code/.s/root/usr/bin:/code/.s/root/usr/sbin:/code/.s/root/sbin:/code/.s/root/bin:/code:/code/node_modules/.bin:/code/.s/python/bin:/code/.s/node_modules/.bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin:/bin
      NODE_PATH:       /code/node_modules:/usr/local/lib/node_modules
      PYTHONUSERBASE:  /code/.s/python
    {
      "LD_LIBRARY_PATH": "/code/.s/root/usr/local/lib:/code/.s/root/usr/lib:/code/.s/root/usr/lib/x86_64-linux-gnu:/code/.s/root/usr/lib64:/code/.s/root/lib:/code/.s/root/lib/x86_64-linux-gnu:/code/.s/root/python/lib/python2.7/site-packages:/code/.s/root/python/lib/python3.6/site-packages:/code:/code/lib:/usr/local/lib",
      "PATH": "/code/.s/root/usr/local/bin:/code/.s/root/usr/local/sbin:/code/.s/root/usr/bin:/code/.s/root/usr/sbin:/code/.s/root/sbin:/code/.s/root/bin:/code:/code/node_modules/.bin:/code/.s/python/bin:/code/.s/node_modules/.bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin:/bin",
      "NODE_PATH": "/code/node_modules:/usr/local/lib/node_modules",
      "PYTHONUSERBASE": "/code/.s/python"
    }
     Checking Service, Function (3.53s)
     Creating Service, Function (18.03s)
    
    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
    
    helloworld:
      region:   cn-hangzhou
      service:
        name: hello-world-service
      function:
        name:       pyEventFunc
        runtime:    custom
        handler:    index.handler
        memorySize: 1536
        timeout:    30
  6. Run the following command to remotely invoke the function:
    s invoke -e "{}"
    Sample command output:
    [2021-12-24 10:00:12] [INFO] [S-CLI] - Start ...
    ========= FC invoke Logs begin =========
    
    Duration: 780.81 ms, Billed Duration: 781 ms, Memory Size: 1536 MB, Max Memory Used: 80.67 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    null
    
    End of method: invoke