ALIYUN::FC::Function is used to create a function. A function is an object that the system uses for scheduling and execution. Functions must be associated with services. All functions of a service share the same properties as the service, such as service authorizations and logging configurations.

Syntax

{
  "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,
    "Runtime": String,
    "Description": String
  }
}

Properties

Property Type Required Editable Description Constraint
Initializer String No Yes The entry point for Function Compute to initialize the created function. The format is determined by the programming language.
InitializationTimeout Integer No Yes The timeout period for Function Compute to initialize the function. Unit: seconds. Valid values: 1 to 300. Default value: 3. If the function is not initialized within the specified period, Function Compute terminates the initialization.
Code Map Yes Yes The code that contains the function invocation. The code must be packaged into a ZIP file. None
Description String No Yes The description of the function. None
ServiceName String Yes No The name of the service. The name must be 1 to 128 characters in length.
MemorySize Integer No Yes The memory size of the function. Valid values: 128 to 1536. The value must be a multiple of 64.

Unit: MB.

InstanceConcurrency Integer No Yes The maximum number of requests that can be concurrently processed by a single instance. Python functions do not support this parameter. Valid values: 1 to 100.
EnvironmentVariables Map No Yes The environment variables for the function. None
Handler String Yes Yes The entry point for Function Compute to invoke the function. For example, if you set Handler to index.handler when you create a Python function, Function Compute loads the handler function defined in the index.py file. The format is determined by the programming language.
Timeout Integer No Yes The timeout period for Function Compute to invoke the function. Valid values: 1 to 300.

Default value: 3.

Unit: seconds.

If the function is not invoked within the specified period, Function Compute terminates the invocation.

Runtime String Yes Yes The runtime environment of the function. Valid values: nodejs6, nodejs8, python2.7, python3, and java8.
FunctionName String Yes No The name of the function. The name must be 1 to 128 characters in length and can contain digits, letters, hyphens (-), and underscores (_). It must start with a letter or underscore (_).

Code syntax

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

Code properties

Property Type Required Editable Description Constraint
OssBucketName String No Yes The name of the bucket where the function code ZIP file is stored. None
OssObjectName String No Yes The name of the object where the function code ZIP file is stored. None
ZipFile String No Yes The Base64-encoded ZIP file to be uploaded in the request body. None
SourceCode String No Yes The source code of the function. Node.js, PHP, and Python code is supported. The code can be up to 4,096 characters in length. ROS writes the value of this parameter into a UTF-8 encoded file that is named index. When the ZipFile, SourceCode, and OssBucketName&OssObjectName parameters are all specified, the three parameters are in descending order of priority.

Response parameters

Fn::GetAtt

  • FunctionId: the unique ID generated by the system for each function.
  • ServiceName: the name of the service to which the function belongs.
  • ARN: the Alibaba Cloud Resource Name (ARN) of the function.
  • FunctionName: the name of the function.

Examples

JSON format

{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Parameters": {
    "ServiceName": {
      "Type": "String",
      "Description": "FC ServiceName",
      "Default": "fvt-ros-test"
    },
    "SourceCode": {
      "Type": "String",
      "Description": "Function SourceCode",
      "Default": "def handler(event, context):\n\treturn 'Hello World!'"
    },
    "Handler": {
      "Type": "String",
      "Description": "Handler",
      "Default": "index.handler"
    },
    "Runtime": {
      "Type": "String",
      "Description": "Runtime",
      "Default": "python3"
    },
    "FunctionName": {
      "Type": "String",
      "Description": "Function Name",
      "Default": "PythonFunc"
    }
  },
  "Resources": {
    "Service": {
      "Type": "ALIYUN::FC::Service",
      "Properties": {
        "ServiceName": {
          "Ref": "ServiceName"
        }
      }
    },
    "Function": {
      "DependsOn": "Service",
      "Type": "ALIYUN::FC::Function",
      "Properties": {
        "ServiceName": {
          "Ref": "ServiceName"
        },
        "Code": {
          "SourceCode": {
            "Ref": "SourceCode"
          }
        },
        "Handler": {
          "Ref": "Handler"
        },
        "Runtime": {
          "Ref": "Runtime"
        },
        "FunctionName": {
          "Ref": "FunctionName"
        }
      }
    }
  },
  "Outputs": {
    "FunctionId": {
      "Value": {
        "Fn::GetAtt": [
          "Function", "FunctionId"
        ]
      }
    }
  }
}

YAML format

ROSTemplateFormatVersion: '2015-09-01'
Parameters:
  ServiceName:
    Type: String
    Description: FC ServiceName
    Default: fvt-ros-test
  SourceCode:
    Type: String
    Description: Function SourceCode
    Default: "def handler(event, context):\n\treturn 'Hello World!'"
  Handler:
    Type: String
    Description: Handler
    Default: index.handler
  Runtime:
    Type: String
    Description: Runtime
    Default: python3
  FunctionName:
    Type: String
    Description: Function Name
    Default: PythonFunc
Resources:
  Service:
    Type: ALIYUN::FC::Service
    Properties:
      ServiceName:
        Ref: ServiceName
  Function:
    DependsOn: Service
    Type: ALIYUN::FC::Function
    Properties:
      ServiceName:
        Ref: ServiceName
      Code:
        SourceCode:
          Ref: SourceCode
      Handler:
        Ref: Handler
      Runtime:
        Ref: Runtime
      FunctionName:
        Ref: FunctionName
Outputs:
  FunctionId:
    Value:
      Fn::GetAtt:
      - Function
      - FunctionId