Deploy Compliance Rule
Application Scenario
Configuration Audit (Config) is a one-stop compliance management service provided by Huawei Cloud, helping users continuously monitor and evaluate the configuration compliance of cloud resources. Config service provides pre-built compliance rule packages and custom rules, supporting multiple compliance frameworks and standards, helping enterprises establish a comprehensive compliance management system.
Compliance rules are a core function of Config service, used to define and execute specific compliance check policies. Through compliance rules, enterprises can monitor whether cloud resource configurations meet security and compliance requirements, and promptly discover and fix configuration risks. Compliance rules support multiple resource types and check conditions, including tag checks, configuration validation, security policies, etc., providing enterprises with comprehensive compliance management solutions. This best practice will introduce how to use Terraform to automatically deploy Config compliance rules, including VPC creation, ECS instance creation, compliance rule configuration, and rule evaluation.
Related Resources/Data Sources
This best practice involves the following main resources and data sources:
Resources
Resource/Data Source Dependencies
huaweicloud_vpc.test
└── huaweicloud_vpc_subnet.test
└── huaweicloud_compute_instance.test
└── huaweicloud_rms_policy_assignment.test
└── huaweicloud_rms_policy_assignment_evaluate.test
└── huaweicloud_networking_secgroup.test
└── huaweicloud_compute_instance.test
└── huaweicloud_rms_policy_assignment.test
└── huaweicloud_rms_policy_assignment_evaluate.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 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
3. Create VPC Subnet Resource
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, assigned by referencing the input variable subnet_cidr, automatically calculated if empty
gateway_ip: Subnet gateway IP, assigned by referencing the input variable subnet_gateway_ip, automatically calculated if empty
4. Create Security Group Resource
Add the following script to the TF file 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
5. Create ECS Instance Resource
Add the following script to the TF file to instruct Terraform to create an ECS instance resource:
Parameter Description:
name: Instance name, assigned by referencing the input variable ecs_instance_name
image_name: Image name, assigned by referencing the input variable ecs_image_name
flavor_name: Flavor name, assigned by referencing the input variable ecs_flavor_name
security_group_ids: Security group ID list, assigned by referencing the security group resource (huaweicloud_networking_secgroup.test) ID
availability_zone: Availability zone, assigned by referencing the input variable availability_zone
network: Network configuration, assigned by referencing the VPC subnet resource (huaweicloud_vpc_subnet.test) ID
tags: Tags, assigned by referencing the input variable ecs_tags
6. Create Compliance Rule
Add the following script to the TF file to instruct Terraform to create a compliance rule resource:
Parameter Description:
name: Policy assignment name, assigned by referencing the input variable policy_assignment_name
description: Policy assignment description, assigned by referencing the input variable policy_assignment_description
policy_definition_id: Policy definition ID, assigned by referencing the input variable policy_definition_id
policy_filter: Policy filter, dynamically creates resource filtering conditions
region: Region, assigned by referencing the region in the filter configuration
resource_provider: Resource provider, assigned by referencing the resource_provider in the filter configuration
resource_type: Resource type, assigned by referencing the resource_type in the filter configuration
resource_id: Resource ID, assigned by referencing the resource_id in the filter configuration
tag_key: Tag key, assigned by referencing the tag_key in the filter configuration
tag_value: Tag value, assigned by referencing the tag_value in the filter configuration
parameters: Parameters, assigned by referencing the input variable policy_assignment_parameters
tags: Tags, assigned by referencing the input variable policy_assignment_tags
7. Evaluate Compliance Rule
Add the following script to the TF file to instruct Terraform to evaluate the compliance rule:
Parameter Description:
policy_assignment_id: Policy assignment ID, assigned by referencing the compliance rule resource (huaweicloud_rms_policy_assignment.test) ID
8. 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="policy_assignment_name=my-policy"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.
9. 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 compliance ruleRun
terraform showto view the details of the created compliance rule
Reference Information
Last updated