Updated on 2024-11-07 GMT+08:00

Creating a Function

Function

This API is used to create a function.

URI

POST /v2/{project_id}/fgs/functions

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Project ID. For details, see Obtaining a Project ID.

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

String

User token.

It can be obtained by calling the IAM API used to obtain a user token. The value of X-Subject-Token in the response header is the user token.

Content-Type

Yes

String

Message body type (format).

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

func_name

Yes

String

Function name.Max. 60 of letters, digits, hyphens (-), and underscores (_). Start with a letter, and end with a letter or digit.

Minimum length: 1 character.

Maximum length: 60 characters.

package

Yes

String

Group to which the function belongs. This field is defined to group functions.

runtime

Yes

String

Environment for executing a function. Options: Python2.7 Python 3.6 Python 3.9 Python 3.10 Go 1.x Java 8 Java 11 Node.js 6.10 Node.js 8.10 Node.js 10.16 Node.js 12.13 Node.js 14.18 Node.js 16.17 Node.js 18.15 C# (.NET Core 2.1) C# (.NET Core 3.1) Custom PHP 7.3 HTTP Custom image-based functions Cangjie1.0

Enumeration values:

  • Java8
  • Java11
  • Node.js6.10
  • Node.js8.10
  • Node.js10.16
  • Node.js12.13
  • Node.js14.18
  • Node.js16.17
  • Node.js18.15
  • Python2.7
  • Python3.6
  • Python3.10
  • Go1.x
  • C#(.NET Core 2.1)
  • C#(.NET Core 3.1)
  • Custom
  • PHP7.3
  • Python3.9
  • http
  • Custom Image
  • Cangjie1.0

timeout

Yes

Integer

Maximum duration the function can be executed. Value range: 3s–259,200s.

handler

Yes

String

Handler of the function. Max. 60 of letters.

It must be in the format of "xx.xx" and contain a period (.). For a function created with a custom image, the handler must be "-".

For example, for Node.js function myfunction.handler, the file name is myfunction.js, and the handler function is handler.

Minimum length: 3 character.

Maximum length: 128 characters.

depend_version_list

No

Array of strings

Dependency version IDs.

func_vpc

No

FuncVpc object

VPC configuration. An agency with VPC permissions must be configured for the function.

memory_size

Yes

Integer

Memory consumed by a function.

Unit: MB.

The value can be 128, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2560, 3072, 3584 or 4096.

The value ranges from 128 to 4096.

code_type

No

String

Function code type. Options: inline: inline code zip: ZIP file obs: function code stored in an OBS bucket jar: JAR file, mainly for Java functions Custom-Image-Swr: The function code comes from the SWR custom image.

This parameter is optional for custom image functions, but mandatory for other functions.

Enumeration values:

  • inline
  • zip
  • obs
  • jar
  • Custom-Image-Swr

code_url

No

String

If code_type is set to obs, enter the OBS URL of the function code package. If code_type is not set to obs, leave this parameter blank.

code_filename

No

String

Name of a function file. This parameter is mandatory only when code_type is set to jar or zip.

custom_image

No

CustomImage object

Container image. This parameter is mandatory only when code_type is set to Custom-Image-Swr.

user_data

No

String

Name/Value information defined for the function. These are parameters used in the function. For example, if a function needs to access a host, define Host={host_ip}. You can define a maximum of 20 such parameters, and their total length cannot exceed 4 KB.

encrypted_user_data

No

String

Name/value information to be encrypted. For example, if a function needs an encryption password, define password={1234}. You can define a maximum of 20 such parameters, and their total length cannot exceed 4 KB.

xrole

No

String

Configuration agency. You need to create an agency on the IAM console. This field is mandatory when a function needs to access other services. After specifying a function configuration agency, you can use the context method in the function handler to obtain a token, an AK, and an SK for accessing other cloud services. No agency is required if FunctionGraph does not access any cloud services.

app_xrole

No

String

