Getting Started
Prerequisites
- .NET SDK 6.0 or later
Step 1: Add the Package
Run this command in the project directory:
dotnet add package FsHcl
Step 2: Open the Namespaces
Add these open declarations at the top of the F# file:
open FsHcl.Hcl
open FsHcl.TerraformHcl
FsHcl.Hclprovides the core DSL: values, attributes, blocks, and rendering.FsHcl.TerraformHclprovides Terraform-specific helpers:resource,data,variable,output, and more.
Step 3: Define a Resource
Use the hcl computation expression to define blocks. Each block contains attributes.
let config =
hcl {
resource "aws_s3_bucket" "my_bucket" {
attr "bucket" (str "my-app-data")
attr "tags" (ofRecord {|
Environment = "dev"
ManagedBy = "terraform"
|})
}
}
resource "type" "name" { ... }creates a Terraform resource block.attr "key" valuesets an attribute.str "text"creates a string value.ofRecord {| ... |}converts an F# anonymous record to an HCL object.
Step 4: Render the Output
Pipe the result to document to produce HCL text:
let hclText = config |> document
printfn "%s" hclText
Output:
resource "aws_s3_bucket" "my_bucket" {
bucket = "my-app-data"
tags = {
Environment = "dev"
ManagedBy = "terraform"
}
}
Step 5: Write to a File
Save the output to a .tf file:
open System.IO
File.WriteAllText("main.tf", config |> document)
Terraform reads this file directly. A build step is not required.
Next Steps
- Values — All available value types
- Syntax — Blocks, attributes, and render options
- Terraform Helpers — Terraform-specific block builders
- Patterns — Project-specific helper functions