Deploy Compliance Package
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 packages are a core function of Config service, used to define and manage a set of related compliance rules. Through compliance packages, enterprises can quickly deploy rule sets that comply with specific compliance frameworks or standards, achieving automated compliance checks and evaluations. Compliance packages support pre-built templates and custom configurations, providing flexible rule management capabilities and comprehensive compliance management solutions for enterprises. This best practice will introduce how to use Terraform to automatically deploy Config compliance packages, including rule package template queries and compliance package creation.
Related Resources/Data Sources
This best practice involves the following main resources and data sources:
Data Sources
Resources
Resource/Data Source Dependencies
data.huaweicloud_rms_assignment_package_templates.test
└── huaweicloud_rms_assignment_package.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. Query Rule Package Templates via Data Source
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to perform a data source query, the result of which will be used to create the compliance package:
Parameter Description:
template_key: Built-in rule package template name, assigned by referencing the input variable template_key
3. Create Compliance Package
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a compliance package resource:
Parameter Description:
name: Rule package name, assigned by referencing the input variable assignment_package_name
template_key: Template key name, assigned by referencing the first template key name from the rule package template data source
vars_structure: Variable structure, dynamically creates rule package parameter configuration
var_key: Variable key name, uses the template parameter name
var_value: Variable value, uses the template parameter default value
4. 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="assignment_package_name=my-package" -var="template_key=my-template"Environment variables:
export TF_VAR_assignment_package_name=my-packageCustom 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 the compliance packageRun
terraform showto view the details of the created compliance package
Reference Information
Last updated