Terraform: Support multi-line comment within string interpolation

Created on 20 Dec 2018  路  2Comments  路  Source: hashicorp/terraform

Current Terraform Version

    $ terraform --version
    Terraform v0.11.7

Use-cases

I would like to annotate template files with comments.

Attempted Solutions

Currently, the parser does not accept multi-line comments within string interpolations:

$ echo '"${ /* comment */ "" }"' | terraform console
parse error at 1:7: expected expression but found "/"

This can be achieved today by writing annotations within a string which is immediately cleared via the built-in replace function:

{
  "foo": 23,
  ${replace("
     this is a comment
  ", "/.*/", "")}
  "bar": 45
}

But this is very noisy and potentially confusing.

Proposal

For instance,

{
  "foo": 23,
  ${/**
     * this is a comment
     */"" }
  "bar": 45
}

References

Support for comments within templates was requested in gh-8260, but that was rejected based on the assumption that this would require a new language construct. This approach isn't quite as legible as a dedicated "template comment" syntax (particularly in its use of the empty string), but it also allows for template annotation without a new language construct.


In any event, thanks for the great tool!

enhancement

Most helpful comment

I suspect this will already work now that we're using HCL2 rather than HCL1+HIL. The template_file provider is not yet updated (that'll be a separate step from Terraform itself since that provider has its own release rhythm) but it should work for _inline_ templates (that is, interpolations in string literals) in the v0.12.0-alpha4 builds.

I tried your first terraform console example quickly on my latest build from master just to see what would happen:

$ terraform console
> "${ /* comment */ "" }"

>  

Once we release an updated template_file using HCL2 it should work there too.

All 2 comments

I suspect this will already work now that we're using HCL2 rather than HCL1+HIL. The template_file provider is not yet updated (that'll be a separate step from Terraform itself since that provider has its own release rhythm) but it should work for _inline_ templates (that is, interpolations in string literals) in the v0.12.0-alpha4 builds.

I tried your first terraform console example quickly on my latest build from master just to see what would happen:

$ terraform console
> "${ /* comment */ "" }"

>  

Once we release an updated template_file using HCL2 it should work there too.

Great news! Thanks

Was this page helpful?
0 / 5 - 0 ratings