Deploy Users Authorized Through Group
Application Scenario
Identity and Access Management (IAM) is a fundamental identity authentication and access management service provided by Huawei Cloud, achieving fine-grained permission control through flexible combinations of users, user groups, roles, and policies. Authorizing users through user groups is a common permission management approach that can simplify permission management processes and improve management efficiency.
This best practice will introduce how to use Terraform to automatically deploy IAM roles, user groups, and users, and authorize users through user groups. Through this approach, you can implement group-based permission management. When you need to grant the same permissions to multiple users, you only need to add users to the corresponding user group, and they will automatically inherit the user group's permissions without needing to configure permissions for each user individually.
Related Resources/Data Sources
This best practice involves the following main resources and data sources:
Data Sources
Resources
Resource/Data Source Dependencies
Implementation Steps
1. Script Preparation
Prepare the TF file (such as 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 introduction in Preparation Before Deploying Huawei Cloud Resources for configuration introduction.
2. Query IAM Role Information Through Data Source
Add the following script to the TF file (such as main.tf) to instruct Terraform to perform a data source query, the query results are used to create IAM role assignment resources:
Parameter Description:
count: The number of data sources to create, used to control whether to execute the IAM role query data source. The data source is only created (i.e., IAM role query is executed) when
var.role_idis empty andvar.role_policyis emptyname: The name of the IAM role, assigned by referencing the input variable
role_name
3. Create IAM Role Resource
Add the following script to the TF file to instruct Terraform to create IAM role resources:
Parameter Description:
count: The number of resources to create, used to control whether to create IAM role resources. Resources are only created when
var.role_idis empty andvar.role_policyis not emptyname: The name of the IAM role, assigned by referencing the input variable
role_nametype: The type of the IAM role, assigned by referencing the input variable
role_type, default is "XA" indicating a custom roledescription: The description of the IAM role, assigned by referencing the input variable
role_descriptionpolicy: The policy of the IAM role, assigned by referencing the input variable
role_policy, the policy is a JSON format string
4. Create IAM User Group Resource
Add the following script to the TF file to instruct Terraform to create IAM user group resources:
Parameter Description:
count: The number of resources to create, used to control whether to create IAM user group resources. Resources are only created when
var.group_idis emptyname: The name of the IAM user group, assigned by referencing the input variable
group_namedescription: The description of the IAM user group, assigned by referencing the input variable
group_description
5. Query IAM Project Information Through Data Source
Add the following script to the TF file to instruct Terraform to perform a data source query, the query results are used to create IAM role assignment resources:
Parameter Description:
count: The number of data sources to create, used to control whether to execute the IAM project query data source. The data source is only created (i.e., IAM project query is executed) when
var.authorized_project_idis emptyname: The name of the IAM project, assigned by referencing the input variable
authorized_project_name
6. Create IAM Role Assignment Resource
Add the following script to the TF file to instruct Terraform to create IAM role assignment resources:
Parameter Description:
group_id: The ID of the IAM user group. If
var.group_idis specified, use that value; otherwise, reference the ID of the IAM user group resource created earlierrole_id: The ID of the IAM role. Based on variable configuration, choose to use
var.role_id, the created IAM role resource ID, or the queried IAM role data source IDdomain_id: The ID of the IAM domain. If
var.authorized_domain_idis specified, use that value; otherwise, it is null, indicating authorization at the project levelproject_id: The ID of the IAM project. When
var.authorized_domain_idis empty, ifvar.authorized_project_idis specified, use that value; otherwise, use the queried IAM project data source ID. Whenvar.authorized_domain_idis not empty, this parameter is null, indicating authorization at the domain level
Note: IAM role assignment supports authorization at the domain level or project level. When
domain_idis specified, it indicates authorization at the domain level; whenproject_idis specified, it indicates authorization at the project level. The two cannot be specified simultaneously.
7. Create Random Password Resource
Add the following script to the TF file to instruct Terraform to create random password resources (automatically generated when users do not specify a password):
Parameter Description:
count: The number of resources to create, used to control whether to create random password resources. Resources are only created when there are users who have not specified a password
length: The length of the password, set to 16 characters
special: Whether to include special characters, set to true to include special characters
override_special: The special character set, set to "_%@" indicating that the password can include underscore, percent sign, and @ symbol
8. Create IAM User Resource
Add the following script to the TF file to instruct Terraform to create IAM user resources:
Parameter Description:
count: The number of resources to create, creating the corresponding number of IAM user resources based on the length of the
var.users_configurationlistname: The name of the IAM user, obtained from the name field of the corresponding user in the
var.users_configurationlistpassword: The password of the IAM user. If a password is specified in the user configuration, use that password; otherwise, use the password generated by the random password resource
9. Create IAM User Group Membership Resource
Add the following script to the TF file to instruct Terraform to create IAM user group membership resources:
Parameter Description:
group: The ID of the IAM user group. If
var.group_idis specified, use that value; otherwise, reference the ID of the IAM user group resource created earlierusers: The list of IAM user IDs, referencing the IDs of all IAM user resources created earlier, using the
[*]syntax to get the IDs of all user resources
10. Preset Input Parameters Required for Resource Deployment (Optional)
In this practice, some resources and data sources use input variables to assign configuration content. These input parameters need to be manually entered during subsequent deployment. 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 a
terraform.tfvarsfile in the working directory (this filename allows users to automatically import the content of thistfvarsfile when executing terraform commands. For other names, you need to add.autobefore tfvars, such asvariables.auto.tfvars)Modify parameter values according to actual needs
When executing
terraform planorterraform apply, Terraform will automatically read the variable values in this file
In addition to using the terraform.tfvars file, you can also set variable values through the following methods:
Command line parameters:
terraform apply -var="role_name=my-role" -var="group_name=my-group"Environment variables:
export TF_VAR_role_name=my-roleCustom named variable file:
terraform apply -var-file="custom.tfvars"
Note: If the same variable is set through multiple methods, Terraform will use variable values according to the following priority: command line parameters > variable file > environment variables > default values.
11. 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 that the resource plan is correct, run
terraform applyto start creating IAM roles, user groups, and users, and authorize users through user groupsRun
terraform showto view the created IAM resource details
Reference Information
Last updated