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.
- Only one trigger can be created for each message queue.
Getting started
To create a trigger, you need:
-
A function that the trigger will invoke. If you do not have a function:
-
Service accounts with rights:
- To invoke a function.
- To read from the queue the trigger receives messages from.
- (Optional) To write to a Dead Letter Queue.
You can use the same service account or different ones. If you do not have a service account, create one.
-
A message queue that the trigger receives 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 your 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 rights to read messages from this message queue.
-
(Optional) 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 10. 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. However, the number of messages does not exceed the specified group 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 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 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 1 \
--batch-cutoff 10s
Where:
-
--name
: Trigger name. -
--queue
: Queue ID.To find out the queue ID:
- In the management console
, select the folder containing the queue. - Select Message Queue.
- Select the desired 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
: Service account with rights to read messages from the queue. -
--invoke-function-service-account-id
: Service account with rights to invoke the function. -
--batch-size
: Message batch size. This is an optional parameter. The values may range from 1 to 10. 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
Terraform
For more information about the provider resources, see the documentation on the Terraform
If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
If you don't have Terraform, install it and configure the Yandex Cloud provider.
To create a trigger for the message queue:
-
In the configuration file, describe the trigger parameters:
-
name
: Timer name. The name format is as follows:- The name must be from 3 to 63 characters long.
- It may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter and the last character cannot be a hyphen.
-
description
: Trigger description. -
message_queue
: Message queue parameters:-
queue_id
: Queue ID.To find out the queue ID:
- In the management console
, select the folder containing the queue. - Select Message Queue.
- Select the desired 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 rights to invoke a function. -
batch_size
: Message batch size. This is an optional parameter. The values may range from 1 to 10. 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 timer groups messages for a period not exceedingbatch-cutoff
and sends them to a function or container. The number of messages cannot exceedbatch-size
.
-
-
function
: Settings for the function, which will be activated by the trigger:id
: Function ID.
Here is an example of the configuration file structure:
resource "yandex_function_trigger" "my_trigger" { name = "<timer_name>" description = "<trigger_description>" message_queue { queue_id = "<queue_ID>" service_account_id = "<service_account_ID>" batch_size = "1" batch_cutoff = "10" } function { id = "<function_ID>" } }
For more information about resource parameters in Terraform, see the provider documentation
. -
-
Make sure the configuration files are correct.
-
In the command line, go to the directory where you created the configuration file.
-
Run a check using this command:
terraform plan
If the configuration is described correctly, the terminal will display a list of created resources and their parameters. If the configuration contains any errors, Terraform will point them out.
-
-
Deploy cloud resources.
-
If the configuration does not contain any errors, run this command:
terraform apply
-
Confirm creating the resources: type
yes
in the terminal and press Enter.All the resources you need will then be created in the specified folder. You can check the new resources and their configuration using the management console
or this CLI command:yc serverless trigger get <trigger_ID>
-
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.