Skip to content

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?

  1. AA resource block for the image, so Terraform creates a fresh image every month
  2. BA variable that the image team updates in the configuration's default value every month
  3. CA data block that looks up the newest image matching the name pattern
  4. 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.

Question 2 · choose 1

A configuration has data "aws_vpc" "shared" { ... }. How does a resource refer to its id attribute?

  1. Aaws_vpc.shared.id
  2. Bdata.shared.aws_vpc.id
  3. Cdata.aws_vpc.shared.id
  4. Dvar.aws_vpc.shared.id
Show the answer and why
  • Aaws_vpc.shared.id

    Incorrect

    Without the data. prefix, this refers to a managed resource.

  • Bdata.shared.aws_vpc.id

    Incorrect

    The type comes before the name.

  • Cdata.aws_vpc.shared.id

    Correct

    Data sources are referenced as data.<TYPE>.<NAME>.<ATTRIBUTE>.

  • Dvar.aws_vpc.shared.id

    Incorrect

    var. refers to input variables.

Managed resources: <TYPE>.<NAME>. Data sources: data.<TYPE>.<NAME>.

Question 3 · choose 1

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?

  1. AFive separate data blocks, because data blocks accept neither count nor for_each
  2. BOne data block with for_each = var.subnet_ids, reading each.value as the ID
  3. CA resource block with for_each over the IDs, which adopts the subnets without changing them
  4. 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.

Question 4 · choose 1

Which actions can Terraform take on a managed resource when it applies a configuration?

  1. AOnly create it; later changes need a new resource block
  2. BCreate, update in place, or destroy and re-create it
  3. COnly read it, the same way as a data source
  4. DCreate it, then hand it to the provider, which manages it from then on
Show the answer and why
  • AOnly create it; later changes need a new resource block

    Incorrect

    Terraform updates and replaces existing resources too.

  • BCreate, update in place, or destroy and re-create it

    Correct

    Applying a configuration creates, updates in place, destroys, or destroys and re-creates resources to match.

  • COnly read it, the same way as a data source

    Incorrect

    Reading only is what data sources do.

  • DCreate it, then hand it to the provider, which manages it from then on

    Incorrect

    Terraform keeps managing the resource through its state.

A managed resource has a full lifecycle under Terraform.

Question 5 · choose 1

A data source's arguments are all literal values. When does Terraform normally read it?

  1. ADuring planning
  2. BOnly after apply, once all resources exist
  3. COnly during terraform init
  4. DOnce, after which the result is fixed forever
Show the answer and why
  • ADuring planning

    Correct

    Terraform attempts to query data sources during the planning phase, and literal arguments give it everything it needs to do so.

  • BOnly after apply, once all resources exist

    Incorrect

    Reading moves to apply only in special cases, and literal arguments are not one of them.

  • COnly during terraform init

    Incorrect

    init prepares the working directory and reads no data sources.

  • DOnce, after which the result is fixed forever

    Incorrect

    Data sources are read again on each run.

Data sources are normally read at plan time, so plans can show real values.

Question 6 · choose 1

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?

  1. AIt keeps the first result in the dependency lock file and never recalculates it
  2. BIt creates a managed resource from the result on the first apply
  3. CIt requires an import before the result can be used
  4. 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.

Practise domain 4 →Practise all domains →