Invoking a function asynchronously
Written by
Updated at February 28, 2024
Note
This feature is in the Preview stage.
Setting up asynchronous invocation
Management console
CLI
API
- In the management console
, select the folder containing your function. - Select Cloud Functions.
- Select a function.
- Go to the Editor tab.
- Under Asynchronous call, switch the Enable toggle to on.
- In the Number of retries field, specify the number of invocation retries before the call is considered failed.
- In the Service account field, specify a service account with rights to invoke the function.
- In the Destination for successful result and Destination for unsuccessful result fields, specify where messages on successful and unsuccessful function calls should be sent to, respectively:
Not specified
: Do not send messages.Message queue
: Send messages to a queue in Message Queue:- Message Queue: Queue in Message Queue to send messages to.
- Service account: Service account with rights to write messages to the queue.
- Click Save changes.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
To set up asynchronous invocation, run the following command:
yc serverless function version create \
--function-name=<function_name> \
--runtime <runtime_environment> \
--entrypoint <entry_point> \
--memory 128m \
--execution-timeout 5s \
--source-version-id <version_ID> \
--async-max-retries 3 \
--async-service-account-id <service_account_ID> \
--async-success-ymq-arn <message_queue> \
--async-success-sa-id <service_account_ID> \
--async-failure-ymq-arn <message_queue> \
--async-failure-sa-id <service_account_ID>
Where:
--function-name
: Function name.--runtime
: Runtime environment.--entrypoint
: Entry point specified in the<function_file_name>.<handler_name>
format.--memory
: Amount of RAM.--execution-timeout
: Maximum function execution time before the timeout is reached.--source-version-id
: ID of the function version to copy the code of.--async-max-retries
: Number of invocation retries before the call is considered failed.--async-service-account-id
: Service account with rights to invoke the function.--async-success-ymq-arn
: Queue in Message Queue where messages about successful function calls should be sent to. If the parameter is omitted, messages will not be sent.--async-success-sa-id
: Service account with rights to write messages to theasync-success-ymq-arn
queue.--async-failure-ymq-arn
: Queue in Message Queue where messages about failed function calls should be sent to. If the parameter is omitted, messages will not be sent.--async-failure-sa-id
: Service account with rights to write messages to theasync-failure-ymq-arn
queue.
To set up asynchronous invocation, use the createVersion REST API method for the Function resource or the FunctionService/CreateVersion gRPC API call.
Invoke the function
Invoke the function using the HTTPS protocol and specifying the ?integration=async
query string parameter.