I tried the "Building HCL from scratch" example seen in README:
https://github.com/amplify-education/python-hcl2#usage
Python 3.13.13 (main, Apr 8 2026, 09:49:30) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hcl2
...
... doc = hcl2.Builder()
... res = doc.block("resource", labels=["aws_instance", "web"], ami="abc-123", instance_type="t2.micro")
... res.block("tags", Name="HelloWorld")
...
... hcl_string = hcl2.dumps(doc.build())
... print(hcl_string)
...
resource aws_instance web {
ami = abc-123
instance_type = t2.micro
tags {
Name = HelloWorld
}
}
This result doesn't feel right - I'd expect the terraform to look like this:
resource "aws_instance" "web" {
ami = "abc-123"
instance_type = "t2.micro"
tags = {
Name = "HelloWorld"
}
}
i.e. the strings get quoted where needed and tags is not a block.
Ideally, the example would, however, include both strings and identifiers. This would be of huge help in understanding how to use this library. To expand the existing example, the ami could be the thing that uses an identifier referencing a data source (I don't think it's crucial to include the data source itself in the example):
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = "t2.micro"
tags = {
Name = "HelloWorld"
}
}
Specifically, as a user, I have no clue how to tell this library to safely quote a string - wrapping the string in " without some additional work to ensure everything is properly escaped. Perhaps it's worth showing an example of HCL interpolation as well? I do think it would be helpful to both show how to get anything that may get treated as interpolation inside a string escaped and how not to get it escaped. I'm not sure that the library currently has any APIs that actually allow for this, though.
I tried the "Building HCL from scratch" example seen in README:
https://github.com/amplify-education/python-hcl2#usage
This result doesn't feel right - I'd expect the terraform to look like this:
i.e. the strings get quoted where needed and
tagsis not a block.Ideally, the example would, however, include both strings and identifiers. This would be of huge help in understanding how to use this library. To expand the existing example, the
amicould be the thing that uses an identifier referencing a data source (I don't think it's crucial to include the data source itself in the example):Specifically, as a user, I have no clue how to tell this library to safely quote a string - wrapping the string in
"without some additional work to ensure everything is properly escaped. Perhaps it's worth showing an example of HCL interpolation as well? I do think it would be helpful to both show how to get anything that may get treated as interpolation inside a string escaped and how not to get it escaped. I'm not sure that the library currently has any APIs that actually allow for this, though.