Working with HCL

Encode and decode to and from HashiCorp Configuration Language (HCL)arrow-up-right.

HCL is commonly used in HashiCorp tools like Terraform for configuration files. The yq HCL encoder and decoder support:

  • Blocks and attributes

  • String interpolation and expressions (preserved without quotes)

  • Comments (leading, head, and line comments)

  • Nested structures (maps and lists)

  • Syntax colorization when enabled

Parse HCL

Given a sample.hcl file of:

io_mode = "async"

then

yq -oy sample.hcl

will output

io_mode: "async"

Roundtrip: Sample Doc

Given a sample.hcl file of:

then

will output

Roundtrip: With an update

Given a sample.hcl file of:

then

will output

Parse HCL: Sample Doc

Given a sample.hcl file of:

then

will output

Parse HCL: with comments

Given a sample.hcl file of:

then

will output

Roundtrip: with comments

Given a sample.hcl file of:

then

will output

Roundtrip: With templates, functions and arithmetic

Given a sample.hcl file of:

then

will output

Roundtrip: Separate blocks with same name.

Given a sample.hcl file of:

then

will output

Last updated