Execution agency. You can optimize performance by configuring a separate agency for function execution, otherwise, the same agency is used for both execution and configuration.

description

No

String

Function description.

func_code

No

FuncCode object

Response body of the FuncCode struct.

mount_config

No

MountConfig object

Mounting configuration.

initializer_handler

No

String

Initializer of the function in the format of "xx.xx". It must contain a period (.). This parameter is mandatory when the initialization function is configured. For example, for Node.js function myfunction.initializer, the file name is myfunction.js, and the initialization function is initializer.

initializer_timeout

No

Integer

Maximum duration the function can be initialized. Value range: 1s–300s. This parameter is mandatory when the initialization function is configured.

pre_stop_handler

No

String

The pre-stop handler of a function. The value must contain a period (.) in the format of xx.xx. For example, for Node.js function myfunction.pre_stop_handler, the file name is myfunction.js, and the initialization function is pre_stop_handler.

pre_stop_timeout

No

Integer

Maximum duration the function can be initialized. Value range: 1s–90s.

enterprise_project_id

No

String

Enterprise project ID. This parameter is mandatory if you create a function as an enterprise user.

type

No

String

Function version. Some regions support only v1. The default value is v1.

Default: v2

Enumeration values:

  • v1
  • v2

log_config

No

FuncLogConfig object

Function log configuration.

network_controller

No

NetworkControlConfig object

Network configuration.

is_stateful_function

No

Boolean

Whether stateful functions are supported. If they are supported, set this parameter to true. This parameter is supported in FunctionGraph v2.

enable_dynamic_memory

No

Boolean

Whether to enable dynamic memory allocation.

Table 4 FuncVpc

Parameter

Mandatory

Type

Description

domain_id

No

String

Domain name ID.

namespace

No

String

Project ID.

vpc_name

No

String

VPC name.

vpc_id

Yes

String

VPC ID.

subnet_name

No

String

Subnet name.

subnet_id

Yes

String

Subnet ID.

cidr

No

String

Subnet mask.

gateway

No

String

Gateway.

security_groups

No

Array of strings

Security group.

Table 5 CustomImage

Parameter

Mandatory

Type

Description

enabled

No

Boolean

Whether to enable this feature.

image

No

String

Image address.

command

No

String

Command for starting a container image.

args

No

String

Command line parameter for starting a container image.

working_dir

No

String

Working directory of an image container.

uid

No

String

User ID of an image container.

gid

No

String

User group ID of an image container.

Table 6 FuncCode

Parameter

Mandatory

Type

Description

file

No

String

Function code, which must be encoded using Base64. If this parameter is left blank, the default code is used.

link

No

String

Function code URL.

Table 7 MountConfig

Parameter

Mandatory

Type

Description

mount_user

Yes

MountUser object

User information.

func_mounts

Yes

Array of FuncMount objects

Mounted resources.

Table 8 MountUser

Parameter

Mandatory

Type

Description

user_id

Yes

String

User ID, a non-0 integer from –1 to 65534.

user_group_id

Yes

String

User group ID, a non-0 integer from –1 to 65534.

Table 9 FuncMount

Parameter

Mandatory

Type

Description

mount_type

Yes

String

Mount type. The value can be sfs, sfsTurbo, or ecs. This parameter is mandatory when func_mounts is not empty.

mount_resource

Yes

String

ID of the mounted resource (cloud service ID). This parameter is mandatory when func_mounts is not empty.

mount_share_path

No

String

Remote mount path. For example, 192.168.0.12:/data. This parameter is mandatory if mount_type is set to ecs.

local_mount_path

Yes

String

Function access path. This parameter is mandatory when func_mounts is not empty.

Table 10 FuncLogConfig

Parameter

Mandatory

Type

Description

group_name

No

String

Name of the log group bound to the function.

group_id

No

String

ID of the log group bound to the function.

stream_name

No

String

Name of the log stream bound to the function.

stream_id

No

String

ID of the log stream bound to the function.

Table 11 NetworkControlConfig

