Creating a trigger for Message Queue
- The trigger must be in the same cloud as the queue that messages are read from.
- Only one trigger can be created for each message queue.
Before you start
To create a trigger, you need:
- Functions the trigger will send messages to for processing. If you don't have a function:
- A message queue that the trigger will pick up messages from. If you don't have a queue, create one.
- Service accounts with rights to read messages from a message queue and to call a function. You can use the same service account or different ones. If you don't have a service account, create one.
Create a trigger
The trigger is initiated within 5 minutes of being created.
- In the management console, go to the folder where you want to create a trigger.
- Open Cloud Functions.
- Go to the Triggers tab.
- Click Create trigger.
- Under Basic parameters:
- Enter a name and description for the trigger.
- In the Type field, select Message Queue.
- Under Message Queue settings, select a message queue and a service account with rights to read messages from it.
- Under Batch message settings, specify the message batch size and the maximum waiting time. The trigger will send the batch of messages to the function when the number of messages in the queue reaches the specified batch size or the maximum waiting time expires.
- Under Function settings:
- Select the function for the trigger to call.
- Specify the function version tag.
- Specify the service account to be used to call the function.
- Click Create trigger.
If you don't 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
To create a trigger, run the command:
yc serverless trigger create message-queue \ --name <trigger name> \ --queue <queue ID> \ --queue-service-account-id <service account ID> \ --invoke-function-id <function ID> \ --invoke-function-service-account-id <service account ID> \ --batch-size 1 \ --batch-cutoff 10s
--name: Trigger name.
--queue: Queue ID.
To find out the queue ID:
- In the management console, go to the folder where the queue is located.
- Open Yandex Message Queue.
- Select the desired queue.
- You can see the queue ID under General information in the ARN field.
--invoke-function-id: Function ID.
--queue-service-account-name: Service account with rights to read messages from the queue.
--invoke-function-service-account-id: Service account with rights to call the function.
--batch-size: Message batch size. Values can be from 1 to 10. The default is 1.
--batch-cutoff: Maximum waiting time. Values can be from 0 to 20 seconds. The default is 10 seconds. The trigger will send the batch of messages to the function when the number of messages in the queue reaches the
id: dd0cspdch6dslu9n3i5g folder_id: aoek49ghmknnpj1ll45e created_at: "2019-08-28T12:14:45.762915Z" name: ymq-trigger rule: message_queue: queue_id: yrn:yc:ymq:ru-central1:aoek49ghmknnpj1ll45e:my-mq service_account_id: bfbqqeo6jkpls2tse5o6 batch_settings: size: "1" cutoff: 10s invoke_function: function_id: b09e5lu91ta21vdrrgma function_tag: $latest service_account_id: bfbqqeo6jkpls2tse5o6 status: ACTIVE
You can create a trigger for Yandex Message Queue using the create API method.
Check the results
Make sure the trigger is working properly. To do this, view the function logs that show information about its invocations.
Check that the number of enqueued messages is decreasing. To do this, view the queue statistics:
- Open Message Queue.
- Select the queue that you created the trigger for.
- Go to Monitoring. View the Messages in queue chart.