Deploy CTS Trigger
Application Scenario
FunctionGraph's CTS trigger (Cloud Trace Service Trigger) is a trigger type based on the Cloud Trace Service (CTS) that can monitor and respond to Huawei Cloud resource operation events. Through CTS triggers, you can implement security auditing, compliance monitoring, automated response, event notification, and other functions.
CTS triggers are particularly suitable for scenarios that require real-time monitoring of cloud resource operations, security auditing, and automated operations, such as resource change monitoring, security event response, compliance checks, operation log analysis, etc. This best practice will introduce how to use Terraform to automatically deploy a FunctionGraph function with a CTS 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
agency: Function agency name, assigned by referencing the input variable function_agency_name, used for function permissions to access other Huawei Cloud services
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 CTS Trigger
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a FunctionGraph CTS 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 "CTS" for CTS 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
operations: List of operations to monitor, assigned by referencing the input variable trigger_operations, supports monitoring specific cloud service operations
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 CTS triggerRun
terraform showto view the created FunctionGraph function and CTS trigger
Reference Information
Last updated