Parameter

Mandatory

Type

Description

disable_public_network

No

Boolean

Disable public access.

trigger_access_vpcs

No

Array of VpcConfig objects

VPC access restriction.

Table 12 VpcConfig

Parameter

Mandatory

Type

Description

vpc_name

No

String

VPC name.

vpc_id

No

String

VPC ID.

Response Parameters

Status code: 200

Table 13 Response body parameters

Parameter

Type

Description

func_id

String

Function ID, which uniquely identifies a function.

func_urn

String

Function URN.

func_name

String

Function name.

domain_id

String

Domain ID.

namespace

String

Project ID.

project_name

String

Project name.

package

String

Group to which the function belongs. This field is defined to group functions.

runtime

String

Environment for executing a function. Options: Python2.7 Python 3.6 Python 3.9 Python 3.10 Go 1.x Java 8 Java 11 Node.js 6.10 Node.js 8.10 Node.js 10.16 Node.js 12.13 Node.js 14.18 Node.js 16.17 Node.js 18.15 C# (.NET Core 2.1) C# (.NET Core 3.1) Custom PHP 7.3 HTTP Custom image-based functions Cangjie1.0

Enumeration values:

  • Java8
  • Java11
  • Node.js6.10
  • Node.js8.10
  • Node.js10.16
  • Node.js12.13
  • Node.js14.18
  • Node.js16.17
  • Node.js18.15
  • Python2.7
  • Python3.6
  • Python3.10
  • Go1.x
  • C#(.NET Core 2.1)
  • C#(.NET Core 3.1)
  • Custom
  • PHP7.3
  • Python3.9
  • http
  • Custom Image
  • Cangjie1.0

timeout

Integer

Maximum duration the function can be executed. Value range: 3s–259,200s.

handler

String

Handler of a function in the format of "xx.xx". Max. 60 of letters. It must contain a period (.).

For example, for Node.js function myfunction.handler, the file name is myfunction.js, and the handler function is handler.

Minimum length: 3 character.

Maximum length: 128 characters.

memory_size

Integer

Memory consumed by a function.

Unit: MB.

The value can be 128, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2560, 3072, 3584 or 4096.

The value ranges from 128 to 4096.

cpu

Integer

CPU resources of a function. Unit: millicore (1 core = 1000 millicores). The value of this field is proportional to that of MemorySize. By default, 100 CPU millicores are required for 128 MB memory.

code_type

String

Function code type. Options: inline: inline code zip: ZIP file obs: function code stored in an OBS bucket jar: JAR file, mainly for Java functions Custom-Image-Swr: The function code comes from the SWR custom image.

Enumeration values:

  • inline
  • zip
  • obs
  • jar
  • Custom-Image-Swr

code_url

String

If code_type is set to obs, enter the OBS URL of the function code package. If code_type is not set to obs, leave this parameter blank.

code_filename

String

Name of a function file. This parameter is mandatory only when code_type is set to jar or zip.

code_size

Long

Code size in bytes.

domain_names

String

Private domain name configured for resolution.

user_data

String

Name/Value information defined for the function.

These are parameters used in a function.

For example, if a function needs to access a host, define Host={host_ip}. You can define a maximum of 20 such parameters, and their total length cannot exceed 4 KB.

encrypted_user_data

String

User-defined name/value to be encrypted.

digest

String

SHA512 hash value of function code, which is used to determine whether the function has changed.

version

String

Function version.

image_name

String

Internal identifier of a function version.

xrole

String

Agency used by the function. You need to create an agency on the IAM console. This field is mandatory when a function needs to access other services.

app_xrole

String

Agency used by the function app. You need to create an agency on the IAM console. This field is mandatory when a function needs to access other services.

description

String

Function description.

last_modified

String

Time when the function was last updated.

func_vpc

FuncVpc object

VPC configuration. An agency with VPC permissions must be configured for the function.

mount_config

MountConfig object

Mounting configuration.

reserved_instance_count

