Creating a timer that invokes a function
Create a timer that invokes a Cloud Functions function every minute.
Getting started
- Create a function to be triggered by timer. For example, you can create any function from this list.
- Create a service account that will be used to invoke the function and assign it the
functions.functionInvoker
role.
Create a timer
Note
The trigger is initiated within 5 minutes of being created.
-
In the management console
, select the folder where you want to create a timer. -
Select Cloud Functions.
-
In the left-hand panel, select
Triggers. -
Click Create trigger.
-
Under Basic settings:
- Enter the trigger name:
timer
. - In the Type field, select
Timer
. - In the Launched resource field, select
Function
.
- Enter the trigger name:
-
Under Timer settings, enter
* * ? * * *
or selectEvery minute
. -
Under Function settings, select your function and specify:
- Function version tag:
$latest
- Service account you created earlier
- Function version tag:
-
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 timer that invokes a function every minute, run this command:
yc serverless trigger create timer \
--name timer \
--cron-expression '* * ? * * *' \
--invoke-function-id <function_ID> \
--invoke-function-service-account-id <service_account_ID>
Where:
--name
: Timer name.--cron-expression
: Function invocation schedule specified as a cron expression.--invoke-function-id
: Function ID.--invoke-function-service-account-id
: Service account ID.
Result:
id: a1s2aanidtep********
folder_id: b1gtmgn9gbvm********
created_at: "2023-03-03T12:18:15.707328472Z"
name: timer
rule:
timer:
cron_expression: '* * ? * * *'
invoke_function_with_retry:
function_id: d4eaic3se926********
function_tag: $latest
service_account_id: ajek1us5r79c********
status: ACTIVE
You can create a timer using the create API method.
Check the result
To make sure the timer is running properly, view the function logs. They should show that the function is invoked every minute.
-
In the management console
, select the folder containing your function. -
Select Cloud Functions.
-
Click a function to view its runtime log.
-
In the window that opens, go to Logs and specify the period for which you want to view logs. The default period is 1 hour.
To view the function logs, run this command:
yc serverless function logs <function_ID>
Result:
2023-03-03 12:44:12 INFO START RequestID: 5906fbf3-7ff5-4fe4-a0b2-b35c******** Version: d4efs25vm37e********
2023-03-03 12:44:12 INFO END RequestID: 5906fbf3-7ff5-4fe4-a0b2-b35c********
2023-03-03 12:44:12 INFO REPORT RequestID: 5906fbf3-7ff5-4fe4-a0b2-b35c******** Duration: 8.951 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 48 MB Queuing Duration: 0.066 ms Function Init Duration: 5.892 ms
2023-03-03 12:45:12 INFO START RequestID: e806a5c4-e06a-4a6f-b468-386d******** Version: d4efs25vm37e********
2023-03-03 12:45:12 INFO END RequestID: e806a5c4-e06a-4a6f-b468-386d********
2023-03-03 12:45:12 INFO REPORT RequestID: e806a5c4-e06a-4a6f-b468-386d******** Duration: 10.266 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 48 MB Queuing Duration: 0.054 ms Function Init Duration: 7.023 ms