函数相关命令

更新时间: 2023-12-01 14:17:49

FC组件是一款基于Serverless Devs的阿里云函数计算操作工具,通过该工具,您可以直接通过交互式命令使用函数相关的API。

前提条件

创建函数

命令格式:

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"}'

参数说明:

  • (必选)--region string:指定部署资源的地域。

  • (可选)--access string-a string:指定使用的密钥别名。

  • (可选)--apiVersion:指定API版本。取值包括2021040620160815

  • path

    • (必选)--serviceName string:指定服务名称。

  • body

    • (可选)--code string:指定压缩的代码文件,代码文件必须是ZIP格式。

    • (可选)--customContainerConfig string:配置Custom Container运行时的参数。配置后函数可以使用自定义容器镜像执行函数。

    • (可选)--layers string:指定层资源名称。

    • (可选)--description string:指定函数中的描述信息。

    • (必选)--functionName string:指定函数名称。

    • (必选)--handler string:设置函数handler,handler的格式为“文件名.函数名”。例如hello_world.handler指定了函数的调用入口为hello_world.js文件中的handler函数。

    • (可选)--initializationTimeout number:设置初始化函数超时时间。

    • (可选)--initializer string:设置初始化函数。

    • (可选)--memorySize number:设置函数执行的内存大小。

    • (必选)--runtime string:指定运行环境。

    • (可选)--timeout number:设置函数执行的超时时间(默认60s)。

    • (可选)--caPort number:指定Custom Runtime或Custom Container运行时HTTP Server的监听端口。

    • (可选)environmentVariables:指定环境变量信息。

    • (可选)instanceConcurrency:配置函数实例并发度。

    • (可选)instanceType:指定函数实例类型。

    • (可选)instanceLifecycleConfig:配置实例生命周期函数。

    • (可选)customDNS:配置自定义DNS域名。

    • (可选)customRuntimeConfig:配置自定义运行环境。

  • header

    • (可选)X-Fc-Code-Checksum:指定函数代码包的CRC-64值。

执行示例:

  • macOS操作系统或Linux操作系统,根据运行环境的不同选择执行示例:

    • 当您的运行环境是编程语言运行时,请执行以下命令创建函数。

      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"}'
    • 当您的运行环境是自定义容器时,请执行以下命令创建函数。

      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操作系统

    • 当您的运行环境是编程语言运行时,请执行以下命令创建函数。

      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"}'
    • 当您的运行环境是自定义容器时,请执行以下命令创建函数。

      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"}'

关于创建函数的API接口的详细信息,请参见CreateFunction

删除函数

命令格式:

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

参数说明:

  • (必选)--region string:指定部署资源的地域。

  • (可选)--access string-a string:指定使用的密钥别名。

  • (可选)--apiVersion:指定API版本。取值包括2021040620160815

  • path

    • (必选)--serviceName string:指定服务名称。

    • (必选)--functionName string:指定函数名称。

  • header

    (可选)--If-Match string:用于确保实际更改的资源与期望更改的资源一致。

执行示例:

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

关于删除函数的API接口的详细信息,请参见DeleteFunction

更新函数

命令格式:

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"}'

参数说明:

  • (必选)--region string:指定部署资源的地域。

  • (可选)--access string-a string:指定使用的密钥别名。

  • (可选)--apiVersion:指定API版本。取值包括2021040620160815

  • path

    • (必选)--serviceName string:指定服务名称。

    • (必选)--functionName string:指定函数名称。

  • body

    • (可选)--code string:指定压缩的代码文件,代码文件必须是ZIP格式。

    • (可选)--customContainerConfig string:配置Custom Container运行时的参数。配置后函数可以使用自定义容器镜像执行函数。

    • (可选)--layers string:指定层资源名称。

    • (可选)--description string:指定函数中的描述信息。

    • (必选)--handler string:设置函数handler,handler的格式为“文件名.函数名”。例如hello_world.handler指定了函数的调用入口为hello_world.js文件中的handler函数。

    • (可选)--initializationTimeout number:设置初始化函数超时时间。

    • (可选)--initializer string:设置初始化函数。

    • (可选)--memorySize number:设置函数执行的内存大小。

    • (必选)--runtime string:指定运行环境。

    • (可选)--timeout number:设置函数执行的超时时间(默认60s)。

    • (可选)--caPort number:指定Custom Runtime或Custom Container运行时HTTP Server的监听端口。

    • (可选)environmentVariables:指定环境变量信息。

    • (可选)instanceConcurrency:配置函数实例并发度。

    • (可选)instanceType:指定函数实例类型。

    • (可选)instanceLifecycleConfig:配置实例生命周期函数。

    • (可选)customDNS:配置自定义DNS域名。

    • (可选)customRuntimeConfig:配置自定义运行环境。

  • header

    • (可选)--If-Match string:用于确保实际更改的资源与期望更改的资源一致。

    • (可选)X-Fc-Code-Checksum:指定函数代码包的CRC-64值。

