Skip to content

002 · Domain 3: Infrastructure Lifecycle and Operations · 22% of the exam

Task 3.8: Input and output data from infrastructure projects

Passing data between projects: outputs and terraform_remote_state, data sources as the looser coupling, module inputs and outputs, and count-dependent references.

Study it

  • Data between projects: outputs, remote state and data sources

    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 network configuration must share its VPC and subnet IDs with three consumers: two Terraform configurations and a deployment tool that is not Terraform. None of the consumers may get read access to the network state. Which approach fits?

  1. AExpose root outputs and have every consumer read them through the terraform_remote_state data source
  2. BCommit the IDs to a shared .tfvars file that each consumer copies
  3. CGive each consumer a copy of the state file with sensitive attributes removed
  4. DPublish the IDs with aws_ssm_parameter resources and read them with the matching data source
Show the answer and why
  • AExpose root outputs and have every consumer read them through the terraform_remote_state data source

    Incorrect

    Reading root outputs through remote state requires access to the whole state snapshot, and the non-Terraform tool cannot use the data source.

  • BCommit the IDs to a shared .tfvars file that each consumer copies

    Incorrect

    Hard-coded copies must be updated by hand whenever the network changes, which HashiCorp advises against.

  • CGive each consumer a copy of the state file with sensitive attributes removed

    Incorrect

    Copied state goes stale immediately and still exposes state structure; HashiCorp recommends publishing data explicitly instead.

  • DPublish the IDs with aws_ssm_parameter resources and read them with the matching data source

    Correct

    A separate configuration store lets you apply its own access controls, and other systems besides Terraform can read the same values.

terraform_remote_state is convenient but needs full state access. Pairs of managed resources and data sources, such as SSM parameters, DNS records or config maps, publish only what consumers need.

Question 2 · choose 1

aws_instance.web uses for_each. An output written as value = aws_instance.web[*].id fails. Which value returns every instance's ID?

  1. Aaws_instance.web.*.id
  2. Bvalues(aws_instance.web)[*].id
  3. Caws_instance.web[0].id
  4. D[for k in keys(aws_instance.web) : k.id]
Show the answer and why
  • Aaws_instance.web.*.id

    Incorrect

    The legacy splat has the same limitation with maps.

  • Bvalues(aws_instance.web)[*].id

    Correct

    for_each resources appear as a map of objects; values() turns it into a list that a splat can read.

  • Caws_instance.web[0].id

    Incorrect

    for_each instances use keys, not numeric indexes.

  • D[for k in keys(aws_instance.web) : k.id]

    Incorrect

    keys() returns the map keys, which are strings without an id attribute.

A for expression, such as [for i in aws_instance.web : i.id], works too.

Question 3 · choose 1

A module's subnet_ids output sometimes returns a map after a refactor, breaking callers that expect a list of strings. Which change makes the mistake fail inside the module?

  1. AMark the output sensitive = true
  2. BAdd a description stating the expected type
  3. CConvert the value to a list in each caller
  4. DAdd type = list(string) to the output
Show the answer and why
  • AMark the output sensitive = true

    Incorrect

    Sensitivity hides values; it does not check types.

  • BAdd a description stating the expected type

    Incorrect

    Descriptions document intent but are not enforced.

  • CConvert the value to a list in each caller

    Incorrect

    That moves the problem to callers instead of the module.

  • DAdd type = list(string) to the output

    Correct

    An output's type argument constrains the value it accepts; without one, the output accepts any type.

Output types tell consumers what the module produces and catch mistakes close to their source.

Question 4 · choose 1

A script runs terraform output -raw subnet_ids, where the output is a list, and fails. What should it run instead?

  1. Aterraform output -json subnet_ids
  2. Bterraform output -raw -no-color subnet_ids
  3. Cterraform show -raw subnet_ids
  4. Dterraform state show subnet_ids
Show the answer and why
  • Aterraform output -json subnet_ids

    Correct

    -raw supports only strings, numbers and booleans; -json handles complex types such as lists.

  • Bterraform output -raw -no-color subnet_ids

    Incorrect

    Removing color does not change which types -raw accepts.

  • Cterraform show -raw subnet_ids

    Incorrect

    show has no -raw option.

  • Dterraform state show subnet_ids

    Incorrect

    state show expects a resource address, not an output name.

Pipe the JSON to a parser, such as jq, to pick individual elements.

Question 5 · choose 1

A team wants to warn users who read an old output named vpc. Where can it set deprecated = "Use network_id instead." on that output?

  1. AIn any module, including the root module
  2. BOnly in the root module, for state readers
  3. COnly in child modules, from Terraform v1.15
  4. DOnly in a module's README.md
Show the answer and why
  • AIn any module, including the root module

    Incorrect

    The argument is not allowed in the root module.

  • BOnly in the root module, for state readers

    Incorrect

    It is for child modules and the modules that call them.

  • COnly in child modules, from Terraform v1.15

    Correct

    Deprecated outputs are available in v1.15 and later and are valid only in child modules.

  • DOnly in a module's README.md

    Incorrect

    The argument goes in the output block itself.

Deprecate first, then remove the output in a later major version.

Practice domain 3 →Practice all domains →