nav-img
Advanced

Creating a Function

FunctionGraph manages the compute resources required for function execution. After editing code for your function, configure compute resources on the FunctionGraph console.

You can create a function from scratch or by using an existing template.

Creating a Function from Scratch

When creating a function from scratch, configure the basic and code information based on Table 1 and Table 2. The parameters marked with an asterisk (*) are mandatory.

For environment information, retain the default values, as shown in Table 4.

Table 1 Basic information

Parameter

Description

*Function Name

Function name.

*App

Select an existing app or define a new app to which the function belongs. You can create multiple functions under an app.

*Enterprise Project

Select an enterprise project to add the function to it.

NOTE:

If Enterprise Project Management Service (EPS) is not enabled, this parameter is unavailable.

Agency

Used to delegate FunctionGraph to access other cloud services. For example, an agency is required when FunctionGraph accesses cloud services such as OBS or SMN.

Description

Description of the function.

Note
  • For details on how to create an agency, see Creating an Agency.
  • To ensure optimal performance, select Specify an exclusive agency for function execution and set different agencies for function configuration and execution. You can also use no agency or specify the same agency for both purposes.

    Function execution agency: After specifying such an agency, you can obtain a token and AK/SK from the context in the function handler for accessing other cloud services.

Table 2 Code information

Parameter

Description

*Runtime

Currently, Python, Node.js, Java, Go, C# (.NET Core), Cangjie, PHP, and Custom runtimes are supported.

*Handler

  • For a Node.js, Python, or PHP function, the handler must be named in the format of [file name].[function name], which must contain a period (.).

    Example: myfunction.handler

  • For a Java function, the handler must be named in the format of [package name].[file name].[function name].

    Example: com.xxxxx.exp.Myfunction.myHandler

  • For a Go function, the handler must be named in the format of [plug-in name].[function name]. The function name must start with an uppercase letter. The handler name can contain a maximum of 128 characters.

    Example: function.Handler

  • For a C# function, the handler must be named in the format of [.NET assembly file name]::[namespace and class of the handler function]::[handler function name].

    Example: HelloCsharp::Example.Hello::Handler

  • For a Cangjie function, the handler must be named in the format of [Dynamic dependency library name].so. The name can contain a maximum of 128 characters.

    Example: libuser_func_test_success.so.

*Code Entry Mode

Method of entering the function code. For details about the supported code entry modes, see Table 3.

Table 3 Code entry modes

Runtime

Code Entry Mode

Description

Node.js

Edit code inline

Edit code in the code box. For more information, see Table 1.

Upload ZIP file

Click Select File and upload a local code package to FunctionGraph. The size of the ZIP file to be uploaded cannot exceed 50 MB. If it exceeds 50 MB, upload the ZIP file using an OBS bucket.

Upload file from OBS

Paste the link URL of the OBS bucket storing a code ZIP file.

Python

Edit code inline

Edit code in the code box. For more information, see Table 1.

Upload ZIP file

Click Select File and upload a local code package to FunctionGraph. The size of the ZIP file to be uploaded cannot exceed 50 MB. If it exceeds 50 MB, upload the ZIP file using an OBS bucket.

Upload file from OBS

Paste the link URL of the OBS bucket storing a code ZIP file.

Java

Upload ZIP file

Click Select File and upload a local code package to FunctionGraph. The size of the ZIP file to be uploaded cannot exceed 50 MB. If it exceeds 50 MB, upload the ZIP file using an OBS bucket.

Upload JAR file

Click Select File and upload a local JAR file to FunctionGraph. The JAR file must contain at least one .class file. The size of the JAR file to be uploaded cannot exceed 50 MB. If it exceeds 50 MB, convert it into a ZIP file, and upload the ZIP file to OBS.

Upload file from OBS

Paste the link URL of the OBS bucket storing a code ZIP file.

Go

Upload ZIP file

Click Select File and upload a local code package to FunctionGraph. The size of the ZIP file to be uploaded cannot exceed 50 MB. If it exceeds 50 MB, upload the ZIP file using an OBS bucket.

Upload file from OBS

Paste the link URL of the OBS bucket storing a code ZIP file.

C#(.NET Core)

Upload ZIP file

Click Select File and upload a local code package to FunctionGraph. The size of the ZIP file to be uploaded cannot exceed 50 MB. If it exceeds 50 MB, upload the ZIP file using an OBS bucket.

Upload file from OBS

Paste the link URL of the OBS bucket storing a code ZIP file.

Custom

Edit code inline

Edit code in the code box. For more information, see Table 1.

Upload ZIP file

Click Select File and upload a local code package to FunctionGraph. The size of the ZIP file to be uploaded cannot exceed 50 MB. If it exceeds 50 MB, upload the ZIP file using an OBS bucket.

Upload file from OBS

Paste the link URL of the OBS bucket storing a code ZIP file.

PHP

Upload ZIP file

Click Select File and upload a local code package to FunctionGraph. The size of the ZIP file to be uploaded cannot exceed 50 MB. If it exceeds 50 MB, upload the ZIP file using an OBS bucket.

Upload file from OBS

Paste the link URL of the OBS bucket storing a code ZIP file.

Cangjie

Upload ZIP file

Click Select File and upload a local code package to FunctionGraph. The size of the ZIP file to be uploaded cannot exceed 50 MB. If it exceeds 50 MB, upload the ZIP file using an OBS bucket.

Upload file from OBS

Paste the link URL of the OBS bucket storing a code ZIP file.

Notice
  • When you write code in Python, do not name your package with the same suffix as a standard Python library, such as json, lib, and os. Otherwise, an error indicating a module loading failure will be reported.
  • If the code to be uploaded contains sensitive information (such as account passwords), encrypt the sensitive information to prevent leakage.
Table 4 Environment information

Parameter

Description

Memory (MB)

Set the memory required for running the function on the Configuration tab page.

*Initializer

Set this parameter on the Code tab page if you have enabled initialization for the function.

For all runtimes, the naming rules of function initializers are the same as those of function handlers. For example, for a Node.js, Python, or PHP function, set an initializer name in the format of [file name].[initialization function name].

*Initialization Timeout (s)

Maximum duration the function can be initialized. Set this parameter on the Configuration tab page if you have enable initialization for the function.

The value ranges from 1s to 300s.

*Execution Timeout (s)

Maximum duration the function can be executed. You can set this parameter on the Configuration tab page. Use asynchronous invocation for functions that take longer than 90s.

The value ranges from 3s to 900s.

Note
  • When creating a function, you can retain the default values for the memory, execution timeout, encryption settings, and environment variables. After creating the function, you can modify these settings.
  • After a function is created, the default version is latest. Each function has the latest version.
  • Functions are billed based on the number of function execution requests and function execution duration. Please set the memory and execution timeout based on service requirements.

Creating a Function Using a Template

FunctionGraph provides built-in function templates for typical scenarios. These templates contain code, configurations, environment, and trigger information. After you choose a template, FunctionGraph automatically loads the function information, enabling you to quickly create functions. For details, see Creating a Function Using a Template.

Creating a Function Using a Container Image

Package your container images complying with the Open Container Initiative (OCI) standard, and upload them to FunctionGraph. The images will be loaded and run by FunctionGraph. Unlike the code upload mode, you can use a custom code package, which is flexible and reduces migration costs. For details, see Deploying a Function Using a Container Image.