Function Compute node

更新时间:
复制 MD 格式

You can use Function Compute nodes in DataWorks to run custom code. These nodes support periodic scheduling, making them ideal for scheduled tasks. You can also combine Function Compute nodes with other node types in DataWorks to build complete data processing workflows. This topic describes how to create and use a Function Compute node.

Prerequisites

  • The RAM user that you want to use is added to your workspace.

    If you want to use a RAM user to develop tasks, you must add the RAM user to your workspace as a member and assign the Develop or Workspace Administrator role to the RAM user. The Workspace Administrator role has more permissions than necessary. Exercise caution when you assign the Workspace Administrator role. For more information about how to add a member and assign roles to the member, see Add members to a workspace.

  • A serverless resource group is associated with your workspace. For more information, see the topics in the Use serverless resource groups directory.

  • A Function Compute node must be created. For more information, see Create a node for a scheduling workflow.

Limitations

  • DataWorks supports invoking only event functions. You cannot invoke http functions. Therefore, to periodically schedule a function in DataWorks, you must create an event function to process event requests. For more information about function types, see Select a function type.

  • You can use Function Compute nodes only in workspaces that are in the following regions: China (Hangzhou), China (Shanghai), China (Beijing), China (Zhangjiakou), China (Shenzhen), China (Hong Kong), Singapore, UK (London), US (Silicon Valley), US (Virginia), Germany (Frankfurt), and Australia (Sydney).

Usage notes

  • When you use a Function Compute node, you invoke an event function from an existing service. If the list of services is not available, check the following possible causes:

    • Your account has overdue payments. Top up your account, refresh the node configuration page, and try again.

    • The current user does not have permission to retrieve the service list. Contact the owner of your Alibaba Cloud account to grant your RAM user the fc:ListServices permission or attach the AliyunFCFullAccess system policy. After granting the permissions, refresh the node configuration page and try again. For more information about authorization, see Control product and console access by using RAM policies.

  • If a function in a DataWorks Function Compute node is expected to run longer than one hour, you must use asynchronous invocation. For more information about asynchronous invocation in Function Compute, see Feature overview.

  • If you use a RAM user to develop a Function Compute node, you must grant the RAM user the required permissions by using a system policy or a custom policy.

    Policy type

    Description

    System policy

    When you use a system policy, grant the user the AliyunFCFullAccess, AliyunFCReadOnlyAccess, and AliyunFCInvocationAccess policies.

    Custom policy

    When you use a custom policy for Function Compute, grant the following common permissions:

    • fc:GetAsyncTask

    • fc:StopAsyncTask

    • fc:GetService

    • fc:ListServices

    • fc:GetFunction

    • fc:InvokeFunction

    • fc:ListFunctions

    • fc:GetFunctionAsyncInvokeConfig

    • fc:ListServiceVersions

    • fc:ListAliases

    • fc:GetAlias

    • fc:ListFunctionAsyncInvokeConfigs

    • fc:GetStatefulAsyncInvocation

    • fc:StopStatefulAsyncInvocation

    Note

    For more information about Function Compute permission policies, see the following documents:

Node development

  1. After you create a Function Compute node, open it. On the node configuration tab, select the function to invoke and configure its invocation method and variables.

    Parameter

    Description

    Select Version or Alias

    Select the service version or alias to use for the function invocation. The default version is LATEST.

    • Service version

      Function Compute's service-level version control lets you publish one or more versions of your service. When you publish a version, Function Compute creates a snapshot of the service, which includes service configurations and the code and configurations of functions within the service, but excludes triggers. The system then automatically assigns a version number to the snapshot for future use. For more information about publishing versions, see Manage versions.

    • Version alias

      In Function Compute, you can create an alias for a service version. An alias points to a specific version. You can use an alias to implement features such as deployments, rollbacks, and canary releases. An alias must be associated with a service and a version. When you use an alias to access a service or function, Function Compute resolves the alias to the version it points to. The caller does not need to know the specific version. For more information about creating an alias, see Manage aliases.

    Select Function

    Select the function to invoke. If no function is available, you must create one. For more information, see Manage functions.

    Note

    DataWorks supports invoking only event functions. You cannot invoke http functions. Therefore, to periodically schedule a function in DataWorks, you must create an event function to process event requests. For more information about function types, see Select a function type.

    This example uses the para_service_01_by_time_triggers function. When creating this function, select the sample code for triggering a function at a scheduled time. The code logic is as follows:

    import json
    import logging
    
    logger = logging.getLogger()
    
    def handler(event, context):
        logger.info('event: %s', event)
    
        # Parse the json
        evt = json.loads(event)
        triggerName = evt["triggerName"]
        triggerTime = evt["triggerTime"]
        payload = evt["payload"]
    
        logger.info('triggerName: %s', triggerName)
        logger.info("triggerTime: %s", triggerTime)
        logger.info("payload: %s", payload)
    
        return 'Timer Payload: ' + payload

    For more sample code for functions, see Sample code.

    Invocation Method

    You can invoke the function in one of the following ways:

    • synchronous invocation: The event directly triggers the function. Function Compute runs the function and waits for a response. After the function is invoked, Function Compute returns the execution result.

    • asynchronous invocation: Function Compute persists the event request and returns a response immediately instead of waiting for the request to complete.

      • You can use asynchronous invocation for functions that have long-running, resource-intensive, or error-prone logic. This improves your application's responsiveness and resilience to traffic spikes.

      • Use asynchronous invocation for Function Compute tasks that are expected to run longer than one hour.

    Variable

    Assign values to the variables in your function code. This configuration corresponds to the content in the panel, which you can access by navigating to Test Function>Configure Test Parameters on the function details page in the Function Compute console.

    This example passes the following parameters to the para_service_01_by_time_triggers function. The ${} syntax defines a variable named bizdate that requires a value assignment.

    {
        "payload": "payload1",
        "triggerTime": "${bizdate}",
        "triggerName": "triggerName1"
    }
  2. After you finish developing the Function Compute node, configure its scheduling properties to enable periodic execution. For more information, see Configure scheduling properties for a node.

Deployment and management

  1. After you configure the scheduling properties, you can deploy the Function Compute node to the production environment. For more information, see Deploy a node or workflow.

  2. After the node is deployed, it runs periodically based on your scheduling configuration. You can view the deployed node and perform O&M operations in Operation and Maintenance Center > Node O&M > Auto Triggered Task O&M > Auto Triggered Task. For more information, see Get started with Operation Center.