liveBook · Manning

Label

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

A label is a name attached to a Terraform block that identifies the block or specifies its subtype.

A label names a Terraform block or specifies its subtype. It appears after the block type and helps Terraform interpret, identify, and reference the block. section 2.2 section 2.2.2

Terraform block types use labels in several ways:

  • No labels: terraform and locals blocks do not need labels because each module has only one of each. section 2.2.2
  • One label: variable, provider, output, and module blocks use a single label; provider labels map to required providers. section 2.2.2
  • Two labels: data and resource blocks use a subtype followed by a unique identifier. section 2.2.2

Referencing a labeled resource

In `resource "aws_instance" "hello_world"`, `aws_instance` is the resource subtype and `hello_world` is its identifier. The resulting reference, `resource.aws_instance.hello_world`, lets other blocks use its attributes. Likewise, `data "aws_vpc" "default"` becomes `data.aws_vpc.default`. section 2.2.2 section 2.2

Listing 2.7 HCL block components
type "label" {                            #1
  parameter1 = "value"                    #2
  parameter2 = "value"                    #3
  parameter3 = true                       #4
  parameter4 = 12                         #5
  parameter5 = {                          #6
    key = "value"
  }
 
  parameter6 = null                       #7
 
  subblock {                              #8
    sub_parameter1 = "value"
    sub_parameter2 = "value"
    sub_parameter3 = "value"
  }
 
  subblock {                              #9
    sub_parameter1 = "value"
    sub_parameter2 = "value"
    sub_parameter3 = "value"
  }
}

Use the annotated HCL listing to locate the block type, label, arguments, and subblocks in a complete block structure. section 2.2

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Distinguishes unlabeled, single-label, and subtype-plus-label blocks, but stops short of broader label strategies.
Examples
Uses terraform, provider, resource, data, AWS instance, AWS VPC, and HCL block examples.
What you can do after
Apply labels to construct Terraform references such as resource.aws_instance.hello_world and data.aws_vpc.default.
How it connects
Connects labels with blocks, subtypes, attributes, data sources, resources, providers, modules, and references.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.