hcl2json
hcl2json copied to clipboard
Provider functions are not supported?
Consider
test.tf
variable "lastname" {
type = string
default = "bär"
}
variable "firstname" {
type = string
default = "föo"
}
terraform {
required_providers {
corefunc = {
source = "northwood-labs/corefunc"
version = "1.5.1"
}
}
}
locals {
username = provider::corefunc::str_iterative_replace(
lower("${var.firstname}.${var.lastname}"),
[
{ old = "ä", new = "ae" },
{ old = "ö", new = "oe" },
{ old = "ü", new = "ue" },
{ old = "ß", new = "ss" },
],
)
}
output "out" {
value = local.username
}
parse-hcl.js
const { parse } = require('@cdktf/hcl2json');
const fs = require('fs').promises;
const hclFilePath = 'test.tf';
(async () => {
try {
// Read the HCL content from the file
const hcl = await fs.readFile(hclFilePath, 'utf-8');
// Parse the HCL content
const json = await parse(hclFilePath, hcl);
// Output the parsed JSON
console.log(JSON.stringify(json, null, 2));
} catch (err) {
console.error('Error:', err.message);
}
})();
docker run -it --rm -v $PWD:/tmp -w /tmp --entrypoint bash node:20.17 -c "npm install @cdktf/hcl2json; node parse-hcl.js"
Error: parse config: [test.tf:22,22-23: Missing newline after argument; An argument definition must end with a newline.]
However, a terraform plan works fine. And the usage of provider::corefunc::str_iterative_replace is as describe here.
docker run -it --rm -v $PWD:/tmp -w /tmp hashicorp/terraform:1.10 init
docker run -it --rm -v $PWD:/tmp -w /tmp hashicorp/terraform:1.10 plan
Changes to Outputs:
+ out = "foeo.baer"
You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
I think this should be fixed in v0.6.5