Deploy Flow Log

Application Scenario

Enterprise Router (ER) is a high-performance, highly available enterprise-grade router service provided by Huawei Cloud, supporting enterprise-level network functions such as multi-VPC interconnection, dedicated line access, and VPN connections. ER service provides flexible routing policies and rich network connectivity capabilities, meeting complex enterprise network architecture requirements.

ER flow logs are an important feature of the ER service, used to record and monitor network traffic information on enterprise routers, including detailed information such as source addresses, destination addresses, protocol types, ports, etc. of data packets. Through flow logs, enterprises can analyze network traffic patterns, monitor network performance, conduct security audits, and troubleshoot issues. This best practice will introduce how to use Terraform to automatically deploy flow logs, including VPC creation, ER instance creation, VPC connection, LTS log group creation, and flow log configuration.

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. Query ER Availability Zone 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 ER instances:

Parameter Description:

  • No additional parameters required, the data source will automatically get all ER availability zone information in the current region

3. Create VPC

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

Parameter Description:

  • name: VPC name, assigned by referencing the input variable vpc_name

  • cidr: VPC CIDR block, assigned by referencing the input variable vpc_cidr

4. Create VPC Subnet

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

Parameter Description:

  • vpc_id: VPC ID, assigned by referencing the VPC resource (huaweicloud_vpc.test) ID

  • name: Subnet name, assigned by referencing the input variable subnet_name

  • cidr: Subnet CIDR block, prioritizes using input variable, calculates using cidrsubnet function if empty

  • gateway_ip: Gateway IP, prioritizes using input variable, calculates using cidrhost function if empty

5. Create ER Instance

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create an ER instance resource:

Parameter Description:

  • availability_zones: Availability zone list, using the first result from ER availability zone list query data source

  • name: Instance name, assigned by referencing the input variable er_instance_name

  • asn: ASN number, assigned by referencing the input variable er_instance_asn

6. Create ER VPC Connection

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create an ER VPC connection resource:

Parameter Description:

  • instance_id: ER instance ID, assigned by referencing the ER instance resource (huaweicloud_er_instance.test) ID

  • vpc_id: VPC ID, assigned by referencing the VPC resource (huaweicloud_vpc.test) ID

  • subnet_id: Subnet ID, assigned by referencing the VPC subnet resource (huaweicloud_vpc_subnet.test) ID

  • name: Connection name, assigned by referencing the input variable er_vpc_attachment_name

  • auto_create_vpc_routes: Auto create VPC routes, assigned by referencing the input variable er_vpc_attachment_auto_create_vpc_routes

7. Create LTS Log Group

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create an LTS log group resource:

Parameter Description:

  • group_name: Log group name, assigned by referencing the input variable lts_group_name

  • ttl_in_days: Log retention days, assigned by referencing the input variable lts_group_ttl_in_days

8. Create LTS Log Stream

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create an LTS log stream resource:

Parameter Description:

  • group_id: Log group ID, assigned by referencing the LTS log group resource (huaweicloud_lts_group.test) ID

  • stream_name: Log stream name, assigned by referencing the input variable lts_stream_name

9. Create ER Flow Log

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create an ER flow log resource:

Parameter Description:

  • name: Flow log name, assigned by referencing the input variable er_flow_log_name

  • instance_id: ER instance ID, assigned by referencing the ER instance resource (huaweicloud_er_instance.test) ID

  • log_store_type: Log storage type, assigned by referencing the input variable er_flow_log_store_type

  • log_group_id: Log group ID, assigned by referencing the LTS log group resource (huaweicloud_lts_group.test) ID

  • log_stream_id: Log stream ID, assigned by referencing the LTS log stream resource (huaweicloud_lts_stream.test) ID

  • resource_type: Resource type, assigned by referencing the input variable er_flow_log_resource_type

  • resource_id: Resource ID, assigned by referencing the ER VPC connection resource (huaweicloud_er_vpc_attachment.test) ID

10. 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="vpc_name=my-vpc" -var="er_instance_name=my-er"

  2. Environment variables: export TF_VAR_vpc_name=my-vpc

  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.

11. 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 flow logs

  4. Run terraform show to view the created flow logs

Reference Information

Last updated