Integer

The number of instances reserved for a function.

depend_list

Array of strings

Dependency ID list.

depend_version_list

Array of strings

Dependency version IDs.

strategy_config

StrategyConfig object

Function policy configuration.

extend_config

String

Extended configuration.

dependencies

Array of Dependency objects

Dependency packages.

initializer_handler

String

Initializer of the function in the format of "xx.xx". It must contain a period (.). This parameter is mandatory when the initialization function is configured. For example, for Node.js function myfunction.initializer, the file name is myfunction.js, and the initialization function is initializer.

initializer_timeout

Integer

Maximum duration the function can be initialized. Value range: 1s–300s. This parameter is mandatory when the initialization function is configured.

pre_stop_handler

String

The pre-stop handler of a function. The value must contain a period (.) in the format of xx.xx. For example, for Node.js function myfunction.pre_stop_handler, the file name is myfunction.js, and the initialization function is pre_stop_handler.

pre_stop_timeout

Integer

Maximum duration the function can be initialized. Value range: 1s–90s.

enterprise_project_id

String

Enterprise project ID. This parameter is mandatory if you create a function as an enterprise user.

long_time

Boolean

Whether to allow a long timeout.

log_group_id

String

Log group ID.

log_stream_id

String

Log stream ID.

type

String

v2 indicates an official version, and v1 indicates a deprecated version.

Enumeration values:

  • v1
  • v2

enable_cloud_debug

String

Whether to enable cloud debugging to adapt to the CloudDebug scenario. (discarded)

enable_dynamic_memory

Boolean

Whether to enable dynamic memory allocation.

is_stateful_function

Boolean

Whether stateful functions are supported. This parameter is supported in FunctionGraph v2.

custom_image

CustomImage object

Container image.

is_bridge_function

Boolean

Whether this is a bridge function.

apig_route_enable

Boolean

Whether to configure gateway routing rules.

heartbeat_handler

String

Entry of the heartbeat function in the format of "xx.xx". It must contain a period (.).This parameter can be configured only for Java functions. The heartbeat function entry must be in the same file as your function handler. This parameter is mandatory when the heartbeat function is enabled.

enable_class_isolation

Boolean

Indicates whether to enable class isolation. This parameter can be configured only for Java functions. Enable to dump logs to Kafka and improve class loading efficiency. However, this may cause compatibility issues.

allow_ephemeral_storage

Boolean

Indicates whether ephemeral storage can be configured.

ephemeral_storage

Integer

Ephemeral storage. By default, the /tmp directory of each function is 512 MB. You can increase the size to 10,240 MB (10 GB) if necessary.

Enumeration values:

  • 512
  • 10240

network_controller

NetworkControlConfig object

Network configuration.

resource_id

String

Resource ID. Unique ID of a function.

is_return_stream

Boolean

Whether to return stream data. (discarded)

enable_auth_in_header

Boolean

Whether to add authentication information to request header for custom image-based functions. (This parameter cannot be modified when creating a function.)

Table 14 FuncVpc

Parameter

Type

Description

domain_id

String

Domain name ID.

namespace

String

Project ID.

vpc_name

String

VPC name.

vpc_id

String

VPC ID.

subnet_name

String

Subnet name.

subnet_id

String

Subnet ID.

cidr

String

Subnet mask.

gateway

String

Gateway.

security_groups

Array of strings

Security group.

Table 15 MountConfig

Parameter

Type

Description

mount_user

MountUser object

User information.

func_mounts

Array of FuncMount objects

Mounted resources.

Table 16 MountUser

Parameter

Type

Description

user_id

String

User ID, a non-0 integer from –1 to 65534.

user_group_id

String

User group ID, a non-0 integer from –1 to 65534.

Table 17 FuncMount

Parameter

Type

Description

mount_type

String

Mount type. The value can be sfs, sfsTurbo, or ecs. This parameter is mandatory when func_mounts is not empty.

mount_resource

String

