This document covers the intermediate pipeline stages, programmatic document construction with Builder, and the full pipeline diagram. For basic load/dump usage and options, see Getting Started.
The full pipeline looks like this:
Forward: HCL2 Text → Lark Parse Tree → LarkElement Tree → Python Dict
Reverse: Python Dict → LarkElement Tree → HCL2 Text
You can access each stage individually for advanced use cases.
tree = hcl2.parses('x = 1') # StartRule
tree = hcl2.parse(open("main.tf")) # StartRulePass discard_comments=True to strip comments during transformation.
lark_tree = hcl2.parses_to_tree('x = 1') # lark.Treelark_tree = hcl2.parses_to_tree('x = 1')
tree = hcl2.transform(lark_tree) # StartRuletree = hcl2.parses('x = 1')
data = hcl2.serialize(tree)
# or with options:
from hcl2 import SerializationOptions
data = hcl2.serialize(tree, serialization_options=SerializationOptions(with_meta=True))tree = hcl2.from_dict(data) # StartRule
tree = hcl2.from_json('{"x": 1}') # StartRuleBoth accept optional deserializer_options, formatter_options, and apply_format (default True).
tree = hcl2.from_dict(data)
text = hcl2.reconstruct(tree)The Builder class produces dicts with the correct __is_block__ markers so that dumps can distinguish blocks from plain objects:
import hcl2
doc = hcl2.Builder()
res = doc.block("resource", labels=["aws_instance", "web"],
ami="abc-123", instance_type="t2.micro")
res.block("tags", Name="HelloWorld")
hcl_string = hcl2.dumps(doc.build())Output:
resource "aws_instance" "web" {
ami = "abc-123"
instance_type = "t2.micro"
tags {
Name = "HelloWorld"
}
}block(
block_type: str,
labels: Optional[List[str]] = None,
__nested_builder__: Optional[Builder] = None,
**attributes,
) -> BuilderReturns the child Builder for the new block, allowing chained calls.
Forward Pipeline
================
HCL2 Text
│
▼
┌──────────────────┐ parse_to_tree / parses_to_tree
│ Lark Parse Tree │
└────────┬─────────┘
│ transform
▼
┌──────────────────┐
│ LarkElement Tree │ parse / parses (shortcut: HCL2 text → here)
└────────┬─────────┘
│ serialize
▼
┌──────────────────┐
│ Python Dict │ load / loads (shortcut: HCL2 text → here)
└──────────────────┘
Reverse Pipeline
================
Python Dict / JSON
│
▼
┌──────────────────┐ from_dict / from_json
│ LarkElement Tree │
└────────┬─────────┘
│ reconstruct
▼
┌──────────────────┐
│ HCL2 Text │ dump / dumps (shortcut: Python Dict / JSON → here)
└──────────────────┘
- Getting Started — basic
load/dumpusage, options reference - Querying HCL (Python) — typed view facades and tree walking
- hq Reference — query HCL files from the command line