Skip to content

Latest commit

 

History

History
154 lines (97 loc) · 5.32 KB

File metadata and controls

154 lines (97 loc) · 5.32 KB

Lab: count and for_each

Help for the VSCode editor.

  1. A new configuration directory has been created at /root/terraform-projects/project-shade. Inspect it. How many files will be created by this configuration?
    1. Navigate to the indicated directory in the Explorer pane and open main.tf
    2. Count the local_file resources
  2. Now add a count argument to create 3 instances of this resource.
    1. Add count to the resource

      count = 3
      
    2. Deploy

      cd /root/terraform-projects/project-shade
      terraform init
      terraform plan
      terraform apply
  3. The resource local_file.name is now created as a:

    count on a resource generates mutiple instances of the resource as a list

  4. what is the id for the resource element at index 1?

    Run terraform show or terraform state show local_file.name[1] in the terminal

  5. How many files were actually created when apply was run?

    Since we did not do anything with the filename argument related to the count, terraform ended up writing the same file three times, so the answer is

    1

  6. We have now created a variables.tf file in the same configuration directory. Update the main.tf file to make use of the list type variable defined for the filename argument.
    • Use count to loop through all the elements of this list and do not use hard-coded values.
    • Use the variable called content for the argument called sensitive_content.

    Inspect variables.tf and note that the list variable is called users. This implies that we're going to greate one file per user in this list. The variable has no default value, meaning that eventually the values will come from elsewhere.

    There's a few things we will need to do here.

    1. Set the count argument from the length of the list (which will be known at plan time) using the length() function.
    2. Set the filename argument using the current value of the counter, which is obtained via the index attribute of count. This index must index into the users list.
    3. Set the content argument as directed

    Update the resource accodringly:

    Reveal
    resource "local_file" "name" {
        filename = var.users[count.index]
        sensitive_content = var.content
        count = length(var.users)
    }
    
  7. We have reverted back to the old configuration file and cleaned up the resources created so far. A variable called users now has default values added to it.
    What type of variable is it?

    Inspect variables.tf and check the value of the type attribute.

  8. Can the same elements in this list be used as it is for a set instead?

    Know that a set must have unique values. Are all the values in this list unique?

  9. Let's do the same exercise as before but this time we will make use of the for_each meta argument to create the files in this configuration.
    Just like before don't use any hard-coded values.
    • Use for_each to loop through the list type variable called users.
    • Use the variable called content as the value of the argument sensitive_content.

    Know that for_each requires a set not a list, therefore we must convert the list to a set using the toset(). This function will also remove any dupicates from the list it is given. Doing it this way satifies the condition that we must not change any hard coded value - just in case you were tempted to edit variables.tf and make that list into a set!

    1. Update resource accordingly

      Reveal
      resource "local_file" "name" {
          for_each = toset(var.users)
          filename = each.value
          sensitive_content = var.content
      }
      
    2. Deploy

      cd /root/terraform-projects/project-shade
      terraform init
      terraform plan
      terraform apply
  10. The resource called name is now created as:

    map

    Why? Because now the resources are keyed by the values that were generated from the list by for_each, rather than by a numeric index as generated by count

  11. The resource address with the filename - /root/user11 is now represented as:

    AS we found out, the resources are now generated as a map keyed by the filenames extracted from the users list, therefore the answer must be

    local_file.name["/root/user11"]