执行示例:

  • macOS操作系统或Linux操作系统

    • 当您的运行环境是编程语言运行时,请执行以下命令更新函数:

      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}'
    • 当您的运行环境是自定义容器时,请执行以下命令更新函数:

      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操作系统

    • 当您的运行环境是编程语言运行时,请执行以下命令更新函数:

      s cli fc api UpdateFunction --region cn-hangzhou --access default --path '{"serviceName": "mytest","functionName": "mytest"}' --body '{"code": {\"ossBucketName\": \"testbucket\",\"ossObjectName\": \"code.zip\"},"timeout": 31}'
    • 当您的运行环境是自定义容器时,请执行以下命令更新函数:

      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}'

关于更新函数的API接口的详细信息,请参见UpdateFunction

获取函数配置信息

命令格式:

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

参数说明:

  • (必选)--region string:指定部署资源的地域。

  • (可选)--access string-a string:指定使用的密钥别名。

  • (可选)--apiVersion:指定API版本。取值包括2021040620160815

  • path

    • (必选)--serviceName string:指定服务名称。

    • (必选)--functionName string:指定函数名称。

    • (可选)--qualifier string:指定服务的别名或版本。

执行示例:

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

关于获取函数配置信息的API接口的详细信息,请参见GetFunction

获取函数代码信息

命令格式:

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

参数说明:

  • (必选)--region string:指定部署资源的地域。

  • (可选)--access string-a string:指定使用的密钥别名。

  • (可选)--apiVersion:指定API版本。取值包括2021040620160815

  • path

    • (必选)--serviceName string:指定服务名称。

    • (必选)--functionName string:指定函数名称。

    • (可选)--qualifier string:指定服务的别名或版本。

执行示例:

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

关于获取函数代码信息的API接口的详细信息,请参见GetFunctionCode

查询函数列表

命令格式:

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"}'

参数说明:

  • (必选)--region string:指定部署资源的地域。

  • (可选)--access string-a string:指定使用的密钥别名。

  • (可选)--apiVersion:指定API版本。取值包括2021040620160815

  • path

    • (必选)--serviceName string:指定服务名称。

    • (可选)--qualifier string:指定服务的别名或版本。

  • query

    • (可选)--limit string:设置限定此次返回资源的数量。

    • (可选)--nextToken string:设置用来返回更多结果的令牌。第一次查询时不需要提供这个参数,后续查询的Token从返回结果中获取。

    • (可选)--prefix string:设置返回资源的名称前缀。

    • (可选)--startKey string:设定结果从startKey之后(包括startKey)按字母排序的第一个开始返回。

执行示例:

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

关于获取函数列表的API接口的详细信息,请参见ListFunctions

调用执行函数

命令格式:

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"}'

参数说明:

  • (必选)--region string:指定部署资源的地域。

  • (可选)--access string-a string:指定使用的密钥别名。

  • (可选)--apiVersion:指定API版本。取值包括2021040620160815

  • path

    • (必选)--serviceName string:指定服务名称。

    • (必选)--functionName string:指定函数名称。

    • (可选)--qualifier string:指定服务的别名或版本。

  • body

    函数请求实际数据。

  • header

    • (可选)X-Fc-Invocation-Type:指定调用函数的方式。取值为Sync(同步)和Async(异步)。

    • (可选)X-Fc-Log-Type:指定返回log的方式。取值为Tail(返回本次请求最后4 KB的log)和None(不返回)。默认值为None。

    • (可选)X-Fc-Stateful-Async-Invocation-Id:设置有状态异步调用的ID,如果不指定,系统默认赋值。

执行示例:

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"}'

关于调用执行函数的API接口的详细信息,请参见InvokeFunction

阿里云首页 函数计算(旧版) 相关技术圈