文档

ALIYUN::FC::Function

更新时间:

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,
    "Cpu": Number,
    "CustomRuntimeConfig": Map,
    "GpuMemorySize": Integer,
    "InstanceSoftConcurrency": Integer,
    "DiskSize": Integer,
    "InstanceLifecycleConfig": Map,
    "CustomDNS": Map,
    "CustomHealthCheckConfig": Map
  }
}

属性

属性名称

类型

必须

允许更新

描述

约束

Initializer

String

初始化函数执行的入口。

具体格式和语言相关。

InitializationTimeout

Integer

初始化函数运行的超时时间。

取值范围:1~300。

单位:秒。

默认值:3。

初始化函数时,如果超该时间,则终止执行。

Code

Map

指定Code ZIP包。

更多信息,请参见Code属性

InstanceType

String

函数实例类型。

取值:

  • e1:弹性实例。

  • c1:性能实例。

Description

String

函数的描述。

ServiceName

String

服务名称。

长度为1~128个字符。

MemorySize

Integer

函数的内存规格。

取值:

  • 弹性实例:128~3072,必须是64的倍数。

  • 性能实例:4096、8192、16384、32768。

单位: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属性

Cpu

Number

函数的CPU规格。

无。

CustomRuntimeConfig

Map

Custom Runtime函数详细配置。

无。

GpuMemorySize

Integer

function的GPU显存规格。

单位为MB,为1024MB的倍数。

InstanceSoftConcurrency

Integer

实例软并发度,用于优雅扩容。

当实例上的并发数超过软并发度时,会触发实例扩容。例如,您的实例启动较慢,可以通过设置合适的软实例并发度,提前启动实例。

取值小于或等于InstanceConcurrency。

DiskSize

Integer

函数的磁盘规格。

单位为MB。

可选值为512 MB或10240 MB。

InstanceLifecycleConfig

Map

实例生命周期函数配置。

更多信息,请参见InstanceLifecycleConfig属性

CustomDNS

Map

函数自定义DNS配置。

更多信息,请参见CustomDNS属性

CustomHealthCheckConfig

Map

Custom Runtime和Custom Container自定义健康检查配置。

更多信息,请参见CustomHealthCheckConfig属性

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,
  "WebServerMode": Boolean
}

CustomContainerConfig属性

属性名称

类型

必须

允许更新

描述

约束

Command

String

容器启动命令。

取值示例:["/code/myserver"]

Args

String

容器启动参数。

取值示例:["-arg1", "value1"]

Image

String

容器镜像地址。

取值示例:registry-vpc.cn-hangzhou.aliyuncs.com/fc-demo/helloworld:v1beta1

AccelerationType

String

是否开启镜像加速。

取值:

  • Default:开启。

  • None(默认值):关闭。

InstanceId

String

容器镜像服务企业版实例的ID。

当容器镜像选择的是企业版实例时,您需要添加企业版实例ID,该实例的默认解析IP必须是服务所在的VPC网络地址。

说明

目前不支持使用PrivateZone产品定义域名解析。

WebServerMode

Boolean

镜像运行是否为Web Server模式。

取值:

  • true(默认值):表示需要在容器镜像中实现Web Server来监听端口并处理请求。

  • false:表示需要容器运行后主动退出进程,并且ExitCode需要为0。

AsyncConfiguration语法

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

AsyncConfiguration属性

属性名称

类型

必须

允许更新

描述

约束

Destination

Map

异步调用目标。

更多信息,请参见Destination属性

MaxAsyncRetryAttempts

Integer

最大重试次数。

MaxAsyncEventAgeInSeconds

Integer

消息最大存活时长。

StatefulInvocation

Boolean

是否开启有状态异步调用。

取值:

  • true:开启。

  • false(默认值):关闭。

更多信息,请参见功能概览

Destination语法

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

Destination属性

属性名称

类型

必须

允许更新

描述

约束

OnSuccess

String

当函数执行成功时,函数计算将调用该配置对应的目标。

OnFailure

String

当函数执行失败(系统错误或函数内部错误)时,函数计算将调用该配置对应的目标。

InstanceLifecycleConfig语法

"InstanceLifecycleConfig":{
  "PreFreeze": Map,
  "PreStop": Map
}

InstanceLifecycleConfig属性

属性名称

类型

必须

允许更新

描述

约束

PreFreeze

Map

生命周期函数配置。

更多信息,请参见PreFreeze属性

PreStop

Map

生命周期函数配置。

更多信息,请参见PreStop属性

PreStop语法

"PreStop":{
  "Handler": String,
  "Timeout": Integer
}

PreStop属性

属性名称

类型

必须

允许更新

描述

约束

Handler

String

函数执行的入口。

更多信息,请参见基础信息

Timeout

Integer

运行的超时时间。

单位为秒。

PreFreeze语法

"PreFreeze":{
  "Handler": String,
  "Timeout": Integer
}

PreFreeze属性

属性名称

类型

必须

允许更新

描述

约束

Handler

String

函数执行的入口。

更多信息,请参见基础信息

Timeout

Integer

运行的超时时间。

单位为秒。

CustomHealthCheckConfig语法

