All Products
Search
Document Center

Function Compute (2.0):CreateFunction

Last Updated:Aug 10, 2023

Creates a function.

Request headers

This operation uses common request headers and special request headers. The following table describes the special request header. For information about common request headers, see Common parameters.

Parameter Type Required Example Description
X-Fc-Code-Checksum String No 543402527838814****

The CRC-64 value of the function code package.

Request syntax

POST /services/{serviceName}/functions HTTP/1.1

Request parameters

Parameter Type Position Required Example Description
serviceName String Path Yes service_name

The name of the service to which the instance belongs. The service name can only contain letters, digits, underscores (_), and hyphens (-). It cannot start with a digit or hyphen (-) and must be 1 to 128 characters in length.

Object Body No

The definition of the function.

code Code Body No

The code for the layer. The code must be packaged into a ZIP file.

customContainerConfig CustomContainerConfig Body No

The configurations of a Custom Container runtime. After you configure the configurations, you can use a custom container image to run functions.

layers Array of String Body No 02f81d283888f5ec63442a88fe82b260#Layer-name#1

The name of the layer.

description String Body No test_description

The description of the function.

functionName String Body Yes function_name

The name of the function. The function name can only contain letters, digits, underscores (_), and hyphens (-). It cannot start with a digit or hyphen (-) and must be 1 to 64 characters in length.

handler String Body Yes index.handler

The handler of the function. The format is determined by the programming language that you use. For more information, see Function handlers.

initializationTimeout Integer Body No 30

The timeout period for the execution of the Initializer hook. Unit: seconds. Default value: 3. Valid values: 1 to 300. When this period ends, the execution of the Initializer hook is terminated.

initializer String Body No index.initializer

The handler of the Initializer hook. The format of the value is determined by the programming language that you use. For more information, see Initializer hook.

memorySize Integer Body No 256

The memory size for the function. Unit: MB. The memory size is a multiple of 64 MB. The memory sizes of instances vary based on the type of the function instance. For more information, see Instance specifications.

runtime String Body No python3

The runtime of the function. Valid values: nodejs14, nodejs12, nodejs10, nodejs8, nodejs6, nodejs4.4, python3, python2.7, java11, java8, go1, php7.2, dotnetcore2.1, custom, and custom-container. For more information, see the "Runtimes supported by Function Compute" section in Manage functions.

timeout Integer Body No 60

The timeout period for Function Compute to execute the function. Unit: seconds. Default value: 60. Valid values: 1 to 86400. When the period expires, the function execution is terminated.

caPort Integer Body No 9000

The port on which the HTTP server listens for a custom runtime or Custom Container runtime.

Function code packages can be provided in the following two methods. You must use only one of the methods in a single request.

  • Specify the name of the Object Storage Service (OSS) bucket (ossBucketName) in which the code package is stored and the name of the object (ossObjectName).
  • Set the value of the zipFile parameter to the Base64-encoded content of the ZIP file.

Response parameters

Parameter Type Example Description
ETag String 738136ea26b79cee660862cd7628****

The value used to ensure that the modified function is consistent with the function to be modified.

codeChecksum String 2825179536350****

The CRC-64 value of the function code package.

codeSize Long 421

The size of the function code package. Unit: byte.

createdTime String 2020-04-01T08:15:27Z

The time when the function was created.

customContainerConfig CustomContainerConfig

The configurations of a Custom Container runtime. After you configure the configurations, you can use a custom container image to run functions.

layers Array of String 02f81d283888f5ec63442a88fe82b260#Layer-name#1

The name of the layer.

description String test_description

The description of the function.

environmentVariables Map

The environment variables that are configured for the function. You can obtain the values of the environment variables from the function. For more information, see Environment variables.

functionId String fc46822c-0fb9-4c15-b898-bace828a****

The ID that is generated by the system for each function. The ID must be globally unique.

functionName String function_name

The name of the function.

handler String index.handler

The handler of the function. The format is determined by the programming language that you use. For more information, see Function handlers.

lastModifiedTime Long 2020-04-01T08:15:27Z

The time when the function was last modified.

memorySize Integer 256

The memory size for the function. Unit: MB. The memory size is a multiple of 64 MB. The memory sizes for instances vary based on the types of the function instances. For more information, see Instance specifications.

runtime String python3

The runtime of the function. Valid values: nodejs14, nodejs12, nodejs10, nodejs8, nodejs6, nodejs4.4, python3, python2.7, java11, java8, go1, php7.2, dotnetcore2.1, custom, and custom-container. For more information, see the "Runtimes supported by Function Compute" section in Manage functions.

timeout Integer 60

The timeout period for Function Compute to execute the function. Unit: seconds. Default value: 60. Valid values: 1 to 86400. When the period expires, the function execution is terminated.

initializationTimeout Integer 60

The timeout period for the execution of the Initializer hook. Unit: seconds. Default value: 3. Valid values: 1 to 300. When this period ends, the execution of the Initializer hook is terminated.

initializer String index.handler

The handler of the Initializer hook. The format of the value is determined by the programming language that you use. For more information, see Initializer hook.

caPort Integer 9000

The port on which the HTTP server listens for a custom runtime or Custom Container runtime.

Example

Sample requests

POST /services/service_name/functions HTTP/1.1
Host:fc-ram.aliyuncs.com
X-Fc-Code-Checksum:543402527838814****
Content-Type:application/json

{
  "code" : {
    "ossBucketName" : "demo-bucket",
    "ossObjectName" : "demo-key",
    "zipFile" : "cHJpbnQoImhlbGxvIHdvcmxkIikK"
  },
  "customContainerConfig" : {
    "args" : "[\"-arg1\", \"value1\"]",
    "command" : "[\"/code/myserver\"]",
    "image" : "registry-vpc.cn-hangzhou.aliyuncs.com/fc-demo/helloworld:v1beta1",
    "accelerationType" : "Default",
    "instanceID" : "cri-xxxxxxxx"
  },
  "layers" : [ "02f81d283888f5ec63442a88fe82b260#Layer-name#1" ],
  "description" : "test_description",
  "functionName" : "function_name",
  "handler" : "index.handler",
  "initializationTimeout" : 30,
  "initializer" : "index.initializer",
  "memorySize" : 256,
  "runtime" : "python3",
  "timeout" : 60,
  "caPort" : 9000
}

Sample success responses

JSON format

HTTP/1.1 200 OK
Content-Type:application/json

{
  "codeChecksum" : "2825179536350****",
  "codeSize" : 421,
  "createdTime" : "2020-04-01T08:15:27Z",
  "customContainerConfig" : {
    "args" : "[\"-arg1\", \"value1\"]",
    "command" : "[\"/code/myserver\"]",
    "image" : "registry-vpc.cn-hangzhou.aliyuncs.com/fc-demo/helloworld:v1beta1",
    "accelerationType" : "Default",
    "instanceID" : "cri-xxxxxxxx"
  },
  "layers" : [ "02f81d283888f5ec63442a88fe82b260#Layer-name#1" ],
  "description" : "test_description",
  "functionId" : "aa715851-1c20-4b89-a8fb-***",
  "functionName" : "function_name",
  "handler" : "index.handler",
  "memorySize" : 256,
  "runtime" : "python3",
  "timeout" : 60,
  "initializationTimeout" : 60,
  "initializer" : "index.handler",
  "caPort" : 9000
}