Syntax

All examples assume the following open declaration:

open FsHcl.Hcl

Blocks

Use block to create a generic HCL block. Pass the block type, a list of labels, and a body.

hcl {
    block "locals" [] {
        attr "region" (str "us-east-1")
    }

    block "resource" [ "aws_s3_bucket"; "example" ] {
        attr "bucket" (str "my-bucket")
    }
}
|> document
// locals {
//   region = "us-east-1"
// }
// resource "aws_s3_bucket" "example" {
//   bucket = "my-bucket"
// }

Attributes

attr

attr sets a key-value pair inside a block.

attr "name" (str "example")

optAttr

optAttr sets an attribute only when the value is Some. FsHcl omits the attribute when the value is None.

optAttr "description" (Some (str "A resource"))  // included
optAttr "deprecated" None                         // omitted

Object Assignments

object_ creates a key-value assignment where the value is an object block.

object_ "variables" {
    attr "region" (str "us-east-1")
    attr "env" (str "prod")
}
// variables = {
//   region = "us-east-1"
//   env    = "prod"
// }

List Assignments

list_ creates a key-value assignment where the value is a list.

list_ "allowed_accounts" [
    str "111111111111"
    str "222222222222"
]
// allowed_accounts = [
//   "111111111111",
//   "222222222222",
// ]

Comments

comment creates a single-line comment. blockComment creates a multi-line comment.

comment "Line comment"
blockComment [ "Multi-line"; "block comment" ]
// # Line comment
// /*
// Multi-line
// block comment
// */

Loops

Use F# for expressions inside the hcl computation expression.

hcl {
    for region in [ "us-east-1"; "eu-west-1" ] do
        attr region (str region)
}
|> document
// us-east-1 = "us-east-1"
// eu-west-1 = "eu-west-1"

Rendering

Render Functions

FunctionDescription
documentRender with default options
Render.nodeRender a single node
Render.joinRender nodes with blank line between
withOptionsRender with custom options

RenderOptions

OptionDefaultDescription
indentSize2Spaces per indent level
alignAttributestruePad attribute keys to equal width
trailingNewlinetrueAppend newline at end of output

Custom Options Example

let options = { indentSize = 4; alignAttributes = false; trailingNewline = false }
hcl { block "locals" [] { attr "x" (number 1) } } |> withOptions options
// locals {
//     x = 1
// }