ID of the mounted resource (cloud service ID). This parameter is mandatory when func_mounts is not empty.

mount_share_path

String

Remote mount path. For example, 192.168.0.12:/data. This parameter is mandatory if mount_type is set to ecs.

local_mount_path

String

Function access path. This parameter is mandatory when func_mounts is not empty.

Table 18 StrategyConfig

Parameter

Type

Description

concurrency

Integer

Maximum number of instances for a single function. For v1, the value can be 0 or –1; for v2, it ranges from –1 to 1000.

  • -1: The function has unlimited instances.
  • 0: The function is disabled.

concurrent_num

Integer

Number of concurrent requests per instance. This parameter is supported only by v2. The value ranges from 1 to 1,000.

Table 19 Dependency

Parameter

Type

Description

id

String

Dependency version ID.

owner

String

Domain ID of the dependency owner.

link

String

URL of the dependency on OBS.

runtime

String

Environment for executing a function. Options: Python2.7 Python 3.6 Python 3.9 Python 3.10 Go 1.x Java 8 Java 11 Node.js 6.10 Node.js 8.10 Node.js 10.16 Node.js 12.13 Node.js 14.18 Node.js 16.17 Node.js 18.15 C# (.NET Core 2.1) C# (.NET Core 3.1) Custom PHP 7.3 HTTP Custom image-based functions Cangjie1.0

Enumeration values:

  • Java8
  • Java11
  • Node.js6.10
  • Node.js8.10
  • Node.js10.16
  • Node.js12.13
  • Node.js14.18
  • Node.js16.17
  • Node.js18.15
  • Python2.7
  • Python3.6
  • Python3.10
  • Go1.x
  • C#(.NET Core 2.1)
  • C#(.NET Core 3.1)
  • Custom
  • PHP7.3
  • Python3.9
  • http
  • Custom Image
  • Cangjie1.0

etag

String

MD5 value of a dependency.

size

Long

Dependency size.

name

String

Dependence name.

description

String

Dependency description.

file_name

String

File name of a dependency package (ZIP).

version

Long

Dependency version ID.

dep_id

String

Dependency ID.

last_modified

Integer

Time when the function was last updated.

Table 20 CustomImage

Parameter

Type

Description

enabled

Boolean

Whether to enable this feature.

image

String

Image address.

command

String

Command for starting a container image.

args

String

Command line parameter for starting a container image.

working_dir

String

Working directory of an image container.

uid

String

User ID of an image container.

gid

String

User group ID of an image container.

Table 21 NetworkControlConfig

Parameter

Type

Description

disable_public_network

Boolean

Disable public access.

trigger_access_vpcs

Array of VpcConfig objects

VPC access restriction.

Table 22 VpcConfig

Parameter

Type

Description

vpc_name

String

VPC name.

vpc_id

String

VPC ID.

Status code: 400

Table 23 Response body parameters

Parameter

Type

Description

error_code

String

Error code.

error_msg

String

Error message.

Status code: 401

Table 24 Response body parameters

Parameter

Type

Description

error_code

String

Error code.

error_msg

String

Error message.

Status code: 403

Table 25 Response body parameters

Parameter

Type

Description

error_code

String

Error code.

error_msg

String

Error message.

Status code: 404

Table 26 Response body parameters

Parameter

Type

Description

error_code

String

Error code.

error_msg

String

Error message.

Status code: 500

Table 27 Response body parameters

Parameter

Type

Description

error_code

String

Error code.

error_msg

String

Error message.

Status code: 503

Table 28 Response body parameters

Parameter

Type

Description

error_code

String

Error code.

error_msg

String

Error message.

Example Requests

  • Create a Python 2.7 function with 128 MB memory and 3s timeout. Add environment variable password and mount a disk.
    POST https://{Endpoint}/v2/{project_id}/fgs/functions
    
    {
      "func_name" : "xxx",
      "package" : "xxx",
      "runtime" : "Python2.7",
      "handler" : "index.py",
      "type" : "v2",
      "memory_size" : 128,
      "timeout" : 3,
      "code_type" : "inline",
      "mount_config" : {
        "mount_user" : {
          "user_id" : -1,
          "user_group_id" : -1
        },
        "func_mounts" : [ {
          "mount_resource" : "268xxx-d3xx-4cxx-98xx-d1dbxxx",
          "local_mount_path" : "/mnt",
          "mount_type" : "sfsTurbo"
        } ]
      },
      "encrypted_user_data" : "{\"password\":\"1234\"}",
      "func_code" : {
        "file" : "xxxxx",
        "link" : ""
      }
    }
  • Create a Python 3.9 function with 128 MB memory and 3s timeout, and upload the code from OBS.
    POST https://{Endpoint}/v2/{project_id}/fgs/functions
    
    {
      "func_name" : "xxx",
      "package" : "xxx",
      "runtime" : "Python3.9",
      "handler" : "index.py",
      "type" : "v2",
      "memory_size" : 128,
      "timeout" : 3,
      "code_type" : "obs",
      "code_url" : "https:xxx"
    }
  • Create a custom image function named xxx. The memory size is 128 MB, the timeout period is 3s, and the code is uploaded by SWR.
    POST https://{Endpoint}/v2/{project_id}/fgs/functions
    
    {
      "func_name" : "xxx",
      "package" : "xxx",
      "runtime" : "Custom Image",
      "handler" : "-",
      "type" : "v2",
      "memory_size" : 128,
      "timeout" : 3,
      "code_type" : "obs",
      "code_url" : "https:xxx",
      "xrole" : "xxx",
      "custom_image" : {
        "enabled" : true,
        "image" : "swr.xxx"
      }
    }

Example Responses

Status code: 200

ok

{
  "func_urn" : "urn:fss:xxxxxxxxx:c3b2459a6d5e4b548e6777e57852692d:function:default:TestCreateFunctionInPythonSdk:latest",
  "func_name" : "TestCreateFunctionInPythonSdk",
  "domain_id" : "14ee2e3501124efcbca7998baa24xxxx",
  "namespace" : "46b6f338fc3445b8846c71dfb1fbxxxx",
  "project_name" : "xxxxxxxxxx",
  "package" : "default",
  "runtime" : "Python3.6",
  "timeout" : 3,
  "handler" : "index.handler",
  "memory_size" : 128,
  "encrypted_user_data" : "{\"password\":\"\"}",
  "mount_config" : {
    "mount_user" : {
      "user_id" : -1,
      "user_group_id" : -1
    },
    "func_mounts" : [ {
      "mount_resource" : "268xxx-d3xx-4cxx-98xx-d1dbxxx",
      "local_mount_path" : "/mnt",
      "mount_type" : "sfsTurbo"
    } ]
  },
  "cpu" : 300,
  "code_type" : "inline",
  "code_filename" : "index.py",
  "code_size" : 100,
  "digest" : "1c8610d1870731a818a037f1d2adf3223e8ac351aeb293fb1f8eabd2e9820069a61ed8b5d38182e760adc33a307d0e957afc357f415cd8c9c3ff6f0426fd85cd",
  "version" : "latest",
  "image_name" : "latest-191025153727@zehht",
  "last_modified" : "2019-10-25 15:37:27",
  "strategy_config" : {
    "concurrency" : 0
  }
}

Status code: 400

Bad request.

{
  "error_code" : "FSS.0400",
  "error_msg" : "invalid parameter"
}

Status code: 401

Unauthorized.

{
  "error_code" : "FSS.0401",
  "error_msg" : "not permit"
}

Status code: 403

Forbidden.

{
  "error_code" : "FSS.0403",
  "error_msg" : "invalid token"
}

Status Codes

Status Code

Description

200

ok

400

Bad request.

401

Unauthorized.

403

Forbidden.

404

Not found.

500

Internal server error.

503

Service unavailable.

Error Codes

See Error Codes.