Deploy EG Trigger

Application Scenario

FunctionGraph's EG trigger (EventGrid Trigger) is a trigger type based on the EventGrid service that can monitor and respond to Huawei Cloud resource operation events. Through EG triggers, you can implement event-driven image processing, file conversion, data synchronization, real-time response, and other functions.

EG triggers are particularly suitable for scenarios that require real-time monitoring of OBS object storage events, image processing, and automated workflows, such as image thumbnail generation, file format conversion, data backup, event notification, etc. This best practice will introduce how to use Terraform to automatically deploy a FunctionGraph function with an EG trigger, implementing automatic thumbnail generation when OBS files are uploaded.

This best practice involves the following main resources and data sources:

Data Sources

Resources

Resource/Data Source Dependencies

Operation 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 OBS Buckets

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create OBS bucket resources:

Parameter Description:

  • bucket: OBS bucket name, assigned by referencing the input variables source_bucket_name and target_bucket_name

  • acl: Bucket access control list, set to "private" for private access

  • force_destroy: Whether to force delete the bucket, set to true to allow deletion of non-empty buckets

3. Query FunctionGraph Dependencies Information Through Data Source

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to perform a data source query, the results of which are used to create FunctionGraph functions:

Parameter Description:

  • type: Dependency type, set to "public" to query public dependencies

  • name: Dependency name, set to "pillow-7.1.2" to query the Pillow image processing library

  • dependency_id: Dependency ID, assigned by referencing the return result from data source huaweicloud_fgs_dependencies

  • version: Dependency version, set to 1 to query the first version

4. Query EventGrid Event Channels Information Through Data Source

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to perform a data source query, the results of which are used to create FunctionGraph EG triggers:

Parameter Description:

  • provider_type: Event channel provider type, set to "OFFICIAL" to query official event channels

  • name: Event channel name, set to "default" to query the default event channel

5. 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

  • agency: Function agency name, assigned by referencing the input variable function_agency_name, used for function permissions to access other Huawei Cloud services

  • 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 256MB

  • timeout: Function timeout (seconds), assigned by referencing the input variable function_timeout, default value is 40 seconds

  • runtime: Function runtime environment, assigned by referencing the input variable function_runtime, default value is Python3.6

  • code_type: Code type, set to "inline" for inline code

  • func_code: Function source code (for automatic image compression), assigned by base64 encoding the input variable function_code

  • description: Function description information, assigned by referencing the input variable function_description

  • depend_list: Function dependency list, assigned by referencing the return result from data source huaweicloud_fgs_dependency_versions

  • user_data: User custom data, in JSON format containing output bucket name and OBS endpoint information

6. Create FunctionGraph EG Trigger

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a FunctionGraph EG trigger resource:

Parameter Description:

  • depends_on: Explicit dependency relationship, ensuring the source OBS bucket exists before trigger creation

  • function_urn: URN of the FunctionGraph function associated with the trigger, assigned by referencing huaweicloud_fgs_function.test.urn

  • type: Trigger type, set to "EVENTGRID" for EG trigger

  • cascade_delete_eg_subscription: Whether to cascade delete EG subscription, set to true to delete EG subscription when function is deleted

  • 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:

    • channel_id: Event channel ID, assigned by referencing the return result from data source huaweicloud_eg_event_channels

    • channel_name: Event channel name, assigned by referencing the return result from data source huaweicloud_eg_event_channels

    • source_name: Event source name, set to "HC.OBS.DWR" for OBS data workflow event source

    • trigger_name: Trigger name suffix, assigned by referencing the input variable trigger_name_suffix

    • agency: Agency name used by the trigger, assigned by referencing the input variable trigger_agency_name

    • bucket: OBS bucket name to monitor, assigned by referencing the input variable source_bucket_name

    • event_types: Event types to monitor, set to monitor OBS object creation events

    • Key_encode: Whether to encode object keys, set to true to enable encoding

7. 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:

  1. Save the above content as terraform.tfvars file in the working directory (this file name allows users to automatically import the content of this tfvars file when executing terraform commands; for other names, .auto needs to be added before tfvars, such as variables.auto.tfvars)

  2. Modify parameter values as needed

  3. When executing terraform plan or terraform 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:

  1. Command line parameters: terraform apply -var="function_name=my-function" -var="source_bucket_name=my-bucket"

  2. Environment variables: export TF_VAR_function_name=my-function

  3. Custom 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.

8. Initialize and Apply Terraform Configuration

After completing the above script configuration, execute the following steps to create resources:

  1. Run terraform init to initialize the environment

  2. Run terraform plan to view the resource creation plan

  3. After confirming the resource plan is correct, run terraform apply to start creating OBS buckets, FunctionGraph function, and EG trigger

  4. Run terraform show to view the created OBS buckets, FunctionGraph function, and EG trigger

Reference Information

Last updated