004 · Domain 4: Terraform configuration · 13% of the exam
Task 4.1: Use and differentiate resource and data blocks
Managed resources that Terraform creates, updates and destroys versus data sources that only read, when each is evaluated, and the ephemeral resource kind for values that must not persist.
Study it
Resources, data sources and ephemeral resources
Lesson coming
Sample questions
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
A configuration hard-codes a machine image ID, and the image team publishes a new image every month with a known name pattern. The configuration should always use the newest matching image without managing the image itself. What should it use?
AA resource block for the image, so Terraform creates a fresh image every month
BA variable that the image team updates in the configuration's default value every month
CA data block that looks up the newest image matching the name pattern
DAn output that exports the image ID from the image team's console session
Show the answer and why
AA resource block for the image, so Terraform creates a fresh image every month
Incorrect
A resource would make Terraform create and own an image, which the image team already publishes.
BA variable that the image team updates in the configuration's default value every month
Incorrect
That still hard-codes the ID, only in a different place, and needs a person each month.
CA data block that looks up the newest image matching the name pattern
Correct
Data sources read information about existing infrastructure for use in the configuration without managing it.
DAn output that exports the image ID from the image team's console session
Incorrect
Outputs expose values from a configuration; they cannot read another system's console.
Resources are what Terraform manages; data sources are what it only reads.
An application configuration must read five existing subnets, whose IDs arrive in var.subnet_ids, a set of strings, so that it can use each subnet's CIDR block. How can the configuration look them up?
AFive separate data blocks, because data blocks accept neither count nor for_each
BOne data block with for_each = var.subnet_ids, reading each.value as the ID
CA resource block with for_each over the IDs, which adopts the subnets without changing them
DOne data block wrapped in a dynamic block that repeats it once per ID
Show the answer and why
AFive separate data blocks, because data blocks accept neither count nor for_each
Incorrect
Data blocks accept the same repetition meta-arguments as resource blocks, so one block can read many objects.
BOne data block with for_each = var.subnet_ids, reading each.value as the ID
Correct
A data block supports for_each, creating one data instance per element, each of which Terraform reads.
CA resource block with for_each over the IDs, which adopts the subnets without changing them
Incorrect
A resource block declares objects for Terraform to create and manage; it does not read existing subnets by ID.
DOne data block wrapped in a dynamic block that repeats it once per ID
Incorrect
dynamic generates nested blocks inside a block; it cannot repeat a whole data block.
Data blocks take count and for_each just as resource blocks do, so one block can read a whole collection of existing objects.
Some data sources do not look up any infrastructure but compute something, such as rendering a policy document. What does Terraform do with their results on later plans?
AIt keeps the first result in the dependency lock file and never recalculates it
BIt creates a managed resource from the result on the first apply
CIt requires an import before the result can be used
DIt re-calculates the data on each plan
Show the answer and why
AIt keeps the first result in the dependency lock file and never recalculates it
Incorrect
The lock file is for provider versions.
BIt creates a managed resource from the result on the first apply
Incorrect
Data sources never become managed resources.
CIt requires an import before the result can be used
Incorrect
Import is for existing managed objects.
DIt re-calculates the data on each plan
Correct
Specialized utility data sources generate data that exists only during the operation; Terraform re-calculates it on consecutive plans.
Not every data source talks to an API; some just compute values every run.