ALIYUN::FC::Function类型是用于系统调度和运行的单位。函数必须从属于服务,一个服务下的所有函数都共享该服务的属性,例如授权、日志设置等。

语法

{
  "Type": "ALIYUN::FC::Function",
  "Properties": {
    "Code": Map,
    "FunctionName": String,
    "ServiceName": String,
    "MemorySize": Integer,
    "InstanceConcurrency": Integer,
    "EnvironmentVariables": Map,
    "Initializer": String,
    "Handler": String,
    "Timeout": Integer,
    "InitializationTimeout": Integer,
    "CustomContainerConfig": Map,
    "AsyncConfiguration": Map,
    "CAPort": Integer,
    "Runtime": String,
    "Description": String
  }
}

属性

属性名称 类型 必须 允许更新 描述 约束
Initializer String 初始化函数执行的入口。 具体格式和语言相关。
InitializationTimeout Integer 初始化函数运行的超时时间。 取值范围:1~300。

单位:秒。

默认值:3。

初始化函数时,如果超该时间,则终止执行。
Code Map 指定code zip包。 详情请参见Code属性
Description String 函数的简短描述。
ServiceName String 服务名称。 长度为1~128个字符。
MemorySize Integer 函数的内存规格。 取值范围:128~3072,必须是64的倍数。

单位:MB。

InstanceConcurrency Integer 实例并发度。 取值范围:1~100。
说明 python函数实例不支持该参数。
EnvironmentVariables Map 函数设置的环境变量。
Handler String 函数执行的入口。 以python为例,创建函数时指定的Handler为index.handler,文件名为index.py,入口函数为handler。具体格式和语言相关。
Timeout Integer 函数运行的超时时间。 取值范围:1~600。

默认值:3。

单位:秒。

运行函数时,如果超出该时间,则终止执行。

Runtime String 函数的运行环境。 目前支持nodejs6、nodejs8、nodejs10、nodejs12、python2.7、python3、java8、custom、custom-container。
FunctionName String 函数名称。 长度为1~128个字符。以英文字母或下划线(_)开头,可以包含英文字母、下划线(_)、数字和短划线(-)。
CustomContainerConfig Map Runtime取值为custom-container时的配置,配置后可以使用自定义容器镜像执行函数。 详情请参见CustomContainerConfig属性
CAPort Integer 自定义HTTP Server监听的端口。 默认为9000。
说明 Runtime取值为custom或custom-container时该参数生效。
AsyncConfiguration Map 异步调用配置。 详情请参见AsyncConfiguration属性

Code语法

"Code": {
  "OssBucketName": String,
  "OssObjectName": String,
  "ZipFile": String,
  "SourceCode": String
}

Code属性

属性名称 类型 必须 允许更新 描述 约束
OssBucketName String 函数code zip包的存储空间名称。
OssObjectName String code zip包的对象名。
ZipFile String 在请求体中上传的code zip包的Base64编码。
SourceCode String 函数源码(目前支持Node.js、PHP、Python)。 最长为4096个字符。ROS会将参数值写入一个UTF-8编码的名为index的文件。当同时传入ZipFile、SourceCode和OssBucketName&OssObjectName时,优先级依次为:ZipFile>SourceCode>OssBucketName&OssObjectName。

CustomContainerConfig语法

"CustomContainerConfig": {
  "Command": String,
  "Args": String,
  "Image": String
}

CustomContainerConfig属性

属性名称 类型 必须 允许更新 描述 约束
Command String 容器镜像地址。 示例值:registry-vpc.cn-hangzhou.aliyuncs.com/fc-demo/helloworld:v1beta1
Args String 容器启动参数。 示例值:["-arg1", "value1"]
Image String 容器启动命令。 示例值:["/code/myserver"]

AsyncConfiguration语法

"AsyncConfiguration": {
  "Destination": Map
  "MaxAsyncRetryAttempts": Integer,
  "MaxAsyncEventAgeInSeconds": Integer
}

AsyncConfiguration属性

属性名称 类型 必须 允许更新 描述 约束
Destination Map 异步调用目标。 详情请参见Destination属性
MaxAsyncRetryAttempts Integer 重试次数。
MaxAsyncEventAgeInSeconds Integer 消息最大存活时长。

Destination语法

