Function Compute supports automatic callback based on the results of asynchronous invocations. When a task is executed, Function Compute automatically invokes the corresponding services based on the execution result. You must configure the destination services if you want to use this feature. This topic describes the principles and scenarios of the result callback feature, the supported destination services for asynchronous invocations, and how to configure destination services for asynchronous invocations in the Function Compute console.

How it works

The following figure shows the process of result callback.

Asynchronous invocation process

Scenario

  • Save discarded events for later use

    If an asynchronous request fails to be executed even after the system repeatedly tries to execute it based on the retry policy, Function Compute discards the request. If the destination for failed invocations is configured, Function Compute pushes the context information of the failed request to a message service such as Message Queue for Apache RocketMQ for subsequent processing. You can also set the destination service to another function. Function Compute automatically pushes the context information of the failed request to this function to execute the custom error handling logic.

  • Notify downstream services of execution results

    After a request is successfully executed, Function Compute pushes the context information of the request to the downstream destination service.

Supported destination services for asynchronous invocations

If you configure a destination for the asynchronous invocations for a function, Function Compute sends the context and data of the request to the specified service when the result of the asynchronous invocation meets the specified conditions. You can configure different destination services for different functions, aliases, and versions. The following destination services are supported for asynchronous invocations:
  • MNS
  • Function Compute
  • EventBridge
  • Message Queue for Apache RocketMQ
Note

Only Function Compute event functions can be configured as the destination services. HTTP functions cannot be configured as the destination services. For more information about event functions and HTTP functions, see Function types.

Take note of the following items when you configure the destination service for asynchronous invocations:

  • Event content sent to the destination for the asynchronous invocation

    The following sample code provides an example of event content when the destination for the asynchronous invocation is MNS, Function Compute, or Message Queue for Apache RocketMQ.

    {
        "timestamp": 1660120276975,
        "requestContext": {
            "requestId": "xxx",
            "functionArn": "acs:fc:::services/{serviceName}/functions/{functionName}",
            "condition": "FunctionResourceExhausted", 
            "approximateInvokeCount": 3
        },
        "requestPayload": "",
        "responseContext": {
            "statusCode": 200,
            "functionError": ""
        },
        "responsePayload": ""
    }
    Table 1. Parameter descriptions
    ParameterDescription
    timestampThe timestamp of the invocation.
    requestContextThe context of the request.
    requestContext.requestIdThe request ID of the asynchronous invocation.
    requestContext.functionArnThe Alibaba Cloud Resource Name (ARN) of the function that is asynchronously invoked.
    requestContext.conditionThe error code of the invocation.
    requestContext.approximateInvokeCountThe number of execution times of the asynchronous invocation. A value greater than 1 indicates that Function Compute has retried your function.
    requestPayloadThe original payload of the function request.
    responseContextThe context of the response.
    responseContext.statusCodeThe status code that is returned by the system for the invoked function. A status code other than 200 indicates that a system error occurred.
    responseContext.functionErrorThe error message of the invocation.
    responsePayloadThe original payload that is returned after the function is executed.
    The following sample code provides an example of event content when the destination for the asynchronous invocation is EventBridge. For more information, see Overview.
    {
        "datacontenttype": "application/json",
        "aliyunaccountid": "143xxxx",
        "data": {
            "requestContext": {
                "condition": "",
                "approximateInvokeCount": 1,
                "requestId": "0fcb7f0c-xxxx",
                "functionArn": "acs:fc:::services/xxxx.LATEST/functions/xxxx"
            },
            "requestPayload": "",
            "responsePayload": "",
            "responseContext": {
                "functionError": "",
                "statusCode": 200
            },
            "timestamp": 1660120276975
        },
        "subject": "acs:fc:::services/xxxx.LATEST/functions/xxxx",
        "source": "acs:fc",
        "type": "fc:AsyncInvoke:succeeded",
        "aliyunpublishtime": "2021-01-03T09:44:31.233Asia/Shanghai",
        "specversion": "1.0",
        "aliyuneventbusname": "xxxxxxx",
        "id": "ecc4865xxxxxx",
        "time": "2021-01-03T01:44:31Z",
        "aliyunregionid": "cn-shanghai-vpc",
        "aliyunpublishaddr": "199.99.xxx.xxx"
    }
  • Payload limits

    The payload that is supported by a destination for the asynchronous invocation cannot exceed the following limits:

    • MNS: 64 KB
    • Function Compute: 128 KB
    • EventBridge: 64 KB
    • Message Queue for Apache RocketMQ: 4 MB
  • Loop avoidance

    When you configure a destination for the asynchronous invocation, make sure that you do not create a loop. For example, you configure Function B as the destination for successful asynchronous invocations of Function A and Function A as the destination for successful asynchronous invocations of Function B. An asynchronous invocation and execution of Function A may start an infinite loop in which Function A and Function B continuously invoke each other.

