Deploy Virtual Interface
Application Scenario
Direct Connect (DC) is a high-performance, low-latency, secure, and reliable dedicated line access service provided by Huawei Cloud, offering enterprises dedicated network connections from local data centers to Huawei Cloud. Direct Connect service supports multiple access methods, including physical dedicated lines and virtual dedicated lines, meeting network connection requirements for different scales and scenarios.
Virtual interface is a core component in Direct Connect service, used to establish logical connections between dedicated lines and VPCs. Through virtual interfaces, you can achieve routing and forwarding of dedicated line traffic, supporting both static routing and BGP dynamic routing, meeting the needs of different network architectures. This best practice will introduce how to use Terraform to automatically deploy DC virtual interfaces, including VPC creation, virtual gateway creation, and virtual interface configuration.
Related Resources/Data Sources
This best practice involves the following main resources and data sources:
Data Sources
None
Resources
Resource/Data Source Dependencies
huaweicloud_vpc.test
└── huaweicloud_dc_virtual_gateway.test
└── huaweicloud_dc_virtual_interface.testOperation 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 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
3. Create DC Virtual Gateway
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a DC virtual gateway resource:
Parameter Description:
vpc_id: VPC ID, assigned by referencing the VPC resource (huaweicloud_vpc.test) ID
name: Virtual gateway name, assigned by referencing the input variable virtual_gateway_name
local_ep_group: Local endpoint group, assigned by referencing the VPC resource (huaweicloud_vpc.test) CIDR
4. Create DC Virtual Interface
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a DC virtual interface resource:
Parameter Description:
direct_connect_id: Direct connect connection ID, assigned by referencing the input variable direct_connect_id
vgw_id: Virtual gateway ID, assigned by referencing the DC virtual gateway resource (huaweicloud_dc_virtual_gateway.test) ID
name: Virtual interface name, assigned by referencing the input variable virtual_interface_name
description: Virtual interface description, assigned by referencing the input variable virtual_interface_description
type: Virtual interface type, assigned by referencing the input variable virtual_interface_type, supports "private" and "public"
route_mode: Routing mode, assigned by referencing the input variable route_mode, supports "static" and "bgp"
vlan: Customer-side VLAN, assigned by referencing the input variable vlan
bandwidth: Inbound bandwidth size, assigned by referencing the input variable bandwidth
remote_ep_group: Remote subnet CIDR list, assigned by referencing the input variable remote_ep_group
address_family: Address family type, assigned by referencing the input variable address_family, supports "ipv4" and "ipv6"
local_gateway_v4_ip: Cloud-side IPv4 address, assigned by referencing the input variable local_gateway_v4_ip
remote_gateway_v4_ip: Customer-side IPv4 address, assigned by referencing the input variable remote_gateway_v4_ip
enable_bfd: Whether to enable BFD function, assigned by referencing the input variable enable_bfd
enable_nqa: Whether to enable NQA function, assigned by referencing the input variable enable_nqa
tags: Tags, assigned by referencing the input variable virtual_interface_tags
5. 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="vpc_name=my-vpc" -var="virtual_interface_name=my-interface"Environment variables:
export TF_VAR_vpc_name=my-vpcCustom 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.
6. 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 the DC virtual interfaceRun
terraform showto view the details of the created DC virtual interface
Reference Information
Last updated