"Destination": {
  "OnSuccess": String,
  "OnFailure": String
}

Destination属性

属性名称 类型 必须 允许更新 描述 约束
OnSuccess String 当函数执行成功时,函数计算将调用该配置对应的目标。
OnFailure String 当函数执行失败(系统错误或函数内部错误)时,函数计算将调用该配置对应的目标。

返回值

Fn::GetAtt

  • FunctionId:系统为每个函数生成的唯一ID。
  • ServiceName:服务名。
  • ARN:函数的ARN。
  • FunctionName:函数名。

示例

JSON格式

{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Parameters": {
    "MemorySize": {
      "Type": "Number",
      "Description": "The amount of memory that’s used to run function, in MB. Function Compute uses this value to allocate CPU resources proportionally. Defaults to 128 MB. It can be multiple of 64 MB and between 128 MB and 3072 MB.",
      "MinValue": 128,
      "MaxValue": 3072,
      "Default": 128
    },
    "Description": {
      "Type": "String",
      "Description": "Function description"
    },
    "Timeout": {
      "Type": "Number",
      "Description": "The maximum time duration a function can run, in seconds. After which Function Compute terminates the execution. Defaults to 3 seconds, and can be between 1 to 600 seconds.",
      "MinValue": 1,
      "MaxValue": 600,
      "Default": 3
    },
    "Handler": {
      "Type": "String",
      "Description": "The function execution entry point."
    },
    "CustomContainerConfig": {
      "Type": "Json",
      "Description": "Custom container runtime related configuration. After configuration, the function can be replaced with a custom container to execute the function"
    },
    "Code": {
      "Type": "Json",
      "Description": "The code that contains the function implementation."
    },
    "AsyncConfiguration": {
      "Type": "Json",
      "Description": "Configuration of asynchronous function calls"
    },
    "FunctionName": {
      "Type": "String",
      "Description": "Function name"
    },
    "CAPort": {
      "Type": "Number",
      "Description": "Custom runtime and custom container runtime dedicated fields, which represent the port that the started custom http server listens to. The default value is 9000",
      "Default": 9000
    },
    "Runtime": {
      "Type": "String",
      "Description": "The function runtime environment. Supporting nodejs6, nodejs8, nodejs10, nodejs12, python2.7, python3, java8, custom, custom-container and so on"
    },
    "EnvironmentVariables": {
      "Type": "Json",
      "Description": "The environment variable set for the function, you can get the value of the environment variable in the function."
    },
    "ServiceName": {
      "Type": "String",
      "Description": "Service name",
      "MinLength": 1,
      "MaxLength": 128
    },
    "Initializer": {
      "Type": "String",
      "Description": "the entry point of the initializer"
    },
    "InitializationTimeout": {
      "Type": "Number",
      "Description": "the max execution time of the initializer, in second"
    },
    "InstanceConcurrency": {
      "Type": "Number",
      "Description": "Function instance concurrency. Value can be between 1 to 100.",
      "MinValue": 1,
      "MaxValue": 100
    }
  },
  "Resources": {
    "Function": {
      "Type": "ALIYUN::FC::Function",
      "Properties": {
        "MemorySize": {
          "Ref": "MemorySize"
        },
        "Description": {
          "Ref": "Description"
        },
        "Timeout": {
          "Ref": "Timeout"
        },
        "Handler": {
          "Ref": "Handler"
        },
        "CustomContainerConfig": {
          "Ref": "CustomContainerConfig"
        },
        "Code": {
          "Ref": "Code"
        },
        "AsyncConfiguration": {
          "Ref": "AsyncConfiguration"
        },
        "FunctionName": {
          "Ref": "FunctionName"
        },
        "CAPort": {
          "Ref": "CAPort"
        },
        "Runtime": {
          "Ref": "Runtime"
        },
        "EnvironmentVariables": {
          "Ref": "EnvironmentVariables"
        },
        "ServiceName": {
          "Ref": "ServiceName"
        },
        "Initializer": {
          "Ref": "Initializer"
        },
        "InitializationTimeout": {
          "Ref": "InitializationTimeout"
        },
        "InstanceConcurrency": {
          "Ref": "InstanceConcurrency"
        }
      }
    }
  },
  "Outputs": {
    "FunctionId": {
      "Description": "The function ID",
      "Value": {
        "Fn::GetAtt": [
          "Function",
          "FunctionId"
        ]
      }
    },
    "FunctionName": {
      "Description": "The function name",
      "Value": {
        "Fn::GetAtt": [
          "Function",
          "FunctionName"
        ]
      }
    },
    "ServiceName": {
      "Description": "The service name",
      "Value": {
        "Fn::GetAtt": [
          "Function",
          "ServiceName"
        ]
      }
    },
    "ARN": {
      "Description": "The ARN for ALIYUN::ROS::CustomResource",
      "Value": {
        "Fn::GetAtt": [
          "Function",
          "ARN"
        ]
      }
    }
  }
}

