Lesson 3 of 3 Resources, Data Sources and References

Resource Addressing and Cross-Resource References

Expressions refer to resources, data sources and other named values, and commands refer to resources by address. This lesson covers both syntaxes and how references create dependencies.

Learning outcomes

After completing this lesson, you will be able to:

  • Interpret references to resources, data sources and other named values.
  • Explain how references create implicit dependencies.
  • Identify resource addresses used by CLI commands and configuration blocks.

Referring to attributes

An expression refers to a resource attribute as TYPE.NAME.ATTRIBUTE, such as aws_vpc.main.id, and to a data source attribute as data.TYPE.NAME.ATTRIBUTE. Nested attributes are reached with dots and indexes, as in aws_instance.web.root_block_device[0].volume_size. When the resource uses count or for_each, the reference selects an instance by index or key.

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "app" {
  for_each   = { a = "10.0.1.0/24", b = "10.0.2.0/24" }
  vpc_id     = aws_vpc.main.id          # implicit dependency on the VPC
  cidr_block = each.value
}

output "subnet_a_id" {
  value = aws_subnet.app["a"].id
}

Each reference does two things: it passes a value and it creates an implicit dependency, so Terraform creates the VPC before the subnets. If the referenced value is computed during creation, the plan shows it as (known after apply).

Other named values

Resources and data sources are only two kinds of named values. The following table lists the others available in expressions.

ExpressionRefers to
var.NAMEAn input variable of the current module
local.NAMEA local value (note the singular local)
module.NAME.OUTPUTAn output of a child module
path.module, path.root, path.cwdThe current module's directory, the root module's directory, the working directory
terraform.workspaceThe name of the current CLI workspace
count.index, each.key, each.valueThe current instance inside a count or for_each block
selfThe current object, inside provisioners, connections and postconditions
Match the pairs

Named values

Exam skills 4.2.2

Match each expression with what it refers to.

Log in to try this activity Interactive activities are free with an account.

Splat expressions

A splat expression extracts one attribute from every element of a list. For a count resource, aws_instance.web[*].id returns a list of all instance IDs, equivalent to [for i in aws_instance.web : i.id]. A resource that uses for_each is a map, not a list, so use values(aws_instance.web)[*].id or a for expression instead. Applied to a single value that is not a list, the splat operator wraps it in a list, or returns an empty list for null.

Scenario questions

Collecting attributes

Exam skills 4.2.2

Collect attributes from resources that create several instances.

Log in to try this activity Interactive activities are free with an account.

Resource addresses in commands

Commands and some configuration blocks identify resources by address rather than by expression. The full form is [module.NAME[KEY].]...TYPE.NAME[KEY]. Examples: aws_instance.web, aws_instance.web[2], module.network.aws_subnet.app["a"] and data.aws_ami.ubuntu.

  • Planning options: -target=ADDRESS and -replace=ADDRESS.
  • State commands: terraform state list, show, mv and rm.
  • Import: terraform import ADDRESS ID and the to argument of import blocks.
  • Refactoring: the from and to arguments of moved and removed blocks.

Quoting addresses

Shells interpret square brackets and double quotes, so quote addresses that contain them: terraform state show 'module.network.aws_subnet.app["a"]'.

Sort into groups

Valid resource address?

Exam skills 4.2.3

Decide whether each string is a valid resource address for commands such as terraform state show.

Log in to try this activity Interactive activities are free with an account.
Worked example

Reading a reference

A configuration declares variable "vpc_cidrs" { type = map(string) } with keys such as us-east-1. Which expression sets cidr_block for us-east-1: var.vpc_cidrs["us-east-1"], var.vpc_cidrs.0, vpc_cidrs["us-east-1"] or var.vpc_cidrs[0]?

The decisive facts are the variable's type and the required prefix.

Maps are indexed by key, and input variables need the var. prefix, so var.vpc_cidrs["us-east-1"] is correct. The two numeric forms index by position, which maps do not support, and the form without var. is not a valid reference. This is one of HashiCorp's own sample questions.

Check yourself

How do you refer to the id of the "web" instance of aws_instance.app, which uses for_each?

Show answer

aws_instance.app["web"].id, selecting the instance by its key.

Exam Tips

Remember for the exam

  • TYPE.NAME.ATTR for resources, data.TYPE.NAME.ATTR for data sources.
  • References pass values and create implicit dependencies.
  • Splat [*] works on lists; for_each resources are maps.
  • Addresses are used by -target, -replace, state commands, import, moved and removed.