All Products
Search
Document Center

Function Compute:Function-related commands

Last Updated:Jan 12, 2024

The FC component is an operation tool that is developed based on Serverless Devs in Function Compute. With this tool, you can run interactive commands to call the Function Compute API.

Prerequisites

Before you start, make sure that the following operations are performed:

Create a function

Command syntax:

sudo s cli fc api CreateFunction --region <regionid> --access <accessname> --apiVersion <20210406 or 20160815> --path '{"serviceName": "serviceName"}' --body '{"code":"code","customContainerConfig": "customContainerConfig","layer": "layer","description": "description","functionName": "functionName","handler": "functionhandler","initializationTimeout": "initializationTimeout","initializer": "initializer","memorySize": "momorysize","runtime“:"runtime","timeout": "timeout","caPort": "caport","environmentVariables": "environmentVariables","instanceConcurrency": "instanceConcurrency","instanceType": "instanceType","instanceLifecycleConfig": "instanceLifecycleConfig","customDNS": "customDNS","customRuntimeConfig": "customRuntimeConfig"}' --header '{"X-Fc-Code-Checksum": "X-Fc-Code-Checksum"}'

Parameters:

  • (Required) --region string: the region in which you want to deploy resources.

  • (Optional) --access string or -a string: the alias of the key to be used.

  • (Optional) --apiVersion: the API version. Valid values: 20210416 and 20160815.

  • path

    • (Required) --serviceName string: the service name.

  • body

    • (Optional) --code string: the compressed code file. The code file must be in the ZIP format.

    • (Optional) --customContainerConfig string: the custom container runtime. After you configure the custom container runtime, you can use custom container images to invoke functions.

    • (Optional) --layers string: the name of the specified layer.

    • (Optional) --description string: the function description.

    • (Required) --functionName string: the function name.

    • (Required) --handler string: the function handler. The format of the handler is "File name.Function name". For example, hello_world.handler indicates that the handler is the handler function in the hello_world.js file.

    • (Optional) --initializationTimeout number: the timeout period for function initialization.

    • (Optional) --initializer string: the Initializer function.

    • (Optional) --memorySize number: the memory size for function execution.

    • (Optional) --runtime string: the runtime.

    • (Optional) --timeout number: the timeout period for function execution. Default value: 60s.

    • (Optional) --caPort number: the listening port of the HTTP server in a custom runtime or Custom Container runtime.

    • (Optional) environmentVariables: the environment variables.

    • (Optional) instanceConcurrency: the concurrency of function instances.

    • (Optional) instanceType: the type of the function instance.

    • (Optional) instanceLifecycleConfig: the instance lifecycle hook configurations.

    • (Optional) customDNS: the custom Domain Name System (DNS) domain name.

    • (Optional) customRuntimeConfig: the custom runtime.

  • header

    • (Optional) X-Fc-Code-Checksum: the CRC-64 value of the function code package.

Sample code:

  • macOS or Linux (run the sample code based on the runtime OS)

    • For a programming language runtime, run the following command to create a function:

      sudo s cli fc api CreateFunction --region cn-hangzhou --access default --path '{"serviceName": "mytest"}'  --body '{"functionName": "mytest","code": {"ossBucketName": "testbucket","ossObjectName": "code.zip"},"handler": "index.handler","runtime": "python3"}'
    • For a Custom Container runtime, run the following command to create a function:

      sudo s cli fc api CreateFunction --region cn-hangzhou --access default --path '{"serviceName": "mytest"}' --body '{"functionName": "mytest","customContainerConfig": {"image": "registry.cn-hangzhou.aliyuncs.com/zh****/zh****:v0.1"},"handler": "index.handler","runtime": "custom-container"}'
  • Windows

    • For a programming language runtime, run the following command to create a function:

      s cli fc api CreateFunction --region cn-hangzhou --access default --path '{"serviceName": "mytest"}'  --body '{"functionName": "mytest","code": {\"ossBucketName\": \"testbucket\",\"ossObjectName\": \"code.zip\"},"handler": "index.handler","runtime": "python3"}'
    • For a Custom Container runtime, run the following command to create a function:

      s cli fc api CreateFunction --region cn-hangzhou --access default --path '{"serviceName": "mytest"}' --body '{"functionName": "mytest","customContainerConfig": {\"image\": \"registry.cn-hangzhou.aliyuncs.com/zh****/zh****:v0.1\"},"handler": "index.handler","runtime": "custom-container"}'

For more information about the API operation for creating a function, see CreateFunction.

Delete a function

Command syntax:

sudo s cli fc api DeleteFunction --region <regionid> --access <accessname> --apiVersion <20210406 or 20160815> --path '{"serviceName": "serviceName","functionName": "functionName"}' --header '{"If-Match": "serviceName"}'