Configure a destination for an asynchronous invocation

Note Before you configure the destination for an asynchronous invocation, Create a function and make sure that the role used by the functions in the service has the permissions on related cloud services. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.
  • MNS: mns:SendMessage or mns:PublishMessage
  • Function Compute: fc:InvokeFunction
  • EventBridge: eventbridge:PutEvents
  • Message Queue for Apache RocketMQ: mq:PUB
  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Services & Functions.
  3. In the top navigation bar, select a region.
  4. On the Services page, click the desired service.
  5. On the Functions page, click the function that you want to modify.
  6. On the function details page, click the Asynchronous Mode Configurations tab.
  7. On the Asynchronous Mode Configurations tab, configure the parameters.
    • Configure the destination for successful invocations
      1. In the Destination for Successful Invocation section, click Modify.
      2. In the Modify Destination for Successful Invocation panel, set the Invoke Other Services upon Success parameter to Enable and then set the Destination Service parameter. The following table describes the parameters.
        ParameterDescription
        Invoke Other Services upon SuccessIf you set this parameter to Enable, the result of the successful invocation of the function is sent to the destination service.
        Destination ServiceFunction Compute. If you select Function Compute, you must configure the following parameters:
        • Service Name: the name of the specified service.
        • Version or Alias: the alias or version of the specified service.
        • Function Name: the name of the specified function.
          Note Only event functions can be configured as destination services. HTTP functions cannot be configured as destination services.
        MNS. If you select MNS, you must configure the following parameters:
        • Destination Type: the type of the destination that receives the results of successful invocations. Valid values:
          • Queue

            A queue-based messaging model provides highly reliable and concurrent message consumption services. Each message in a queue can be consumed by only one client.

          • Topic

            A topic-based messaging model is used to send messages from one publisher client to multiple subscriber clients. MNS topics can be pushed as messages by using multiple methods.

        • Queue: the name of the MNS queue. This parameter is required when the Destination Type parameter is set to Queue.
        • Topic: the name of the MNS topic. This parameter is required when the Destination Type parameter is set to Topic.
        If you select Message Queue for Apache RocketMQ, you must set the following parameters:Message Queue for Apache RocketMQ
        • Instances: the destination instance.
        • Topic: the destination topic.
        EventBridge. If you select EventBridge, you must configure the Custom Event Bus parameter.
      3. Click OK.
    • Configure the destination for failed invocations
      1. In the Destination for Failed Invocation section, click Modify.
      2. In the Modify Destination for Failed Invocation panel, set the Invoke Other Services upon Failure parameter to Enable and then set the Destination Service parameter.

        For more information about how to configure the required parameters, see the description of configuring the destination to receive the results of successful invocations.

      3. Click OK.
    After the function is triggered, you can view the following information from the configured destination service after the function execution succeeds or fails:
    {
        "timestamp": 1660120276975,
        "requestContext": {
            "requestId": "xxx",
            "functionArn": "acs:fc:::services/{serviceName}/functions/{functionName}",
            "condition": "FunctionResourceExhausted",
            "approximateInvokeCount": 3
        },
        "requestPayload": "",
        "responseContext": {
            "statusCode": 200,
            "functionError": ""
        },
        "responsePayload": ""
    }

Handling of callback failures

If the service role does not have the permissions on the destination services or the destination service is unavailable, the callback to the destination service may fail. You can handle the errors based on the metrics and logs provided by Function Compute. The following table describes the common errors and corresponding system actions.

ErrorDescriptionSystem action
5xxRequests are throttled or a content error occurs. Function Compute automatically retries the callback in the exponential backoff mode. The initial retry interval is 500 milliseconds and the maximum retry duration is 30 minutes.
4xxYou do not have sufficient permissions, the request parameter is incorrect, or the request message body exceeds the limit of the destination service. An example of incorrect request parameter is that the resources for the destination service are deleted. The system returns an error and records the error message.

Result callback metrics

When a callback fails, Function Compute records the corresponding metrics and displays them in the console. Log on to the Function Compute console. In the left-side navigation pane, choose Advanced Features > Monitoring Dashboard. On the page that appears, click the name of the service whose metrics you want to view in the Service Name column. The following table describes the metrics for the invocation of destination services.

MetricDescription
FunctionDestinationErrorsThe number of requests that fail to trigger the destination during function execution when a destination is configured for asynchronous invocations of a function. The statistics are collected every minute or every hour.
FunctionDestinationSucceedThe number of requests that trigger the destination during function execution when a destination is configured for asynchronous invocations of a function. The statistics are collected every minute or every hour.

For more information about metrics, see Monitoring metrics.

References

In addition to the Function Compute console console, you can use the following methods to configure the destination services for asynchronous invocations: