.tfvars
formae can read Terraform and OpenTofu .tfvars files directly inside a forma, so existing variable files from a Terraform or OpenTofu codebase can drive a formae deployment without conversion or duplication.
Usage
terraform.readTFVars() parses an HCL-format .tfvars file and returns its variables as a Dynamic so values can be accessed with dot notation.
Import
import "@formae/ext/terraform.pkl"
Read a file
local vars = terraform.readTFVars("prod.tfvars")
The path is resolved relative to the forma file's directory. Absolute paths are also supported.
Access values
Given this prod.tfvars:
region = "us-west-2"
instance_count = 3
enable_logging = true
tags = { Name = "web-server", Environment = "production" }
zones = ["us-west-2a", "us-west-2b"]
values come through as:
local vars = terraform.readTFVars("prod.tfvars")
vars.region // "us-west-2"
vars.instance_count // 3
vars.enable_logging // true
vars.tags.Name // "web-server"
vars.zones // List("us-west-2a", "us-west-2b")
Example forma
amends "@formae/forma.pkl"
import "@formae/formae.pkl"
import "@formae/ext/terraform.pkl"
import "@aws/aws.pkl"
import "@aws/sqs/queue.pkl"
local tfvars = terraform.readTFVars("env/prod.tfvars")
forma {
new formae.Stack {
label = "my-stack"
description = "Stack using tfvars"
}
new formae.Target {
label = "aws-target"
config = new aws.Config {
region = tfvars.region
}
}
new queue.Queue {
label = "my-queue"
queueName = "app-\(tfvars.region)-queue"
}
}
The same prod.tfvars can stay shared between Terraform or OpenTofu and formae during a migration, eliminating the need to maintain two sets of inputs.
Supported types
| tfvars type | Pkl type |
|---|---|
"string" |
String |
42 |
Int |
3.14 |
Float |
true / false |
Boolean |
["a", "b"] |
List |
{ key = "val" } |
Dynamic (dot-accessible) |
null |
Null |
Heredoc (<<EOF) |
String |