Before you can invoke a function in the Serverless workflow console, you must create the function and its service. This topic describes how to create a function.

Create a service

  1. Log on to the Function Compute console.
  2. In the top navigation bar, select your region.
  3. In the left-side navigation pane, click Service/Function. Select Create Service from the drop-down list.
    creat_service
  4. In the Create Service dialog box, enter a new service name and click Create.
    enter_name
    On the Service/Function page, you can view the created service in the service list.creat_service_result

Create a function

  1. Log on to the Function Compute console.
  2. In the top navigation bar, select a region.
  3. In the left-side navigation pane, click Services and Functions. On the Services and Functions page, click the service that you require. Then, click Create Function in the upper-right corner.
    create-function
  4. Click Create Function, select a function type, and then click Configure and Deploy.
    In this example, select Event Function to create an event function.creat_function
  5. On the Create Function page, configure the parameters and click Create.
    config_function

    The following table describes the parameters.

    Parameter Required Description Example
    Function Type Yes The type of the function. You cannot modify the type after you select it. Event Function
    Service Name Yes
    • If you have created at least one service, select an existing service from the drop-down list.
    • If you have not created services, enter a new service name. The system then automatically creates a service with the specified service name.
    Service
    Function Name Yes The name of the function. Function
    Runtime Yes Select a language, such as Python, Java, PHP, or Node.js. For more information about the runtime environments supported by Function Compute, see Introduction.

    After you select the runtime environment, you can use one of the following methods to upload the function code:

    • Upload Zip File: Select this option and then click Upload Code to upload the function code.
    • Upload Folder: Select this option and then click Select Folder to select the folder to be uploaded.
    • Import from OSS: Select this option and then set Bucket Name and Object Name to upload the function code in OSS.
    • Use Sample Code: Select this option to use the sample code of the Function Compute.
    Node.JS 12.x
    Function Handler Yes The name of the function handler. It is in the format of [File name].[ Function name]. index.handler
    Advanced Settings
    Instance 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 Yes The memory that is required to execute the function.
    • Select: click the Memory value box and select a value from the drop-down list.
    • Manually enter: click manually enter and customize a memory value. The memory value must be a multiple of 64 MB.
    512 MB
    Timeout Yes The timeout period. The maximum value is 600 seconds. Default value: 60 seconds.
    Note If the execution of the function is not complete before the specified timeout period expires, the function fails to be executed.
    60
    Single Instance Concurrency No The number of requests that can be concurrently processed by a single instance. For more information, see Concurrent requests in a single instance. This parameter is not valid for the Python language.
    Layer No Select the layer you want to load. For more information, see Overview. NodeJS
  6. After the function is created, click Go to the page directly or Code.
  7. In the Code Management section, enter the following sample function code and click Invoke.

    The event is a JSON object. If the object contains a name, the function returns {"hello": "The value of the name in the event"}. Otherwise, the function returns {"hello": "world"}.

    # -*- coding: utf-8 -*-
    import json
    
    def handler(event, context):
     evt = json.loads(event)
     if "name" in evt:
        return {
          "hello": evt["name"]
        }
     else:
        return {
          "hello": "world"
        }

What to do next