Updated on 2022-02-22 GMT+08:00

Creating a Function

Function

This API is used to create a function.

URI

POST /v2/{project_id}/fgs/functions

Table 1 describes the URI parameter.

Table 1 URI parameter

Parameter

Type

Mandatory

Description

project_id

String

Yes

Project ID

Request

Table 2 describes the request parameters.

Table 2 Request parameters

Parameter

Type

Mandatory

Description

func_name

String

Yes

Function name.

package

String

Yes

Group to which the function belongs. Default value: default. You can customize the value as required.

code_type

String

Yes

Function code type. Options:

  • inline: inline code
  • zip: ZIP file
  • jar: JAR file (mainly for Java functions)
  • obs: function code stored in an Object Storage Service (OBS) bucket

code_url

String

No

  • When code_type is set to obs, this parameter indicates the address of a function code package in OBS.
  • When code_type is set to inline, zip, or jar, this parameter is left blank.

description

String

No

Description of the function.

code_filename

String

No

Code file name.

  • When code_type is set to zip or jar, this parameter is required.
  • When code_type is set to obs or inline, this parameter is not required.

handler

String

Yes

Entry point of the function.

memory_size

Int

Yes

Memory (MB) consumed by the function.

Options: 128, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2560, 3072, 3584, and 4096.

runtime

String

Yes

Environment for executing the function. FunctionGraph supports Node.js 6.10, Node.js 8.10, Node.js 10.16, Node.js 12.13, Python 2.7, Python 3.6, Java 8, Go 1.8, C# (.NET Core 2.0), C# (.NET Core 2.1), C# (.NET Core 3.1), and PHP 7.3.

timeout

Int

Yes

Timeout allowed for the function. Value range: 3s–900s.

user_data

String

No

Name/Value information defined for the function.

xrole

String

No

This parameter is mandatory if the function needs to access other cloud services.

func_code.file

String

Yes

Function code.

  • This parameter is mandatory when code_type is set to inline, zip, or jar. Moreover, the code must be encoded using Base64.
  • This parameter is optional when code_type is set to obs.

Response

Table 3 describes the response parameters.
Table 3 Response parameters

Parameter

Type

Description

func_urn

String

Function URN.

func_name

String

Function name.

domain_id

String

Domain ID.

namespace

String

Tenant's project ID.

project_name

String

Tenant's project name.

package

String

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

runtime

String

Environment for executing the function. FunctionGraph supports Node.js 6.10, Node.js 8.10, Node.js 10.16, Node.js 12.13, Python 2.7, Python 3.6, Java 8, Go 1.8, C# (.NET Core 2.0), C# (.NET Core 2.1), C# (.NET Core 3.1), and PHP 7.3.

timeout

Int

Maximum duration the function can be executed. Value range: 3s–900s.

handler

String

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

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

memory_size

Int

Memory (MB) consumed by the function.

Options: 128, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2560, 3072, 3584, and 4096.

cpu

Int

Number of CPU millicores used by the function (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. The value is calculated as follows: Memory/128 x 100 + 200 (basic CPU millicores).

code_type

String

Function code type. Options:

  • inline: inline code
  • zip: ZIP file
  • jar: JAR file (mainly for Java functions)
  • obs: function code stored in an OBS bucket

code_url

String

  • When code_type is set to obs, this parameter indicates the address of a function code package in OBS.
  • When code_type is set to inline, zip, or jar, this parameter is left blank.

code_filename

String

Function file name.

  • When code_type is set to zip or jar, this parameter is required.
  • When code_type is set to obs or inline, this parameter is not required.

code_size

Int64

Code size in bytes.

user_data

String

Name/Value information defined for 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.

digest

String

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

version

String

Function version, which is automatically generated by the system. The version name is in the format of "vYYYYMMDD-HHMMSS" (v+year/month/day-hour/minute/second).

image_name

String

Internal identifier of a function version.

xrole

String

Agency used by the function. You need to create an agency on the Identity and Access Management (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

Description of the function.

version_description

String

Description of the function version.

last_modified

String

Time when the function was last updated.

func_vpc

*String

Virtual Private Cloud (VPC) configuration. See Table 6.

func_code

String

Function code. See Table 4.

depend_list

[]String

Dependency list.

strategy_config

String

Function policy configuration. See Table 4.

extend_config

String

Function extension configuration.

dependencies

[]*String

Dependency code package.

initializer_handler

String

Initializer of the function.

initializer_timeout

Int

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

Table 4 strategy_config parameter

Parameter

Type

Mandatory

Description

concurrency

Int

Yes

  • 0: The function is disabled.
  • -1: The function is enabled.

Example

Example request

POST /v2/7aad83af3e8d42e99ac194e8419e2c9b/fgs/functions HTTP/1.1 
 { 
   "func_name": "test", 
   "package": "default", 
   "description": "", 
   "handler": "test.handler", 
   "memory_size": 128, 
   "timeout": 3, 
   "runtime": "Python2.7", 
   "user_data": "", 
   "code_type": "inline", 
   "func_code": { 
     "file": "aW1wb3J0IGpzb24KZGVmIGhhbmRsZXIgKGV2ZW50LCBjb250ZXh0KToKICAgIG91dHB1dCA9ICdIZWxsbyBtZXNzYWdlOiAnICsganNvbi5kdW1wcyhldmVudCkKICAgIHJldHVybiBvdXRwdXQ=" 
   } 
 } 

Example response

The format of the response for a successful request is as follows:

HTTP/1.1 200 OK
{
  "func_urn": "urn:fss:xxxxxxxxx:c3b2459a6d5e4b548e6777e57852692d:function:default:TestCreateFunctionInPythonSdk:latest",
  "func_name": "TestCreateFunctionInPythonSdk",
  "user_domain": "FGS_hwx559619",
  "namespace": "c3b2459a6d5e4b548e6777e57852692d",
  "project_name": "xxxxxxxxxx",
  "package": "default",
  "runtime": "Python3.6",
  "timeout": 30,
  "handler": "index.handler",
  "memory_size": 128,
  "cpu": 300,
  "code_type": "inline",
  "code_filename": "index.py",
  "code_size": 110,
  "digest": "1c8610d1870731a818a037f1d2adf3223e8ac351aeb293fb1f8eabd2e9820069a61ed8b5d38182e760adc33a307d0e957afc357f415cd8c9c3ff6f0426fd85cd",
  "version": "latest",
  "image_name": "latest-0zf5g",
  "last_modified": "2019-03-07T18:37:19+08:00",
  "concurrency": 0,
  "strategy_config": {
   "concurrency": -1
  },
 }

The format of the response for a failed request is as follows:

HTTP/1.1 409 Conflict 
{ 
  "error_code": "FSS.1061", 
  "error_msg": "The function has existed" 
 }

Status Code

See Status Codes.