YAML格式

ROSTemplateFormatVersion: '2015-09-01'
Parameters:
  MemorySize:
    Type: Number
    Description: >-
      The amount of memory that’s used to run function, in MB. Function Compute
      uses this value to allocate CPU resources proportionally. Defaults to 128
      MB. It can be multiple of 64 MB and between 128 MB and 3072 MB.
    MinValue: 128
    MaxValue: 3072
    Default: 128
  Description:
    Type: String
    Description: Function description
  Timeout:
    Type: Number
    Description: >-
      The maximum time duration a function can run, in seconds. After which
      Function Compute terminates the execution. Defaults to 3 seconds, and can
      be between 1 to 600 seconds.
    MinValue: 1
    MaxValue: 600
    Default: 3
  Handler:
    Type: String
    Description: The function execution entry point.
  CustomContainerConfig:
    Type: Json
    Description: >-
      Custom container runtime related configuration. After configuration, the
      function can be replaced with a custom container to execute the function
  Code:
    Type: Json
    Description: The code that contains the function implementation.
  AsyncConfiguration:
    Type: Json
    Description: Configuration of asynchronous function calls
  FunctionName:
    Type: String
    Description: Function name
  CAPort:
    Type: Number
    Description: >-
      Custom runtime and custom container runtime dedicated fields, which
      represent the port that the started custom http server listens to. The
      default value is 9000
    Default: 9000
  Runtime:
    Type: String
    Description: >-
      The function runtime environment. Supporting nodejs6, nodejs8, nodejs10,
      nodejs12, python2.7, python3, java8, custom, custom-container and so on
  EnvironmentVariables:
    Type: Json
    Description: >-
      The environment variable set for the function, you can get the value of
      the environment variable in the function.
  ServiceName:
    Type: String
    Description: Service name
    MinLength: 1
    MaxLength: 128
  Initializer:
    Type: String
    Description: the entry point of the initializer
  InitializationTimeout:
    Type: Number
    Description: 'the max execution time of the initializer, in second'
  InstanceConcurrency:
    Type: Number
    Description: Function instance concurrency. Value can be between 1 to 100.
    MinValue: 1
    MaxValue: 100
Resources:
  Function:
    Type: 'ALIYUN::FC::Function'
    Properties:
      MemorySize:
        Ref: MemorySize
      Description:
        Ref: Description
      Timeout:
        Ref: Timeout
      Handler:
        Ref: Handler
      CustomContainerConfig:
        Ref: CustomContainerConfig
      Code:
        Ref: Code
      AsyncConfiguration:
        Ref: AsyncConfiguration
      FunctionName:
        Ref: FunctionName
      CAPort:
        Ref: CAPort
      Runtime:
        Ref: Runtime
      EnvironmentVariables:
        Ref: EnvironmentVariables
      ServiceName:
        Ref: ServiceName
      Initializer:
        Ref: Initializer
      InitializationTimeout:
        Ref: InitializationTimeout
      InstanceConcurrency:
        Ref: InstanceConcurrency
Outputs:
  FunctionId:
    Description: The function ID
    Value:
      'Fn::GetAtt':
        - Function
        - FunctionId
  FunctionName:
    Description: The function name
    Value:
      'Fn::GetAtt':
        - Function
        - FunctionName
  ServiceName:
    Description: The service name
    Value:
      'Fn::GetAtt':
        - Function
        - ServiceName
  ARN:
    Description: 'The ARN for ALIYUN::ROS::CustomResource'
    Value:
      'Fn::GetAtt':
        - Function
        - ARN