!labels
The !labels YAML function reads the current component’s resolved
metadata.labels. Use it without arguments
to return a map of strings, or supply a key to retrieve one string value with an optional fallback.
Usage
Use bare !labels to return the full map:
components:
terraform:
vpc:
metadata:
labels:
cost-center: platform
compliance: sox
vars:
labels: !labels
Arguments
key(optional)- The literal, case-sensitive label key. Without it, the result is the complete label map. Keys containing dots, slashes, or hyphens are read literally; they are not nested paths.
default(optional)- A string returned when the key is absent. Quote values containing spaces, or use
""for an explicit empty fallback. Without a fallback, a missing key produces an error. An existing empty label value is returned unchanged.
The function accepts at most two arguments. Empty keys and malformed arguments are errors.
How It Works
Atmos reads the labels after stack defaults and component inheritance have been merged.
These are the same labels used by atmos list components --labels.
If labels are unset, bare !labels returns an empty map. A key lookup instead uses its
explicit fallback or reports the missing key.
Single label lookup
Here compliance resolves to sox, cost_center to platform, owner to
Platform Team, and optional to an empty string. The runner resolves to
self-hosted-large. See runner routing
for the workflow input and job configuration.
Go template equivalents
Individual labels are also available through the template context:
vars:
compliance: '{{ .metadata.labels.compliance }}'
cost_center: '{{ index .metadata.labels "cost-center" }}'
The path is .metadata.labels, not a top-level .labels shortcut. These examples
require the labels to exist. For an explicit missing-key fallback, use !labels key default.
Bridging metadata.labels into vars.tags
The most common use of !labels is to bridge Atmos's own metadata.labels (used for CLI
categorization and filtering) into the map-shaped vars.tags/vars.labels that
terraform-null-label-style modules expect for actual cloud resource tagging:
vars.tags: !labels isn't a typoTerraform/AWS-style modules call their map-shaped resource-tagging input tags
(var.tags map(string)) — the same "AWS calls a map a tag" naming that Atmos's tags-vs-labels
standard distinguishes: a list is a tag, a map is a label. So !labels — which returns
the map — is what belongs in a module's tags variable. !tags returns Atmos's own []string
list and is for a different shape of consumer entirely.
Examples
Bridging into a Terraform module
No Labels Set
Related Functions
- !tags - Get the current component's own
metadata.tagsas a list - !labels.keys - Get the current component's
metadata.labelskeys - !labels.values - Get the current component's
metadata.labelsvalues