Skip to content

004 · Domain 4: Terraform configuration · 13% of the exam

Task 4.2: Refer to resource attributes and create cross-resource references

Reference syntax for resources, data sources, modules, variables and locals; instances made by count and for_each; splat expressions; and the implicit dependencies that references create.

Study it

  • References, count and for_each instances, splats

    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 2

A configuration contains: resource "aws_instance" "web" { count = 3 # ... } Which two expressions, written in an output block of the same module, are valid references to these instances' IDs? (Choose TWO.)

  1. Aaws_instance.web[0].id
  2. Baws_instance.web.id
  3. Caws_instance.web[count.index].id
  4. Dvalues(aws_instance.web)[0].id
  5. Eaws_instance.web[*].id
Show the answer and why
  • Aaws_instance.web[0].id

    Correct

    With count, each instance is addressed by its zero-based index.

  • Baws_instance.web.id

    Incorrect

    That form fits a resource without count or for_each. A resource with count has no single instance; an index or a splat is required.

  • Caws_instance.web[count.index].id

    Incorrect

    count.index exists only inside a block that sets count itself, such as another counted resource; an output block has no count, so the reference is an error there.

  • Dvalues(aws_instance.web)[0].id

    Incorrect

    values() takes a map or object, which is what a for_each resource produces. A count resource is a list of instances, so the call fails.

  • Eaws_instance.web[*].id

    Correct

    The splat expression returns a list of the id attribute of every instance.

count makes a list of instances: index one with [n], or take them all with [*]. count.index and map functions belong to other contexts.

Question 2 · choose 1

A subnet must be created in the VPC declared as resource "aws_vpc" "main". Which expression gives the VPC's ID?

  1. Aaws_vpc.main.id
  2. Baws_vpc.main
  3. C"aws_vpc.main.id"
  4. Dresource.main.id
Show the answer and why
  • Aaws_vpc.main.id

    Correct

    Managed resource attributes are referenced as <TYPE>.<NAME>.<ATTRIBUTE>.

  • Baws_vpc.main

    Incorrect

    That is the whole resource object, not its id attribute.

  • C"aws_vpc.main.id"

    Incorrect

    Quoting makes it a literal string, not a reference.

  • Dresource.main.id

    Incorrect

    The resource type is part of the reference.

<TYPE>.<NAME>.<ATTRIBUTE> without quotes is a reference.

Question 3 · choose 1

A module declares locals { env_prefix = "prod-eu" }. How is the value referenced?

  1. Alocals.env_prefix
  2. Bvar.env_prefix
  3. Clocal.env_prefix
  4. Dself.env_prefix
Show the answer and why
  • Alocals.env_prefix

    Incorrect

    The block is locals, but references use the singular local.

  • Bvar.env_prefix

    Incorrect

    var. refers to input variables.

  • Clocal.env_prefix

    Correct

    Local values are declared in a locals block and referenced as local.<NAME>.

  • Dself.env_prefix

    Incorrect

    self refers to the current resource in specific blocks.

Declare in locals { }, reference as local.name.

Question 4 · choose 1

A configuration used with CLI workspaces must include the current workspace name in each bucket name. Which named value provides it?

  1. Apath.workspace
  2. Bvar.workspace
  3. Clocal.workspace
  4. Dterraform.workspace
Show the answer and why
  • Apath.workspace

    Incorrect

    path values are file system paths, and there is no path.workspace.

  • Bvar.workspace

    Incorrect

    That would be an input variable the caller must set.

  • Clocal.workspace

    Incorrect

    That is a local value only if the module defines one.

  • Dterraform.workspace

    Correct

    terraform.workspace is the name of the currently selected workspace.

terraform.workspace is built in; no variable is needed.

Question 5 · choose 1

Inside a provisioner block of an instance resource, how does the command refer to that same instance's private IP?

  1. Athis.private_ip
  2. Bself.private_ip
  3. Ceach.value.private_ip
  4. Dcount.private_ip
Show the answer and why
  • Athis.private_ip

    Incorrect

    There is no this object; the keyword is self.

  • Bself.private_ip

    Correct

    In provisioner blocks and postconditions, self refers to the resource the block belongs to.

  • Ceach.value.private_ip

    Incorrect

    each exists only in blocks with for_each.

  • Dcount.private_ip

    Incorrect

    count.index is the only count attribute.

Provisioners cannot reference their own resource by name; self does that.

Question 6 · choose 1

A resource uses for_each = var.users, where var.users is a map from user name to team. Which expressions give the current user's name and team inside the block?

  1. Aeach.key and each.value
  2. Bcount.index and count.value
  3. Cvar.users.key and var.users.value
  4. Dself.key and self.value
Show the answer and why
  • Aeach.key and each.value

    Correct

    In blocks with for_each, each.key is the map key and each.value is the corresponding value.

  • Bcount.index and count.value

    Incorrect

    count objects exist only with count, and count.value does not exist.

  • Cvar.users.key and var.users.value

    Incorrect

    These are not iteration references.

  • Dself.key and self.value

    Incorrect

    self refers to the resource, not to the iteration.

for_each gives each.key and each.value; count gives count.index.

Practise domain 4 →Practise all domains →