Deploy Security Group
Application Scenario
Security Group is a virtual firewall in Huawei Cloud VPC used to control network access. By configuring security group rules, you can precisely control network access permissions for cloud servers, databases, and other resources. Security groups support both inbound and outbound rule configurations, effectively protecting the security of cloud resources. This best practice will introduce how to use Terraform to automatically deploy security groups and their rule configurations.
Related Resources/Data Sources
This best practice involves the following main resources:
Resources
Resource/Data Source Dependencies
huaweicloud_networking_secgroup
└── huaweicloud_networking_secgroup_ruleOperation 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 Security Group Resource
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a security group resource:
Parameter Description:
name: Security group name, assigned by referencing the input variable security_group_name
delete_default_rules: Whether to delete default rules, set to true to delete default rules
3. Create Security Group Rule Resources
Add the following script to the TF file to instruct Terraform to batch create security group rules:
Parameter Description:
direction: Rule direction, assigned by referencing the input variable security_group_rule_configurations, default value is "ingress"
ethertype: Ethernet type, assigned by referencing the input variable security_group_rule_configurations, default value is "IPv4"
protocol: Protocol type, assigned by referencing the input variable security_group_rule_configurations, default value is null
ports: Port range, assigned by referencing the input variable security_group_rule_configurations, default value is null
remote_ip_prefix: Remote IP address range, assigned by referencing the input variable security_group_rule_configurations, default value is "0.0.0.0/0"
security_group_id: Security group ID, referencing the ID of the previously created security group resource
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="security_group_name=my-security-group"Environment variables:
export TF_VAR_security_group_name=my-security-groupCustom 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 security groups and rulesRun
terraform showto view the created security group and rule details
Reference Information
Last updated