A function is a scheduling and operational unit in Function Compute. A function must belong to a service. You can create multiple functions under the same service. These functions share some of the same settings, such as service authorization and log configuration, but are independent of each other and do not affect each other. This topic describes the properties and running environment of functions, and how to create, configure, and delete functions in the Function Compute console.

Prerequisites

Create a Service

Supported runtime environments

Runtime environment Description Reference
Node.js 6.x Node.js version 6.x Node.js runtime environment
Node.js 8.x Node.js version 8.x
Node.js 10.x Node.js 10.x version
Node.js 12.x Node.js 12.x version
Node.js 14.x Node.js 14.x version
Python 2.7 Python 2.7 Python runtime environment
Python 3 Python 3.6
PHP 7.2 PHP 7.2.7 PHP runtime environment
Java 11 Java 11 Java runtime environment
Java 8 Java 8
.NET Core 2.1 .NET Core 2.1 .NET Core runtime environment
Go 1.x Go 1.8 or later Go Getting Started Overview
Custom Runtime N/A Custom runtime environment
Custom Container N/A Custom container runtime

Create a function

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Services and Functions.
  3. In the top navigation bar, select the region where the service resides.
  4. On the Services page, click the target service.
  5. On the Functions page, click Create Function.
  6. On the Create Function page, select a method for creating the function as needed.
    In this example, Create from scratch is used. For more information, see the following topics:
  7. In the Basic Settings section, set the parameters and click Create. sc_create_function_v2.png
    The following table describes the parameters in the Display Content Column dialog box.
    Parameter Required? Title bar Example
    Parameter No The name of the function. It must start with a letter and can contain digits, letters (case-sensitive), underscores (_), and hyphens (-). It can be up to 64 characters in length.
    Note If you do not specify a name, the Function Compute is automatically created for you.
    Function
    Runtime environment Yes Select a language, such as Python, Java, PHP, or Node.js. For more information about the runtime environments supported by Function Compute, see Function overview. Node.JS 12.x
    Function trigger mode Yes Select a function trigger mode.
    • Trigger by Event: triggers function execution through timers and triggers of other Alibaba Cloud services.
    • Triggered by HTTP Request: You can send an HTTP request to trigger function execution, which is suitable for scenarios such as quickly building Web services.
    Triggered by events
    Network Type Yes The instance category for the function. Valid values:
    • Elastic Instance
    • Performance Instance
    For more information, see Instance specifications and usage modes.
    Elastic Instance
    Memory specification Yes The memory that is required to execute the function.
    • Select Input: Select the required memory from the drop-down list.
    • Manual Input: Click Manually Enter Memory Size to customize the memory size of the function. The memory specifications are described as follows:
      • Elastic Instance: Valid values: [128, 3072]. Unit: MB.
      • Performance Instances: Valid values: [4 to 32]. Unit: GB.
      Note The memory value must be a multiple of 64 MB.
    512 MB
    After the function is created, you can view the created function on the Function Management page. pg_view_function

Obtain the ARN of a function

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Services and Functions.
  3. In the top navigation bar, select the region where the service resides.
  4. On the Services page, click the target service.
  5. On the Functions page, click the name of the function that you want to manage.
  6. In the Function Overview section of the Function Details page, copy the function ARN.
    Resource ARN(Aliyun Resource Name) can be used to locate Alibaba Cloud resources in code.

Modify a function

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Services and Functions.
  3. In the top navigation bar, select the region where the service resides.
  4. On the Services page, click the target service.
  5. On the Functions page, find the function that you want to manage and click Configure in the Actions column.
  6. In the Edit Function dialog box, modify the parameters as needed and click Save.
    The following table describes the parameters.
    • Basic Information
      Parameter Required? Description Example
      Description No The description of the custom service. Description
      Network Type Yes The instance category for the function. Valid values:
      • Elastic Instance
      • Performance Instance
      For more information, see Instance specifications and usage modes.
      Elastic instance
      Memory specification Yes The memory that is required to execute the function.
      • Select Input: Select the required memory from the drop-down list.
      • Manual Input: Click Manually Enter Memory Size to customize the memory size of the function. The memory specifications are described as follows:
        • Elastic Instance: Valid values: [128, 3072]. Unit: MB.
        • Performance Instances: Valid values: [4 to 32]. Unit: GB.
        Note The memory value must be a multiple of 64 MB.
      512 MB
      Notice The instance type is modified by following the following principles:
      • You can modify Elastic Instance and Performance Instance.
      • When you change from a performance instance to an elastic instance, that is, the instance type is reduced from high-configuration to low-configuration. The system detects the risk of switchover and automatically downgrades the memory specifications.
    • environment information
      Parameter Required? Description Example value
      Runtime environment No Select a language, such as Python, Java, PHP, or Node.js. For more information about the runtime environments supported by Function Compute, see . Node.JS 12.x
      Function handler Yes The name of the function handler. The format is [File name].[Function name]. index.handler
      Execution timeout period Yes The timeout period. The default value is 60 seconds. The maximum value is 86400 seconds.
      Note If the execution of the function does not complete before the specified timeout period expires, the function fails to be executed. For longer time limits, submit a ticket .
      60
      Single-instance parallelism Yes The number of requests that can be concurrently processed by a single instance. For more information about the backup methods, see A single instance that concurrently processes multiple requests.
      Notice This parameter is not valid for the Python language.
      1
    • lifecycle function
      Parameter Required? Description Example value
      Initialize a function No Initialize the function to ensure that it is executed in the same instance and successfully executed once. The format is [File name].[Function name]. index.handler
      Timeout of the initialization function Yes The timeout period. The default value is 3 seconds. The maximum value is 300 seconds. 3
      PreFreeze function No Before each time the Function Compute service decides to freeze the current function instance, the function that you want to call is Function Compute. The format is [File name].[Function name]. index.preFreeze
      PreFreeze Function Timeout Period Yes The timeout period. The default value is 3 seconds. The maximum value is 300 seconds. 3
      PreStop function No The function you want to call Function Compute before each Function Compute decides to stop the current function instance. The format is [File name].[Function name]. index.preStop
      PreStop Function Timeout Period Yes The timeout period. The default value is 3 seconds and the maximum value is 300 seconds. 3
    • Layer

      In the Layer section, click + Add Configuration, select the required configuration from the Layer Name and Layer Version drop-down lists, and then click Save.

      For more information about the backup methods, see Use layers in functions.

      Notice You can select a maximum of five layers for a function.
    • Environment Variables
      • Use form editing (default method)
        1. Click + Add Variable.
        2. Configure key-value pairs for environment variables:
          • Key: custom
          • Value: custom.
      • Edit using JSON format
        1. Click Edit in JSON Format.
        2. In the text box, enter a JSON-formatted key-value pair in the following format:
          {
              "key": "value"
          }

      For more information about the backup methods, see Environment variables.

Delete a function

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Services and Functions.
  3. In the top navigation bar, select the region where the service resides.
  4. On the Services page, click the target service.
  5. On the Functions page, find the function that you want to delete and click Delete in the Actions column.
  6. Deletes an existing function.
    • The trigger is not configured or deleted: In the dialog box that appears, click Delete. db_delete_function_without_trigger
    • Triggers are configured: In the dialog box that appears, select I am sure that I want to delete these triggers and delete this function at the same time. check box, and then click Delete. db_delete_function_with_trigger

References

In addition to the console, Function Compute also provides the following methods to manage functions: