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.

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
- MNS
- Function Compute
- EventBridge
- Message Queue for Apache RocketMQ
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 Parameter Description timestamp The timestamp of the invocation. requestContext The context of the request. requestContext.requestId The request ID of the asynchronous invocation. requestContext.functionArn The Alibaba Cloud Resource Name (ARN) of the function that is asynchronously invoked. requestContext.condition The error code of the invocation. requestContext.approximateInvokeCount The number of execution times of the asynchronous invocation. A value greater than 1 indicates that Function Compute has retried your function. requestPayload The original payload of the function request. responseContext The context of the response. responseContext.statusCode The 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.functionError The error message of the invocation. responsePayload The 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
- MNS: mns:SendMessage or mns:PublishMessage
- Function Compute: fc:InvokeFunction
- EventBridge: eventbridge:PutEvents
- Message Queue for Apache RocketMQ: mq:PUB
- Log on to the Function Compute console.
- In the left-side navigation pane, click Services & Functions.
- In the top navigation bar, select a region.
- On the Services page, click the desired service.
- On the Functions page, click the function that you want to modify.
- On the function details page, click the Asynchronous Mode Configurations tab.
- On the Asynchronous Mode Configurations tab, configure the parameters.
- Configure the destination for successful invocations
- In the Destination for Successful Invocation section, click Modify.
- 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.
Parameter Description Invoke Other Services upon Success If you set this parameter to Enable, the result of the successful invocation of the function is sent to the destination service. Destination Service Function 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
- 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. - Click OK.
- Configure the destination for failed invocations
- In the Destination for Failed Invocation section, click Modify.
- 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.
- 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": "" }
- Configure the destination for successful invocations
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.
Error | Description | System action |
---|---|---|
5xx | Requests 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. |
4xx | You 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 . 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.
Metric | Description |
---|---|
FunctionDestinationErrors | The 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. |
FunctionDestinationSucceed | The 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
- Serverless Devs
When you use Serverless Devs to manage resources, you can asynchronously invoke functions and set the parameters related to asynchronous invocations. For more information about the message routing feature, see Serverless Devs commands.
- SDK
For more information about the supported SDKs, see SDKs.
You can invoke a function by calling the InvokeFunction operation. For information about the API operations related to asynchronous invocations, see the following topics: