When you invoke a function asynchronously, Function Compute queues the request and returns a response immediately. You do not need to wait for the function to finish running. To retain records of failed requests that exceed their maximum retry attempts, or to notify a downstream service of the outcome, you can configure a destination. After you configure a destination, Function Compute automatically sends details about the invocation result to the specified service.
How it works
The following diagram shows how destinations work.
Use cases
Retain discarded events for further processing
If an asynchronous invocation fails after all retry attempts based on your policy, Function Compute discards the event. By configuring a destination for failures, you can have Function Compute automatically send the context of the failed invocation to a message service, such as ApsaraMQ for RocketMQ, for further analysis or processing. You can also set another function as the destination. Function Compute automatically sends the context of the failed request to that function to execute your custom error-handling logic.
Notify downstream services of invocation results
If you have configured a destination for successful invocations, the Function Compute system automatically sends the context of the successful request to the destination after the request is executed. For example, if you use Function Compute to automatically decompress ZIP files uploaded to OSS and want to receive a notification upon completion, you can configure a destination for the function's asynchronous invocations.
Supported destinations for asynchronous invocations
When you configure a destination for a function's asynchronous invocations, Function Compute sends the request context and data to the specified service if the invocation result meets the conditions. You can configure different destinations for different functions, versions, and aliases. The following services are supported as destinations for asynchronous invocations:
Simple Message Queue (formerly MNS)
Function Compute
EventBridge
ApsaraMQ for RocketMQ
You can configure only event functions in Function Compute as destinations. You cannot configure HTTP functions as destinations. For more information about event functions and HTTP functions, see Select a function type.
Review the following configuration details for destinations:
Event content for the destination
The following code is a sample event sent when the destination is Simple Message Queue (formerly MNS), Function Compute, or ApsaraMQ for 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
异步执行的函数ARN。
requestContext.condition
调用错误码。
requestContext.approximateInvokeCount
异步调用的执行次数。当该值大于1时,说明函数计算对您的函数进行了重试。
requestPayload
请求函数的原始负载。
responseContext
返回上下文。
responseContext.statusCode
调用函数的返回码(系统)。当该返回码不为200时,说明出现了系统错误。
responseContext.functionError
调用错误信息。
responsePayload
执行函数返回的原始负载。
The following example shows an event when EventBridge is the target for an asynchronous invocation. For more information, see Event 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 maximum payload limits for destination services in asynchronous invocations are as follows:
Simple Message Queue (formerly MNS): 64 KB
Function Compute: 128 KB
EventBridge: 64 KB
ApsaraMQ for RocketMQ: 4 MB
Avoid circular invocations
When you configure an asynchronous execution destination, avoid creating a circular invocation. For example, you configure Function B as the destination for a successful invocation of Function A, and Function A as the destination for a successful invocation of Function B. If you asynchronously invoke Function A and it runs successfully, it triggers Function B. Function B then triggers Function A again, which creates a loop.
Configure a destination for an asynchronous invocation
Before configuring a destination for an asynchronous invocation, create a function and make sure the role used by the function has the required permissions to access the cloud service. For more information, see Grant Function Compute permissions to access other cloud services.
Simple Message Queue (formerly MNS): mns:SendMessage or mns:PublishMessage.
Function Compute: fc:InvokeFunction.
EventBridge: eventbridge:PutEvents.
ApsaraMQ for 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 destination function. Then, on the function details page, click the Asynchronous Configurations tab.
On the Asynchronous Configurations tab, configure the parameters as needed.
Configure a Destination for Successful Invocation
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. Then, configure the destination cloud service to which the result is sent after the request is successfully executed. The following table describes the parameters:
Parameter
Description
Destination Service
Function Compute. If you set Destination Service to Function Compute, configure the following parameters:
Service Name: Specify the name of the destination service.
Version or Alias: Specify the alias or version of the service.
Function Name: Specify the name of the destination function.
NoteYou can set only an event function of Function Compute as a destination. You cannot set an HTTP function as a destination.
Simple Message Queue (formerly MNS). If you set Destination Service to Simple Message Queue (formerly MNS), configure the following parameters:
Destination Type: Select a destination type as needed. Valid values:
Queue:
The queue model provides a highly reliable and concurrent one-to-one consumption model. Each message in a queue can be consumed by only one consumer.
Topic:
The topic model provides a one-to-many publish-subscribe model that supports message notifications.
Queue: The name of the Simple Message Queue (formerly MNS) queue. This parameter is required if you set Destination Type to Queue.
Topic: The name of the Simple Message Queue (formerly MNS) topic. This parameter is required if you set Destination Type to Topic.
ApsaraMQ for RocketMQ. If you set Destination Service to ApsaraMQ for RocketMQ, configure the following parameters:
Instances: Select the destination instance.
Topic: Select the destination topic.
EventBridge. If you set Destination Service to EventBridge, specify a Custom Event Bus.
Click OK.
Configure a Destination for Failed Invocation
In the Destination for Failed Invocation section, click Modify.
In the Modify Destination for Failed Invocation panel, set Invoke Other Services upon Failure to Enable. Then, configure the destination cloud service to which the result is sent after the request fails.
For information about the parameters for configuring a destination for a failed invocation, see Configure a Destination for Successful Invocation.
Click OK.
After the function is triggered, you can read the following content 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 callback failures
A callback to a Destination Service may fail if the service role lacks access permissions or if the Destination Service is unavailable. Function Compute provides relevant metrics and logs to help you handle these failures. The following table describes common errors and system behaviors.
Error code | Cause | System behavior |
5xx | Throttling, content errors, or other issues. | Function Compute automatically retries with exponential backoff. The initial retry interval is 500 ms, and the maximum retry duration is 30 minutes. |
4xx | Lack of permissions, incorrect request parameters such as a deleted resource in the Destination Service, or a request message body that exceeds the Destination Service limit. | An error is returned and the error message is recorded. |
Result callback metrics
If a callback to a Destination Service fails, Function Compute records the relevant metrics and displays them in the console. Log on to the Function Compute console. In the navigation pane on the left, choose . Then, in the Service Name list, click the name of the destination service to view its metrics. The following table describes the metrics for the destination feature.
Metric name | Description |
Destination Invocation Errors (FunctionDestinationErrors) | The number of failed requests to invoke a destination during function execution. This metric applies when a destination is configured for an asynchronous invocation. The values are summed up at a 1-minute or 1-hour granularity. |
Destination Invocation Successes (FunctionDestinationSucceed) | The number of successful requests to invoke a destination during function execution. This metric applies when a destination is configured for an asynchronous invocation. The values are summed up at a 1-minute or 1-hour granularity. |
For more information about monitoring metrics, see Monitoring metrics.
More information
You can also use an API or the Serverless Devs tool to configure a Destination Service for asynchronous invocations. For more information, see API Reference and Common Serverless Devs commands.
HTTP functions are not supported as destination functions, but they support synchronous and asynchronous invocations. For more information, see HTTP invocation methods.
You can also enable the asynchronous task mode when you configure the result callback feature for asynchronous invocations. For more information, see Asynchronous tasks.