-
in our terraform/vars.tf we have a local section which has constants defined for the the service component value. I was trying to do SERVICE_NAME = read("local:../terraform/vars.tf:service_name") but this doesn't seem to work. # vars.tf in terraform folder
locals {
service_name = "my-service"
}
8000
Has anyone done anything like this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Pkl does not have native support for parsing HCL files, but you have a few options here: Switch from HCL to HCL-JSONFor the files that need to be read by both tf and Pkl, use the JSON format for HCL. The data in your example would look like this: # vars.ts.json
{
"locals": {
"service_name": "my-service"
}
} And reading this in Pkl would look like this: import "pkl:json"
local vars = new json.Parser { useMapping = true }.parse(read("../terraform/vars.tf.json"))
local serviceName = vars["locals"]["service_name"] Render your HCL from PklInstead of going HCL->Pkl, flip it around! Use Pkl as the source of truth like so: locals {
service_name = "my-service"
} And render that to vars.tf.json with This likely requires keeping the generated output up to date using some sort of CI validation. Pretend HCL is PklI imagine your actual HCL is more complicated, but in the case you've shown, the HCL code (minus the comment) is also valid Pkl code! If you really do use just the strict Pkl-compatible subset of HCL you can import your HCL files as if they were Pkl: import "../terraform/vars.tf"
local serviceName = vars.locals.service_name This is pretty prone to breakage if non-Pkl-compatible HCL is added later, so I don't really recommend it, but it's fun to think about! Implement an External Reader to parse HCL from PklExternal readers allow Pkl code to call into external "native" code. Using pkl-go and import "pkl:json"
local hclData = read("../terraform/vars.tf")
local jsonData = read("hcl2json:\(hclData.base64)")
local vars = new json.Parser { useMapping = true }.parse(jsonData)
local serviceName = vars["locals"]["service_name"] Assuming your external reader executable is named pkl eval path/to/file.pkl --external-resource-reader hcl2json=hcl2json If you're using a PklProject file you can also configure the external reader there. |
Beta Was this translation helpful? Give feedback.
Pkl does not have native support for parsing HCL files, but you have a few options here:
Switch from HCL to HCL-JSON
For the files that need to be read by both tf and Pkl, use the JSON format for HCL. The data in your example would look like this:
And reading this in Pkl would look like this:
Render your HCL from Pkl
Instead of going HCL->Pkl, flip it around! Use Pkl as the source of truth like so:
And render that to vars…