Creating a trigger for Yandex Cloud Logging
Create a trigger for Cloud Logging that calls a Cloud Functions function or Serverless Containers container when entries are added to a log group.
Before you start
To create a trigger, you need:
-
A function or a container the trigger will launch.
-
If you don't have a function:
-
If you don't have a container:
-
-
The log group that activates the trigger when records are added to it. If you don't have a log group, create one.
-
(optional) A Dead Letter Queue where messages that could not be processed by a function or a container will be redirected. If you don't have a queue, create one.
-
Service accounts with rights to invoke a function or a container and (optionally) write messages to the Dead Letter Queue. You can use the same service account or different ones. If you don't have a service account, 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 wish to create your trigger.
-
Open Cloud Functions.
-
On the left-hand panel, select Triggers.
-
Click Create trigger.
-
Under Basic parameters:
- Enter a name and description for the trigger.
- In the Type field, select Cloud Logging.
- Choose what the trigger will launch — a function or a container.
-
Under Cloud Logging settings, specify:
- The log group.
- (Optional) Resource types: Yandex Cloud or your services, such as,
serverless.function
. - (Optional) IDs of Yandex Cloud or your resources, such as Cloud Functions functions.
- (Optional) Logging levels.
A trigger activates when records that match all the optional settings are added to the specified log group. If an optional setting is undefined, the trigger activates for any value of the setting.
-
(optional) Under Batch message settings, specify:
- Batch size. Valid values range from 1 to 100. The default is 1.
- Maximum wait time. Valid values range from 1 to 60 seconds. The default is 1 second.
The trigger groups messages for a period of time not exceeding the specified timeout and sends them to a function or container. However, the number of messages does not exceed the specified group size.
-
If the trigger launches:
-
A function, select one under Function settings and specify:
- Tag of the function version;
- A service account to be used to invoke the function.
-
A container, select one under Container settings and specify:
- A container revision;
- A service account to be used to invoke the container.
-
-
(optional) Under Repeat request settings:
- In the Interval field, specify the time after which the function or the container will be invoked again if the current attempt fails. Values can be from 10 to 60 seconds. The default is 10 seconds.
- In the Number of attempts field, specify the number of invocation retries before the trigger moves a message to the Dead Letter Queue. Values can be from 1 to 5. The default is 1.
-
(optional) Under Dead Letter Queue settings, select the Dead Letter Queue and the service account with write privileges to this queue.
-
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 --folder-name
or --folder-id
parameter.
To create a trigger that launches a function, run the command:
yc serverless trigger create logging \
--name <trigger name> \
--log-group-name <log group name> \
--batch-size 1 \
--batch-cutoff 1s \
--invoke-function-id <function ID> \
--invoke-function-service-account-id <service account ID> \
--retry-attempts 1 \
--retry-interval 10s \
--dlq-queue-id <Dead Letter Queue ID> \
--dlq-service-account-id <service account ID>
Where:
--name
: Trigger name.--log-group-name
: The name of the log group that will cause the function to be called when records are added.--batch-size
: Message batch size. Optional. Valid values range from 1 to 100. The default is 1.--batch-cutoff
: Maximum waiting time. Optional. Valid values range from 0 to 60 seconds. The default is 1 second. The trigger groups messages for a period not exceedingbatch-cutoff
and sends them to a function or container. At the same time, the number of messages does not exceedbatch-size
.--invoke-function-id
: Function ID.--invoke-function-service-account-id
: ID of the service account with rights to invoke the function.--retry-attempts
: The time after which the function will be invoked again if the current attempt fails. Optional. Values can be from 10 to 60 seconds. The default is 10 seconds.--retry-interval
: The number of invocation retries before the trigger moves a message to the Dead Letter Queue. Optional. Values can be from 1 to 5. The default is 1.--dlq-queue-id
: Dead Letter Queue ID. Optional.--dlq-service-account-id
: Service account with rights to write messages to the Dead Letter Queue. Optional.
Result:
id: a1sfe084v4se4morbu2i
folder_id: b1g88tflru0ek1omtsu0
created_at: "2019-12-04T08:45:31.131391Z"
name: logging-trigger
rule:
logging:
log-group-name: default
batch_settings:
size: "1"
cutoff: 1s
invoke_function:
function_id: d4eofc7n0m03********
function_tag: $latest
service_account_id: aje3932acd0c********
retry_settings:
retry_attempts: "1"
interval: 10s
dead_letter_queue:
queue-id: yrn:yc:ymq:ru-central1:aoek49ghmknn********:dlq
service-account-id: aje3932acd0c********
status: ACTIVE
You can create a trigger using the API create.
Checking the result
Make sure the trigger is working properly. To do this, view function logs or container logs: they show information about calls.