gruntwork-io / terragrunt

Terragrunt is a thin wrapper for Terraform that provides extra tools for working with multiple Terraform modules.

Home Page:https://terragrunt.gruntwork.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Variables with type = any are not encoded properly for Terraform 0.12

antonbabenko opened this issue · comments

# terragrunt.hcl
terraform {
  source = "..."
}

inputs = {
  name ="something"                 # <-- this does not work
  # name = jsonencode("something")  # <-- this works
}
# module/main.tf
variable "name" {
  type    = any      # Works as expected when type is not `any`
  default = null
}

The error message:

Error: Invalid expression

  on <value for var.name> line 1:
  (source code not available)

Expected the start of an expression, but found an invalid expression token.

It looks like terragrunt should do JSON encode for all variables, but I am not sure if it is a BC break, or not.

Using terragrunt version v0.23.23 and Terraform 0.12.26.

The workaround, for now, is to know which variables have "type = any" and wrap value with jsonencode().

Investigating into this, it looks like terraform does not like the string value being passed in without quotes when type=any. However, this is problematic because we can't pass in with quotes when type=string, as the quotes make it to the final input value (we deliberately do not do a json encoding for strings for this reason).

I'm not sure there is anything we can do here other than to parse the terraform source for variables and dynamically adjust the conversion logic based on the type, but I am not sure we want to go that far. I am inclined to close this as something we won't address in terragrunt, and unfortunately have to rely on the jsonencode workaround.

Could use a second opinion though: @brikis98 what do you think?

@yorinasub17 Agreed. Should we also open a bug in the Terraform repo? The fact that you wrap strings with quotes sometimes in Terraform seems like an issue that would be best fixed on their end.

What's the status on this, guys?

Cost me 2 days. 😔 thanks a lot for the workaround.

Thanks for the workaround and sorry for this "useless" comment, but since GitHub allows searching for commented issues but not subscribed issues I have to comment to easily find this issue later.