Help for the VSCode editor.
-
A new configuration directory has been created at /root/terraform-projects/project-shade. Inspect it. How many files will be created by this configuration?
- Navigate to the indicated directory in the Explorer pane and open
main.tf
- Count the
local_file
resources
- Navigate to the indicated directory in the Explorer pane and open
-
Now add a count argument to create 3 instances of this resource.
-
Add count to the resource
count = 3
-
Deploy
cd /root/terraform-projects/project-shade terraform init terraform plan terraform apply
-
-
The resource local_file.name is now created as a:
count
on a resource generates mutiple instances of the resource as alist
-
what is the id for the resource element at index 1?
Run
terraform show
orterraform state show local_file.name[1]
in the terminal -
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
-
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 thelist
variable is calledusers
. 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.
- Set the
count
argument from the length of the list (which will be known at plan time) using the length() function. - Set the
filename
argument using the current value of the counter, which is obtained via the index attribute ofcount
. This index must index into theusers
list. - 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) }
- Use
-
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 thetype
attribute. -
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? -
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 calledusers
. - Use the variable called
content
as the value of the argument sensitive_content.
Know that
for_each
requires aset
not alist
, 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 editvariables.tf
and make that list into a set!-
Update resource accordingly
Reveal
resource "local_file" "name" { for_each = toset(var.users) filename = each.value sensitive_content = var.content }
-
Deploy
cd /root/terraform-projects/project-shade terraform init terraform plan terraform apply
- Use
-
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 bycount
-
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"]