002 · Domain 1: Scalable Terraform Configuration Design · 20% of the exam
Task 1.2: Predictable and secure Terraform configuration
Configuration that behaves the same on every run: stable instance keys, the style guide's file layout, outputs as the only interface, providers passed into modules instead of configured there, and moved blocks so a refactor never destroys anything.
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
A shared logging module names its bucket "${terraform.workspace}-logs". A root configuration now needs two instances of the module, one for application logs and one for audit logs, in every workspace. Bucket names must stay readable and must be decided by configuration, not chosen at random. What should the module author change?
AAppend count.index to the name and set count = 1 on each module call
BReplace terraform.workspace with uuid() to make every name unique
CReplace terraform.workspace with path.module to give each module call its own name
DAdd a name_prefix input that callers set, such as "audit-${terraform.workspace}"
Show the answer and why
AAppend count.index to the name and set count = 1 on each module call
Incorrect
Each call has its own instance 0, so both calls still build the same name and collide.
BReplace terraform.workspace with uuid() to make every name unique
Incorrect
uuid() returns a new value each time it is called, so the names are neither readable nor stable and every plan shows a spurious diff.
CReplace terraform.workspace with path.module to give each module call its own name
Incorrect
path.module is a filesystem path, not a readable name, and it does not tell two calls of the same local module apart.
DAdd a name_prefix input that callers set, such as "audit-${terraform.workspace}"
Correct
The caller supplies the prefix, so it can call the module more than once and still use the workspace name where it wants it.
Apart from path.module, HashiCorp recommends using the filesystem and workspace values only in the root module. A shared module that needs a unique prefix should take it as an input variable, so the caller decides how names differ between calls.
A module sets the tag created_at = timestamp() on every bucket. Since then, every plan proposes updating all buckets even when nothing else changed. What explains this?
Atimestamp() returns a new value on every run, and the tag never matches the value in state
BThe provider stores tags in another timezone, and each refresh reports a new value
CTags set by any function are unknown until apply, which forces an update on every plan
DTerraform swaps timestamp() for plantimestamp() during plans, and the two never agree
Show the answer and why
Atimestamp() returns a new value on every run, and the tag never matches the value in state
Correct
The value changes every second and is taken at apply, so using it in a resource argument causes a diff on every run.
BThe provider stores tags in another timezone, and each refresh reports a new value
Incorrect
The diff comes from the function's changing value, not from how the provider stores the tag.
CTags set by any function are unknown until apply, which forces an update on every plan
Incorrect
Most functions return known values during planning; timestamp() is special because its value is always new.
DTerraform swaps timestamp() for plantimestamp() during plans, and the two never agree
Incorrect
Terraform does not substitute one function for the other; they are separate functions.
HashiCorp advises against timestamp() in resource arguments. Use it with ignore_changes to keep the creation time, or use the Time provider for a stable timestamp.
A team writes the .gitignore for a new Terraform repository. Which of these belongs in it according to the HashiCorp style guide?
AThe .terraform.lock.hcl dependency lock file
BThe README.md that documents the inputs and outputs of the root module
CThe *.tf configuration files
DSaved plan files created with terraform plan -out
Show the answer and why
AThe .terraform.lock.hcl dependency lock file
Incorrect
The style guide says to always commit the dependency lock file.
BThe README.md that documents the inputs and outputs of the root module
Incorrect
The guide recommends committing a README.md that describes the code.
CThe *.tf configuration files
Incorrect
All Terraform code files belong in version control.
DSaved plan files created with terraform plan -out
Correct
The style guide lists saved plan files among the files you should not commit.
Do not commit state files and their backups, the .terraform directory, state lock info, saved plans or .tfvars files with secrets; do commit code, the lock file and the README.