Deploy Timer Trigger
Application Scenario
FunctionGraph's timer trigger (Timer Trigger) is a trigger type based on time scheduling that supports both Cron expression and fixed interval scheduling methods. Through timer triggers, you can implement scheduled tasks, periodic data processing, scheduled backups, scheduled monitoring, and other functions.
Timer triggers are particularly suitable for tasks that need to be executed at fixed time intervals or specific time points, such as data cleanup, report generation, system monitoring, scheduled notifications, etc. This best practice will introduce how to use Terraform to automatically deploy a FunctionGraph function with a timer trigger.
Related Resources/Data Sources
This best practice involves the following main resources and data sources:
Data Sources
This best practice does not use data sources.
Resources
Resource/Data Source Dependencies
huaweicloud_fgs_function
└── huaweicloud_fgs_function_triggerOperation Steps
1. Script Preparation
Prepare the TF file (e.g., main.tf) in the specified workspace for writing the current best practice script, ensuring that it (or other TF files in the same directory) contains the provider version declaration and Huawei Cloud authentication information required for deploying resources. Refer to the "Preparation Before Deploying Huawei Cloud Resources" document for configuration introduction.
2. Create FunctionGraph Function
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a FunctionGraph function resource:
Parameter Description:
name: FunctionGraph function name, assigned by referencing the input variable function_name
app: Application name the function belongs to, set to "default" to use the default application
handler: Function entry point, set to "index.handler" indicating the handler method is in the index.py file
memory_size: Function memory size (MB), assigned by referencing the input variable function_memory_size, default value is 128MB
timeout: Function timeout (seconds), assigned by referencing the input variable function_timeout, default value is 10 seconds
runtime: Function runtime environment, assigned by referencing the input variable function_runtime, default value is Python2.7
code_type: Code type, set to "inline" for inline code
func_code: Function source code, assigned by base64 encoding the input variable function_code
description: Function description information, assigned by referencing the input variable function_description
3. Create FunctionGraph Timer Trigger
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a FunctionGraph timer trigger resource:
Parameter Description:
function_urn: URN of the FunctionGraph function associated with the trigger, assigned by referencing huaweicloud_fgs_function.test.urn
type: Trigger type, set to "TIMER" for timer trigger
status: Trigger status, assigned by referencing the input variable trigger_status, default value is "ACTIVE" for active status
event_data: Trigger event data, in JSON format containing the following parameters:
name: Trigger name, assigned by referencing the input variable trigger_name
schedule_type: Schedule type, assigned by referencing the input variable trigger_schedule_type, default value is "Cron" for Cron expression
sync_execution: Whether to execute synchronously, assigned by referencing the input variable trigger_sync_execution, default value is false for asynchronous execution
user_event: User event description, assigned by referencing the input variable trigger_user_event
schedule: Schedule expression, assigned by referencing the input variable trigger_schedule, supports Cron expression and fixed interval formats
4. Preset Input Parameters Required for Resource Deployment (Optional)
In this practice, some resources and data sources use input variables to assign values to configuration content. These input parameters need to be manually entered during subsequent deployments. At the same time, Terraform provides a method to preset these configurations through tfvars files, which can avoid repeated input during each execution.
Create a terraform.tfvars file in the working directory with the following example content:
Usage:
Save the above content as
terraform.tfvarsfile in the working directory (this file name allows users to automatically import the content of thistfvarsfile when executing terraform commands; for other names,.autoneeds to be added before tfvars, such asvariables.auto.tfvars)Modify parameter values as needed
When executing
terraform planorterraform apply, Terraform will automatically read the variable values from this file
In addition to using terraform.tfvars file, variable values can also be set in the following ways:
Command line parameters:
terraform apply -var="function_name=my-function" -var="trigger_name=my-trigger"Environment variables:
export TF_VAR_function_name=my-functionCustom named variable files:
terraform apply -var-file="custom.tfvars"
Note: If the same variable is set in multiple ways, Terraform will use the variable value according to the following priority: command line parameters > variable files > environment variables > default values.
5. Initialize and Apply Terraform Configuration
After completing the above script configuration, execute the following steps to create resources:
Run
terraform initto initialize the environmentRun
terraform planto view the resource creation planAfter confirming the resource plan is correct, run
terraform applyto start creating FunctionGraph function and timer triggerRun
terraform showto view the created FunctionGraph function and timer trigger
Reference Information
Last updated