Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

templatestring Function Crashes When Map Contains a Key Mapped to null #36652

Open
aclarke822 opened this issue Mar 7, 2025 · 1 comment
Open
Assignees
Labels
bug confirmed a Terraform Core team member has reproduced this issue hcl Use in conjunction with "upstream" when HCL is the relevant upstream new new issue not yet triaged upstream

Comments

@aclarke822
Copy link

aclarke822 commented Mar 7, 2025

Terraform Version

Terraform v1.11.1
on windows_amd64

Terraform Configuration Files

locals {
  map = {
    test = null
  }
  template = "$${test}"

  test = templatestring(local.template, local.map)
}

output "test" {
  value = local.test
}

Debug Output

https://gist.github.com/aclarke822/db92ba7a7a8a95851a9911db75dc7191

Expected Behavior

Terraform should handle this scenario gracefully, without crashing and output a relevant error message.

Actual Behavior

Terraform crashes.

Steps to Reproduce

  1. terraform apply

Additional Context

Also tested on 1.9.8 and 1.10.5 with the same result

References

No response

Generative AI / LLM assisted development?

No response

@aclarke822 aclarke822 added bug new new issue not yet triaged labels Mar 7, 2025
@liamcervante liamcervante added upstream confirmed a Terraform Core team member has reproduced this issue hcl Use in conjunction with "upstream" when HCL is the relevant upstream labels Mar 7, 2025
@liamcervante
Copy link
Member

Hi @aclarke822, thanks for filing this. I've marked this as confirmed as I can replicate it.

Interestingly enough it does return an error if you the template isn't just a single reference: template = "this: $${test}".

╷
│ Error: Error in function call
│ 
│   on main.tf line 7, in locals:
│    7:   test = templatestring(local.template, local.map)
│     ├────────────────
│     │ while calling templatestring(template, vars)
│     │ local.map is object with 1 attribute "test"
│     │ local.template is "this: ${test}"
│ 
│ Call to function "templatestring" failed: <templatestring argument>:1,9-13: Invalid template interpolation value; The expression result is null. Cannot include a null
│ value in a string template..
╵

The implementation of this function is actually directly within HCL, so I've tagged this as an upstream issue and we'll have to fix this within HCL. I think that HCL is trying to be clever in the way it interprets this, and realises it doesn't need to do any string interpolation with the original template so it appears to incorrectly skip the necessary validation. We need to make it perform the same validation even when it isn't actually performing any real templating.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug confirmed a Terraform Core team member has reproduced this issue hcl Use in conjunction with "upstream" when HCL is the relevant upstream new new issue not yet triaged upstream
Projects
None yet
Development

No branches or pull requests

3 participants