"CustomHealthCheckConfig":{
  "SuccessThreshold": Integer,
  "InitialDelaySeconds": Integer,
  "PeriodSeconds": Integer,
  "HttpGetUrl": String,
  "TimeoutSeconds": Integer,
  "FailureThreshold": Integer
}

CustomHealthCheckConfig属性

属性名称

类型

必须

允许更新

描述

约束

SuccessThreshold

Integer

健康检查成功次数阈值。

达到该值后系统认为检查成功。

取值范围1~120。

默认值为1。

InitialDelaySeconds

Integer

容器启动到发起健康检查的延迟。

取值范围0~120。

默认值为0。

PeriodSeconds

Integer

健康检查周期。

取值范围1~120。

默认值为3。

HttpGetUrl

String

容器自定义健康检查URL地址。

长度不超过2048个字符。

TimeoutSeconds

Integer

健康检查超时时间。

取值范围1~3。

默认值为1。

FailureThreshold

Integer

健康检查失败次数阈值。

达到该值后系统认为检查失败。

取值范围1~120。

默认值为3。

CustomRuntimeConfig语法

"CustomRuntimeConfig":{
  "Args": List,
  "Command": List,
}

CustomRuntimeConfig属性

属性名称

类型

必须

允许更新

描述

约束

Args

List

启动入口命令接收的参数。

示例值:

["-arg1", "value1"]

Command

List

启动入口命令。

示例值:

["/code/myserver"]

CustomDNS语法

"CustomDNS":{
  "NameServers": List,
  "DnsOptions": List,
  "Searches": List
}

CustomDNS属性

属性名称

类型

必须

允许更新

描述

约束

NameServers

List

DNS服务器的IP地址。

DnsOptions

List

resolv.conf文件中的配置项。

Searches

List

DNS搜索域值。

DnsOptions语法

"DnsOptions":{
  "Value": String,
  "Name": String,
}

DnsOptions属性

属性名称

类型

必须

允许更新

描述

约束

Value

String

配置项值。

示例值:

2。

Name

String

配置项名称。

示例值:

ndots。

返回值

Fn::GetAtt

  • FunctionId:系统为每个函数生成的唯一ID。

  • ServiceName:服务名称。

  • ARN:函数的ARN。

  • FunctionName:函数名。

  • ServiceId:函数服务ID。

示例

YAML格式

ROSTemplateFormatVersion: '2015-09-01'
Parameters: {}
Resources:
  Service:
    Type: ALIYUN::FC::Service
    Properties:
      ServiceName: mytest
  Function:
    Type: ALIYUN::FC::Function
    Properties:
      ServiceName:
        Fn::GetAtt:
          - Service
          - ServiceName
      FunctionName: mytest
      Handler: index.handler
      Runtime: python2.7
      Code:
        SourceCode: |
          import time
          import json
          import urllib2
          import logging


          def handler(event, context):
              logger = logging.getLogger()

              event = json.loads(event)
              logger.info('receive request: %s', event)

              res_props = event['ResourceProperties']

              result = dict(
                  RequestId=event['RequestId'],
                  LogicalResourceId=event['LogicalResourceId'],
                  StackId=event['StackId'],
                  Status='SUCCESS',
                  PhysicalResourceId='dummy'
              )
              if event['RequestType'] != 'Delete':
                  result['Data'] = dict(z=res_props['X'] + res_props['Y'])

              headers = {
                  'Content-type': 'application/json',
                  'Accept': 'application/json',
                  'Date': time.strftime('%a, %d %b %Y %X GMT', time.gmtime())
              }
              req = urllib2.Request(event['ResponseURL'], data=json.dumps(result), headers=headers)
              resp = urllib2.urlopen(req)
              resp_content = resp.read()
              logger.info('response: %s', resp_content)
  SimpleTest:
    Type: Custom::Add
    Properties:
      ServiceToken:
        Fn::GetAtt:
          - Function
          - ARN
      Parameters:
        X: 1
        'Y': 1
      Timeout: 60
Outputs:
  SimpleTestOutputs:
    Value:
      Fn::GetAtt:
        - SimpleTest
        - Outputs                       

JSON格式

{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Parameters": {
  },
  "Resources": {
    "Service": {
      "Type": "ALIYUN::FC::Service",
      "Properties": {
        "ServiceName": "mytest"
      }
    },
    "Function": {
      "Type": "ALIYUN::FC::Function",
      "Properties": {
        "ServiceName": {
          "Fn::GetAtt": [
            "Service",
            "ServiceName"
          ]
        },
        "FunctionName": "mytest",
        "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": 1,
          "Y": 1
        },
        "Timeout": 60
      }
    }
  },
  "Outputs": {
    "SimpleTestOutputs": {
      "Value": {
        "Fn::GetAtt": [
          "SimpleTest",
          "Outputs"
        ]
      }
    }
  }
}

关于以下操作的更多示例,请参见YAML示例

  • 创建函数服务

  • 创建函数

  • 执行函数

  • 触发函数执行

  • 发布版本

  • 创建别名

  • 创建预留实例

  • 本页导读 (1)
文档反馈