Create a trigger for Message Queue that sends messages to the Cloud Functions function
Create a trigger for a message queue in Message Queue and process the messages using the Cloud Functions function.
Warning
- You can only create a trigger for a standard message queue.
- The trigger must be in the same cloud as the queue from which it reads messages.
- You can create only one trigger for each message queue.
Getting started
To create a trigger, you need:
-
A function to be invoked by the trigger. If you do not have a function:
-
Service accounts with the following permissions:
- To invoke a function.
- To read from the queue the trigger receives messages from.
You can use the same service account or different ones. If you do not have a service account, create one.
-
Message queue the trigger will collect messages from. If you do not have a queue, create one.
Creating a trigger
Note
The trigger is initiated within 5 minutes of being created.
-
In the management console
, select the folder where you want to create a trigger. -
Select Cloud Functions.
-
In the left-hand panel, select
Triggers. -
Click Create trigger.
-
Under Basic settings:
- Enter a name and description for the trigger.
- In the Type field, select Message Queue.
- In the Launched resource field, select Function.
-
Under Message Queue message settings, select a message queue and a service account with permissions to read messages from this queue.
-
Under Batch message settings, specify:
- Waiting time, s. The values may range from 0 to 20 seconds. The default value is 10 seconds.
- Batch size. The values may range from 1 to 1,000. The default value is 1.
The trigger groups messages for a period of time not exceeding the specified timeout and sends them to a function. The number of messages cannot exceed the specified batch size.
-
Under Function settings, select a function and specify:
- Function version tag.
- Service account to invoke the function under.
-
Click Create trigger.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID>
command. You can also set a different folder for any specific command using the --folder-name
or --folder-id
parameter.
To create a trigger that invokes a function, run this 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 <message_group_size> \
--batch-cutoff <maximum_timeout>
Where:
-
--name
: Trigger name. -
--queue
: Queue IDTo find out the queue ID:
- In the management console
, go the folder containing the queue. - Select Message Queue.
- Select the queue.
- You can see the queue ID under General information in the ARN field.
- In the management console
-
--invoke-function-id
: Function ID. -
--queue-service-account-name
: ID of the service account with permissions to read messages from the queue. -
--invoke-function-service-account-id
: ID of the service account with the permissions to invoke the function. -
--batch-size
: Message batch size. This is an optional parameter. The values may range from 1 to 1,000. The default value is 1. -
--batch-cutoff
: Maximum wait time. This is an optional parameter. The values may range from 0 to 20 seconds. The default value is 10 seconds. The trigger groups messages for a period not exceedingbatch-cutoff
and sends them to a function. The number of messages cannot exceedbatch-size
.
Result:
id: dd0cspdch6**********
folder_id: aoek49ghmk**********
created_at: "2019-08-28T12:14:45.762915Z"
name: ymq-trigger
rule:
message_queue:
queue_id: yrn:yc:ymq:ru-central1:aoek49ghmk**********:my-mq
service_account_id: bfbqqeo6jk**********
batch_settings:
size: "1"
cutoff: 10s
invoke_function:
function_id: b09e5lu91t**********
function_tag: $latest
service_account_id: bfbqqeo6j**********
status: ACTIVE
With Terraform
Terraform is distributed under the Business Source License
For more information about the provider resources, see the relevant documentation on the Terraform
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
To create a trigger for Message Queue:
-
In the configuration file, describe the trigger parameters:
resource "yandex_function_trigger" "my_trigger" { name = "<trigger_name>" description = "<trigger_description>" function { id = "<function_ID>" service_account_id = "<service_account_ID>" } message_queue { queue_id = "<queue_ID>" service_account_id = "<service_account_ID>" batch_size = "<message_group_size>" batch_cutoff = "<maximum_timeout>" }
Where:
-
name
: Trigger name. The name format is as follows:- It must be from 2 to 63 characters long.
- It can only contain lowercase Latin letters, numbers, and hyphens.
- It must start with a letter and cannot end with a hyphen.
-
description
: Trigger description. -
function
: Function parameters:id
: Function ID.service_account_id
: ID of the service account with the permissions to invoke the function.
-
message_queue
: Trigger parameters:-
queue_id
: Message queue ID.To find out the queue ID:
- In the management console
, go the folder containing the queue. - Select Message Queue.
- Select the queue.
- You can see the queue ID under General information in the ARN field.
- In the management console
-
service_account_id
: ID of the service account with permissions to read messages from the queue. -
batch_size
: Message batch size. This is an optional parameter. The values may range from 1 to 1,000. The default value is 1. -
batch_cutoff
: Maximum wait time. This is an optional parameter. The values may range from 0 to 20 seconds. The default value is 10 seconds. The trigger groups messages for a period not exceedingbatch-cutoff
and sends them to a function. The number of messages cannot exceedbatch-size
.
-
For more information about the
yandex_function_trigger
resource parameters, see the provider documentation . -
-
Create resources:
-
In the terminal, go to the directory where you edited the configuration file.
-
Make sure the configuration file is correct using this command:
terraform validate
If the configuration is correct, you will get this message:
Success! The configuration is valid.
-
Run this command:
terraform plan
You will see a detailed list of resources. No changes will be made at this step. If the configuration contains any errors, Terraform will show them.
-
Apply the changes:
terraform apply
-
Type
yes
and press Enter to confirm the changes.
Terraform will create all the required resources. You can check the new resources using the management console
or this CLI command:yc serverless trigger list
-
To create a trigger for Yandex Message Queue, use the create REST API method for the Trigger resource or the TriggerService/Create gRPC API call.
Checking the result
Check that the trigger operates correctly. Do it by viewing function logs that present information on invocations.
Check that the number of enqueued messages is decreasing. To do this, view the queue statistics:
- In the management console
, select Message Queue. - Select the queue that you created the trigger for.
- Go to Monitoring. Check the Messages in the queue chart.