Deploy Basic Network
Application Scenario
Virtual Private Cloud (VPC) is a logically isolated network space that users can customize and manage on Huawei Cloud. Through VPC, users can flexibly divide subnets, configure routes and security policies, implementing secure isolation and efficient management of cloud resources. This best practice will introduce how to use Terraform to automatically deploy a basic VPC and its subnets.
Related Resources/Data Sources
This best practice involves the following main resources:
Resources
Resource/Data Source Dependencies
huaweicloud_vpc
└── huaweicloud_vpc_subnetOperation 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 Resource
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, default value is "172.16.0.0/16"
enterprise_project_id: Enterprise project ID, assigned by referencing the input variable enterprise_project_id, default value is null
3. Create VPC Subnet Resource
Add the following script to the TF file to instruct Terraform to create a VPC subnet resource:
Parameter Description:
vpc_id: VPC ID that the subnet belongs to, referencing the ID of the previously created VPC resource
name: Subnet name, assigned by referencing the input variable subnet_name
cidr: Subnet CIDR block, assigned by referencing the input variable subnet_cidr, default value is "172.16.10.0/24"
gateway_ip: Subnet gateway IP, assigned by referencing the input variable subnet_gateway, default value is "172.16.10.1"
dns_list: List of DNS server IP addresses for the subnet, assigned by referencing the input variable dns_list, default value is null
4. Preset Input Parameters Required for Resource Deployment (Optional)
In this practice, some resources 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="subnet_name=my-subnet"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.
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 VPC and subnetsRun
terraform showto view the created VPC and subnet details
Reference Information
Last updated