After receiving an asynchronous invocation request, Function Compute persists the request and immediately returns a response without waiting for the request execution to complete. If you want to retain failed requests that are retried for the specified maximum number of times or if you want to notify downstream applications of asynchronous invocation results, you can configure the result callback feature. After you configure a destination service, Function Compute automatically calls back the service based on the execution result when an asynchronous invocation request is executed.
How it works
The following figure shows the process of result callback.
Scenarios
Save discarded events for later use
If an asynchronous request fails to be executed even after the system repeatedly tries to execute the request based on the retry policy, Function Compute discards the request. If a destination is configured for failed invocations, Function Compute pushes the context information of a failed request to a message service such as ApsaraMQ for RocketMQ for subsequent processing. You can also set another function as the destination service. Function Compute automatically pushes the context information of a 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. For example, you have configured Function Compute to automatically decompress ZIP files that are uploaded to Object Storage Service (OSS) and you want to receive notifications after a ZIP file is decompressed, you can configure the result callback feature for the decompression function.
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:
Simple Message Queue (formerly MNS)
Function Compute
EventBridge
ApsaraMQ for RocketMQ
Only event functions can be configured as destination services. HTTP functions cannot be configured as destination services in Function Compute. For more information about event functions and HTTP functions, see Function type selection.
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 Simple Message Queue (formerly MNS), Function Compute, or ApsaraMQ for RocketMQ is configured as the destination for asynchronous invocation.
{ "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 description
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 to invoke 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 EventBridge is configured as the destination for asynchronous invocation. 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:
Simple Message Queue (formerly MNS): 64 KB
Function Compute: 128 KB
EventBridge: 64 KB
ApsaraMQ for 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 begin an infinite loop in which Function A and Function B continuously invoke each other.
Configure a destination for an asynchronous invocation
Before you configure the destination for an asynchronous invocation, you must Creates a function and make sure that the role used by the functions in the service has the permissions on related Alibaba Cloud services. The following items list the permissions that are required by the corresponding cloud service. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.
Simple Message Queue (formerly 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 name of the function that you want to manage. On the function details page, click the Asynchronous Configurations tab.
On the Asynchronous Configurations tab, configure the parameters based on your business requirements.
Configure the destination for successful invocations
In the Destination for Successful Invocation section, click Modify.
In the Modify Destination for Successful Invocation panel, set Invoke Other Services upon Success to Enable and configure the Destination Service parameter. The following table describes the Destination Service parameter.
Parameter
Description
Destination Service
Function Compute. If you select Function Compute for the Destination Service parameter, you must configure the following parameters:
Service Name: the name of a service in Function Compute.
Version or Alias: the alias or version of the specified service.
Function Name: the name of a function in the service.
NoteOnly event functions can be configured as destination services. HTTP functions cannot be configured as destination services in Function Compute.
Simple Message Queue (formerly MNS). If you select Simple Message Queue (formerly MNS) for the Destination Service parameter, 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. SMQ topics can be pushed as messages by using multiple methods.
Queue: the name of the Simple Message Queue (formerly MNS) queue. This parameter is required when the Destination Type parameter is set to Queue.
Theme: the name of the Simple Message Queue (formerly MNS) topic. This parameter is required when the Destination Type parameter is set to Topic.
ApsaraMQ for RocketMQ. If you select ApsaraMQ for RocketMQ for the Destination Service parameter, you must configure the following parameters:
Instances: the ApsaraMQ for RocketMQ instance.
Topic: the name of the ApsaraMQ for RocketMQ topic.
EventBridge. If you select EventBridge for the Destination Service parameter, 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, select Enable for the Invoke Other Services upon Failure parameter and configure the Destination Service parameter.
The Destination Service configuration for failed invocations is similar to that for successful invocations. For more information, see the Configure the destination for successful invocations section of this topic.
Click OK.
After the function is triggered, you can view the following information from the configured destination service when 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 service 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 code | Description | System action |
5xx | Requests are throttled or a content error occurs. | Function Compute automatically retries the callback in 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 an incorrect request parameter is that the resources for the destination service specified by the request parameter have been 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 the metrics 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
You can also call an API operation or use Serverless Devs to configure destination services for asynchronous invocations. For more information, see API Reference and Serverless Devs commands.
HTTP functions cannot be used as destination services of callbacks in Function Compute. However, HTTP functions support synchronous and asynchronous invocations. For more information, see Overview.
You can enable the asynchronous task mode when you configure the result callback feature. For more information, see Overview.