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.

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_id is empty and var.role_policy is empty

  • name: 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_id is empty and var.role_policy is not empty

  • name: The name of the IAM role, assigned by referencing the input variable role_name

  • type: The type of the IAM role, assigned by referencing the input variable role_type, default is "XA" indicating a custom role

  • description: The description of the IAM role, assigned by referencing the input variable role_description

  • policy: 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_id is empty

  • name: The name of the IAM user group, assigned by referencing the input variable group_name

  • description: 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_id is empty

  • name: 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_id is specified, use that value; otherwise, reference the ID of the IAM user group resource created earlier

  • role_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 ID

  • domain_id: The ID of the IAM domain. If var.authorized_domain_id is specified, use that value; otherwise, it is null, indicating authorization at the project level

  • project_id: The ID of the IAM project. When var.authorized_domain_id is empty, if var.authorized_project_id is specified, use that value; otherwise, use the queried IAM project data source ID. When var.authorized_domain_id is 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_id is specified, it indicates authorization at the domain level; when project_id is 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_configuration list

  • name: The name of the IAM user, obtained from the name field of the corresponding user in the var.users_configuration list

  • password: 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_id is specified, use that value; otherwise, reference the ID of the IAM user group resource created earlier

  • users: 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:

  1. Save the above content as a terraform.tfvars file in the working directory (this filename allows users to automatically import the content of this tfvars file when executing terraform commands. For other names, you need to add .auto before tfvars, such as variables.auto.tfvars)

  2. Modify parameter values according to actual needs

  3. When executing terraform plan or terraform 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:

  1. Command line parameters: terraform apply -var="role_name=my-role" -var="group_name=my-group"

  2. Environment variables: export TF_VAR_role_name=my-role

  3. Custom 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:

  1. Run terraform init to initialize the environment

  2. Run terraform plan to view the resource creation plan

  3. After confirming that the resource plan is correct, run terraform apply to start creating IAM roles, user groups, and users, and authorize users through user groups

  4. Run terraform show to view the created IAM resource details

Reference Information

Last updated