Parameters:

  • (Required) --region string: the region in which you want to deploy resources.

  • (Optional) --access string or -a string: the alias of the key to be used.

  • (Optional) --apiVersion: the API version. Valid values: 20210416 and 20160815.

  • path

    • (Required) --serviceName string: the service name.

    • (Required) --functionName string: the function name.

  • header

    (Optional) --If-Match string: used to ensure that the resource that you actually changed is the same as the resource that you wanted to change.

Sample code:

sudo s cli fc api DeleteFunction --region cn-hangzhou --access default --path '{"serviceName": "mytest","functionName": "mytest"}'

For more information about the API operation for deleting a function, see DeleteFunction.

Update a function

Command syntax:

sudo s cli fc api UpdateFunction --region <regionid> --access <accessname> --apiVersion <20210406 or 20160815> --path '{"serviceName": "serviceName","functionName": "functionName"}' --body '{"code": "code","customContainerConfig": "customContainerConfig","layer": "layer","description": "description","handler": "functionhandler","initializationTimeout": "initializationTimeout","initializer": "initializer","memorySize": "momorysize","runtime“: "runtime","timeout": "timeout","caPort": "caport","environmentVariables": "environmentVariables","instanceConcurrency": "instanceConcurrency","instanceType": "instanceType","instanceLifecycleConfig": "instanceLifecycleConfig","customDNS": "customDNS","customRuntimeConfig": "customRuntimeConfig"}' --header '{"If-Match": "ifmatch","X-Fc-Code-Checksum": "X-Fc-Code-Checksum"}'

Parameters:

  • (Required) --region string: the region in which you want to deploy resources.

  • (Optional) --access string or -a string: the alias of the key to be used.

  • (Optional) --apiVersion: the API version. Valid values: 20210416 and 20160815.

  • path

    • (Required) --serviceName string: the service name.

    • (Required) --functionName string: the function name.

  • body

    • (Optional) --code string: the compressed code file. The code file must be in the ZIP format.

    • (Optional) --customContainerConfig string: the custom container runtime. After you configure the custom container runtime, you can use custom container images to invoke functions.

    • (Optional) --layers string: the name of the specified layer.

    • (Optional) --description string: the function description.

    • (Required) --handler string: the function handler. The format of the handler is "File name.Function name". For example, hello_world.handler indicates that the handler is the handler function in the hello_world.js file.

    • (Optional) --initializationTimeout number: the timeout period for function initialization.

    • (Optional) --initializer string: the Initializer function.

    • (Optional) --memorySize number: the memory size for function execution.

    • (Optional) --runtime string: the runtime.

    • (Optional) --timeout number: the timeout period for function execution. Default value: 60s.

    • (Optional) --caPort number: the listening port of the HTTP server in a custom runtime or Custom Container runtime.

    • (Optional) instanceConcurrency: the concurrency of function instances.

    • (Optional) instanceType: the type of the function instance.

    • (Optional) instanceLifecycleConfig: the instance lifecycle hook configurations.

    • (Optional) customDNS: the custom Domain Name System (DNS) domain name.

    • (Optional) customRuntimeConfig: the custom runtime.

  • header

    • (Optional) --If-Match string: ensures that the changed resource is the resource that you want to change.

    • (Optional) X-Fc-Code-Checksum: the CRC-64 value of the function code package.

Sample code:

  • Run the following command in macOS or Linux:

    • For a programming language runtime, run the following command to update a function:

      sudo s cli fc api UpdateFunction --region cn-hangzhou --access default --path '{"serviceName": "mytest","functionName": "mytest"}' --body '{"code": {"ossBucketName": "testbucket","ossObjectName": "code.zip"},"timeout": 31}'
    • For a Custom Container runtime, run the following command to update a function:

      sudo s cli fc api UpdateFunction --region cn-hangzhou --access default --path '{"serviceName": "mytest","functionName": "mytest"}' --body '{"customContainerConfig": {"image": "registry.cn-hangzhou.aliyuncs.com/zh****/zh****:v0.1"},"runtime": "custom-container","timeout": 31}'
  • Windows

    • For a programming language runtime, run the following command to update a function:

      s cli fc api UpdateFunction --region cn-hangzhou --access default --path '{"serviceName": "mytest","functionName": "mytest"}' --body '{"code": {\"ossBucketName\": \"testbucket\",\"ossObjectName\": \"code.zip\"},"timeout": 31}'
    • For a Custom Container runtime, run the following command to update a function:

      s cli fc api UpdateFunction --region cn-hangzhou --access default --path '{"serviceName": "mytest","functionName": "mytest"}' --body '{"customContainerConfig": {\"image\": \"registry.cn-hangzhou.aliyuncs.com/zh****/zh****:v0.1\"},"runtime": "custom-container","timeout": 31}'

For more information about the API operation for updating a function, see UpdateFunction.

