ALIYUN::FC::Function类型用于创建函数。函数必须从属于服务,一个服务下的所有函数都共享该服务的属性,例如:授权、日志设置等。
语法
{
"Type": "ALIYUN::FC::Function",
"Properties": {
"Code": Map,
"FunctionName": String,
"ServiceName": String,
"InstanceType": 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属性。 |
InstanceType | String | 否 | 是 | 函数实例类型。 | 取值:
|
Description | String | 否 | 是 | 函数的描述。 | 无 |
ServiceName | String | 是 | 否 | 服务名称。 | 长度为1~128个字符。 |
MemorySize | Integer | 否 | 是 | 函数的内存规格。 | 取值:
单位: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 | 否 | 是 | 函数源码。 | 最长为4096个字符。支持Node.js、PHP、Python语言。
ROS会将参数值写入一个UTF-8编码的名为index的文件。 当同时指定ZipFile、SourceCode、OssBucketName和OssObjectName时,优先级由高到低依次为:ZipFile、SourceCode、OssBucketName和OssObjectName。 |
CustomContainerConfig语法
"CustomContainerConfig": {
"Command": String,
"Args": String,
"Image": String,
"AccelerationType": String,
"InstanceId": String
}
CustomContainerConfig属性
属性名称 | 类型 | 必须 | 允许更新 | 描述 | 约束 |
---|---|---|---|---|---|
Command | String | 否 | 是 | 容器启动命令。 | 取值示例:["/code/myserver"] 。
|
Args | String | 否 | 是 | 容器启动参数。 | 取值示例:["-arg1", "value1"] 。
|
Image | String | 是 | 是 | 容器镜像地址。 | 取值示例:registry-vpc.cn-hangzhou.aliyuncs.com/fc-demo/helloworld:v1beta1 。
|
AccelerationType | String | 否 | 是 | 是否开启镜像加速。 | 取值:
|
InstanceId | String | 否 | 是 | 容器镜像服务企业版实例的ID。 | 当容器镜像选择的是企业版实例时,您需要添加企业版实例ID,该实例的默认解析IP必须是服务所在的VPC网络地址。
说明 目前不支持使用PrivateZone产品定义域名解析。
|
AsyncConfiguration语法
"AsyncConfiguration": {
"Destination": Map
"MaxAsyncRetryAttempts": Integer,
"MaxAsyncEventAgeInSeconds": Integer,
"StatefulInvocation": Boolean
}
AsyncConfiguration属性
属性名称 | 类型 | 必须 | 允许更新 | 描述 | 约束 |
---|---|---|---|---|---|
Destination | Map | 否 | 否 | 异步调用目标。 | 更多信息,请参见Destination属性。 |
MaxAsyncRetryAttempts | Integer | 否 | 是 | 重试次数。 | 无 |
MaxAsyncEventAgeInSeconds | Integer | 否 | 是 | 消息最大存活时长。 | 无 |
StatefulInvocation | Boolean | 否 | 是 | 是否开启有状态异步调用。 | 取值:
更多信息,请参见有状态异步调用。 |
Destination语法
"Destination": {
"OnSuccess": String,
"OnFailure": String
}
Destination属性
属性名称 | 类型 | 必须 | 允许更新 | 描述 | 约束 |
---|---|---|---|---|---|
OnSuccess | String | 否 | 是 | 当函数执行成功时,函数计算将调用该配置对应的目标。 | 无 |
OnFailure | String | 否 | 是 | 当函数执行失败(系统错误或函数内部错误)时,函数计算将调用该配置对应的目标。 | 无 |
返回值
Fn::GetAtt
- FunctionId:系统为每个函数生成的唯一ID。
- ServiceName:服务名称。
- ARN:函数的ARN。
- FunctionName:函数名。
- ServiceId:函数服务ID。
示例
JSON
格式
{
"ROSTemplateFormatVersion": "2015-09-01",
"Parameters": {
"ServiceName": {
"Type": "String",
"Default": "mytest"
},
"FunctionName": {
"Type": "String",
"Default": "mytest"
},
"Timeout": {
"Type": "Number",
"Default": 60
},
"X": {
"Type": "Number",
"Default": 1
},
"Y": {
"Type": "Number",
"Default": 2
}
},
"Resources": {
"Service": {
"Type": "ALIYUN::FC::Service",
"Properties": {
"ServiceName": {
"Ref": "ServiceName"
}
}
},
"Function": {
"Type": "ALIYUN::FC::Function",
"Properties": {
"ServiceName": {
"Fn::GetAtt": [
"Service",
"ServiceName"
]
},
"FunctionName": {
"Ref": "FunctionName"
},
"Handler": "index.handler",
"Runtime": "python2.7",
"Code": {
"SourceCode": "import time\nimport json\nimport urllib2\nimport logging\n\n\ndef handler(event, context):\n logger = logging.getLogger()\n\n event = json.loads(event)\n logger.info('receive request: %s', event)\n\n res_props = event['ResourceProperties']\n\n result = dict(\n RequestId=event['RequestId'],\n LogicalResourceId=event['LogicalResourceId'],\n StackId=event['StackId'],\n Status='SUCCESS',\n PhysicalResourceId='dummy'\n )\n if event['RequestType'] != 'Delete':\n result['Data'] = dict(z=res_props['X'] + res_props['Y'])\n\n headers = {\n 'Content-type': 'application/json',\n 'Accept': 'application/json',\n 'Date': time.strftime('%a, %d %b %Y %X GMT', time.gmtime())\n }\n req = urllib2.Request(event['ResponseURL'], data=json.dumps(result), headers=headers)\n resp = urllib2.urlopen(req)\n resp_content = resp.read()\n logger.info('response: %s', resp_content)\n"
}
}
},
"SimpleTest": {
"Type": "Custom::Add",
"Properties": {
"ServiceToken": {
"Fn::GetAtt": [
"Function",
"ARN"
]
},
"Parameters": {
"X": {
"Ref": "X"
},
"Y": {
"Ref": "Y"
}
},
"Timeout": {
"Ref": "Timeout"
}
}
}
},
"Outputs": {
"SimpleTestOutputs": {
"Value": {
"Fn::GetAtt": [
"SimpleTest",
"Outputs"
]
}
}
}
}