You can use the custom runtime of Function Compute to write functions in Function Compute by using TypeScript. This topic describes how to deploy and invoke a TypeScript function. You can modify the TypeScript sample code to perform secondary development.

Prerequisites

The following operations are complete:

Procedure

  1. Run the following command in the target directory to initialize the project:
    s init start-fc-custom-samples -d start-fc-custom-samples
  2. Run the following command to go to the code directory:
    cd start-fc-custom-samples/typescript/event-demo/code
  3. Run the following command to install dependencies:
    npm install
    Sample output:
    npm WARN old lockfile
    npm WARN old lockfile The package-lock.json file was created with an old version of npm,
    npm WARN old lockfile so supplemental metadata must be fetched from the registry.
    npm WARN old lockfile
    npm WARN old lockfile This is a one-time fix-up, please be patient...
    npm WARN old lockfile
    
    up to date in 828ms
  4. Run the following command to go to the project directory:
    cd ../
  5. Run the following command to deploy the project:
    s deploy -y
    Sample output:
    [2021-09-09T16:30:26.128] [INFO ] [S-CLI] - Start ...
    [2021-09-09T16:30:26.848] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-09-09T16:30:26.849] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-09T16:30:26.849] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-09-09T16:30:26.849] [INFO ] [FC-DEPLOY] - Using accessKeySecret: LTAI4G4cwJkK4Rza6xd9****
      Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
    [2021-09-09T16:30:27.466] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T16:30:27.739] [INFO ] [FC-DEPLOY] - Checking Function tsEventFunc exists
      Make service custom-demo success.
      Make function custom-demo/tsEventFunc success.
    [2021-09-09T16:32:29.150] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T16:32:29.305] [INFO ] [FC-DEPLOY] - Checking Function tsEventFunc exists
    
    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
    
    
    
    fc-ts-demo-fc-ts:
      region: cn-hangzhou
      service:
        name: custom-demo
      function:
        name: tsEventFunc
        runtime: custom
        handler: index.handler
        memorySize: 512
        timeout: 3
  6. Run the following command to invoke the function:
    s invoke -e "hello"
    Sample output:
    [2021-09-09T16:33:36.971] [INFO ] [S-CLI] - Start ...
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: 84b50778-d27f-4554-b5e5-5cbe908498ba
    hello
    FC Invoke End RequestId: 84b50778-d27f-4554-b5e5-5cbe908498ba
    
    Duration: 7.11 ms, Billed Duration: 8 ms, Memory Size: 512 MB, Max Memory Used: 171.41 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    hello
    
    
    End of method: invoke