Query configurations of a function

Command syntax:

sudo s cli fc api GetFunction --region <regionid> --access <accessname> --apiVersion <20210406 or 20160815> --path '{"serviceName": "serviceName","functionName": "functionName","qualifier": "version or alias"}'

Parameters:

  • (Required) --region string: the region in which you want to deploy resources.

  • (Optional) --access string or -a string: the alias of the key to be used.

  • (Optional) --apiVersion: the API version. Valid values: 20210416 and 20160815.

  • path

    • (Required) --serviceName string: the service name.

    • (Required) --functionName string: the function name.

    • (Optional) --qualifier string: the service alias or version.

Sample code:

sudo s cli fc api GetFunction --region cn-hangzhou --access default --path '{"serviceName": "mytest","functionName": "mytest","qualifier": "LATEST"}'

For more information about the API operation for querying the configurations of a function, see GetFunction.

Query the code of a function

Command syntax:

sudo s cli fc api GetFunctionCode --region <regionid> --access <accessname> --apiVersion <20210406 or 20160815> --path '{"serviceName": "serviceName","functionName": "functionName","qualifier": "version or alias"}'

Parameters:

  • (Required) --region string: the region in which you want to deploy resources.

  • (Optional) --access string or -a string: the alias of the key to be used.

  • (Optional) --apiVersion: the API version. Valid values: 20210416 and 20160815.

  • path

    • (Required) --serviceName string: the service name.

    • (Required) --functionName string: the function name.

    • (Optional) --qualifier string: the service alias or version.

Sample code:

sudo s cli fc api GetFunctionCode --region cn-hangzhou --access default --path '{"serviceName": "mytest","functionName": "mytest","qualifier": "LATEST"}'

For more information about the API operation for querying the code of a function, see GetFunctionCode.

Query functions

Command syntax:

sudo s cli fc api ListFunctions --region <regionid> --access <accessname> --apiVersion <20210406 or 20160815> --path '{"serviceName": "serviceName","qualifier": "version or alias"}' --query '{"limit": "limit","nextToken": "nextToken","prefix": "prefix","startKey": "startKey"}'

Parameters:

  • (Required) --region string: the region in which you want to deploy resources.

  • (Optional) --access string or -a string: the alias of the key to be used.

  • (Optional) --apiVersion: the API version. Valid values: 20210416 and 20160815.

  • path

    • (Required) --serviceName string: the service name.

    • (Optional) --qualifier string: the service alias or version.

  • query

    • (Optional) --limit string: the number of resources to return.

    • (Optional) --nextToken string: the token that you can use to return more results. Include this parameter in subsequent invocations to obtain more results. You do not need to provide this parameter in the first invocation.

    • (Optional) --prefix string: the name prefix of the returned resources.

    • (Optional) --startKey string: specifies that results are returned alphabetically starting from the first after startKey (including startKey).

Sample code:

sudo s cli fc api ListFunctions --region cn-hangzhou --access default --path '{"serviceName": "mytest","qualifier": "LATEST"}' 

For more information about the API operation for querying functions, see ListFunctions.

Invoke a function

Command syntax:

sudo s cli fc api InvokeFunction --region <regionid> --access <accessname> --apiVersion <20210406 or 20160815> --path '{"serviceName": "serviceName","functionName": "functionName","qualifier": "version or alias"}' --body "hello fc payload" --header '{"X-Fc-Invocation-Type": "Sync or Async","X-Fc-Log-Type": "Tail or None","X-Fc-Stateful-Async-Invocation-Id": "invocationID"}'

Parameters:

  • (Required) --region string: the region in which you want to deploy resources.

  • (Optional) --access string or -a string: the alias of the key to be used.

  • (Optional) --apiVersion: the API version. Valid values: 20210416 and 20160815.

  • path

    • (Required) --serviceName string: the service name.

    • (Required) --functionName string: the function name.

    • (Optional) --qualifier string: the service alias or version.

  • body

    The function requests the actual data.

  • header

    • (Optional) X-Fc-Invocation-Type: the method to invoke the function. Valid values: Sync and Async.

    • (Optional) X-Fc-Log-Type: the method to return logs. Valid values: Tail (returns the last 4 KB of logs for this request) and None (does not return logs). Default value: None.

    • (Optional) X-Fc-Stateful-Async-Invocation-Id: the ID of the stateful asynchronous invocation. If you do not specify this parameter, the system assigns a value by default.

Sample code:

sudo s cli fc api InvokeFunction --region cn-hangzhou --access default --path '{"serviceName": "mytest","functionName": "mytest","qualifier": "LATEST"}'  --body "hello fc payload" --header '{"X-Fc-Invocation-Type": "Async","X-Fc-Log-Type": "None"}'

For more information about the API operation for invoking and executing functions, see InvokeFunction.