TheKoguryo's Tech Blog

Version 2023.04.29

Warning

This content has been generated by machine translation. The translations are automated and have not undergone human review or validation.

14.2.6 Example, creating a user using list and count

When creating resources, there are cases where multiple identical resources are created through Array. Considering the situation where multiple users are created to compose the practice environment, let’s see how to create multiple users at once through list and count.

Policy required for user creation

Allow group <group_name> to manage users in tenancy

Create User

OCI users can be created using the syntax oci_identity_user.

  • example

    resource "oci_identity_user" "test_user" {
      #Required
      compartment_id = "${var.tenancy_ocid}"
      description    = "${var.user_description}"
      name           = "${var.user_name}"
    
      #Optional
      defined_tags   = {"Operations.CostCenter"= "42"}
      email          = "${var.user_email}"
      freeform_tags  = {"Department"= "Finance"}
    }
    

Create multiple users using count

By default, one resource block corresponds to one resource. You can create multiple identical types by adding count here. In general, set the number of iterations as count as in loop programming, and use count.index to inquire the current loop value. So, you can designate the number of resources to create in count as shown below, and use count.index to avoid conflicting resource names.

  • Example of creating as many users as desired, such as testuser_01 and testuser_02

    resource "oci_identity_user" "test_user" {
      count          = 2  
    
      #Required
      compartment_id = "${var.tenancy_ocid}"
      description    = "testuser_${format("%02d", count.index + 1)}"
      name           = "testuser_${format("%02d", count.index + 1)}"
    }
    
  • Example of the entire contents of users.tf

    • For provider.tf and terraform.tfvars, the previous practice files are used as they are.
    ### Create Users
    resource "oci_identity_user" "users_1" {
      count          = 2
    
      compartment_id = "${var.tenancy_ocid}"
      description    = "testuser_${format("%02d", count.index + 1)}"
      name           = "testuser_${format("%02d", count.index + 1)}"
    }
    
    ### Set User Passwords
    ### This is one time password.
    resource "oci_identity_ui_password" "users_1_password" {
      count   = 2
      user_id = "${oci_identity_user.users_1.*.id[count.index]}"
    }
    
    ### Outputs
    output "my_users_1_password" {
      sensitive = false
      value     = "${concat(oci_identity_user.users_1.*.name, oci_identity_ui_password.users_1_password.*.password)}"
    }
    
  • Execution result You can see that the output and OCI have been created as many as the number of counts specified by the user.

    [opc@bastion-host example_user_count]$ terraform apply
    
    ...
    
    Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    my_users_1_password = [
      "testuser_01",
      "testuser_02",
      ":<7:qa.Z[XqvKa19uL$u",
      "s.;sl72P_.v]Eu]V{rxu",
    ]
    [opc@bastion-host example_user_count]$
    

    image-20220119143947359

Create multiple users using list and count

Unlike the previous example, if you want to create a completely different name for each user, you can create multiple users with different names by combining the list type array variable and count.

  • Example of creating multiple users with different names

    Create a list type variable and use "${element(LIST, INDEX)}", "${length(LIST)}" to search specific data and list length.

    variable "user_names" {
      type        = "list"
      default     = ["oracle", "neo"]
    }
    
    resource "oci_identity_user" "my_users_2" {
      count          = "${length(var.user_names)}"
    
      compartment_id = "${var.tenancy_ocid}"
      description    = "${element(var.user_names, count.index)}"
      name           = "${element(var.user_names, count.index)}"
    }
    
  • Example of the entire contents of users.tf

    • For provider.tf and terraform.tfvars, the previous practice files are used as they are.
    variable "user_names" {
      type        = list
      default     = ["oracle", "neo"]
    }
    
    ### Create Users
    resource "oci_identity_user" "my_users_2" {
      count          = "${length(var.user_names)}"
    
      compartment_id = "${var.tenancy_ocid}"
      description    = "${element(var.user_names, count.index)}"
      name           = "${element(var.user_names, count.index)}"
    }
    
    ### Set User Passwords
    ### This is one time password.
    resource "oci_identity_ui_password" "my_users_2_password" {
      count   = "${length(var.user_names)}"
      user_id = "${oci_identity_user.my_users_2.*.id[count.index]}"
    }
    
    ### Outputs
    output "my_users_2_password" {
      sensitive = false
      value     = "${concat(oci_identity_user.my_users_2.*.name, oci_identity_ui_password.my_users_2_password.*.password)}"
    }
    
  • Execution result You can see the output and OCI users created by their names in the list.

    [opc@bastion-host example_user_list]$ terraform apply
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      + create
    
    ...
    
    Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    my_users_2_password = [
      "oracle",
      "neo",
      "W$tC!bS6k&h;mH8R#}9u",
      "(I;hZJ-uZq+.LkXGl5#Z",
    ]
    [opc@bastion-host example_user_list]$
    

    image-20220119152115623



As an individual, this article was written with my personal time. There may be errors in the content of the article, and the opinions in the article are personal opinions.

Last updated on 1 Apr 2019