toowoxx / terraform-provider-packer

Terraform Provider for HashiCorp Packer
Mozilla Public License 2.0
32 stars 7 forks source link

Cannot use complex input variable types (lists) #7

Closed karelorigin closed 3 weeks ago

karelorigin commented 2 years ago
# image.pkr.hcl
variable "foo" {
  type = list(string)
}
# main.tf
resource "packer_image" "image" {
  file             = data.packer_files.image.file
  force            = false
  keep_environment = true

  variables = {
    foo = ["bar"]
  }
}

results in

Inappropriate value for attribute "variables": element "foo": string required.
simaotwx commented 2 years ago

Unfortunately there is currently no support for complex data types – only strings. You could convert it to a JSON in Terraform using jsonencode and decode it in Packer using jsondecode.

I might implement this in the future, though.

karelorigin commented 2 years ago

Ah, I see. I figured it was due to some kind of type conversion limitation. The jsonencode/decode solution should do the trick for now :)

simaotwx commented 2 years ago

Just for the record: Currently variables are passed to Packer using the -var command line parameter. I'm considering changing this and implement it the following way:

  1. A temporary JSON file is created in a temporary location (e. g. /tmp or %USERPROFILE%/AppData/Local/Tmp) with all variables
  2. Packer is passed the -var-file parameter to use that JSON file
  3. When done, the temporary file is deleted

This has multiple advantages:

karelorigin commented 2 years ago

Hi @simaotwx,

Thanks for the update. I always thought this provider simply imported Packer as a library rather than calling it externally. Is that not possible? And if so, wouldn't that make temporary files unnecessary?

simaotwx commented 2 years ago

Hi @karelorigin it does import Packer, however currently it only invokes the Main function, passing the command line parameters as an array to said Main function. To isolate the address space (heap and stack) as well as the environment, the provider is invoked as a program in "Packer mode", effectively acting as if it was the Packer binary itself. That was the easiest and most straightforward way to embed Packer without many modifications, however, I'd be happy to explore better options.

karelorigin commented 2 years ago

Personally, I'm relying on Terraform Cloud to manage TF state and it doesn't seem very fond of files on disk (even in the tmp directory). I've even created my own provider to work around it, so I think anything relying on too many local sources is likely to break.

simaotwx commented 2 years ago

That's a good point. We use Terraform Cloud, too, and I don't know how /tmp behaves there. Complex data types can also be passed on the command line but I fear that it would cause issues related to the command line length and encoding.

nwmcsween commented 1 year ago

Could this not be implemented using the https://github.com/hashicorp/terraform-plugin-sdk/issues/248 DynamicPseudoType and then just quote the var passed to packer e.g. -var 'list=["foo". "bar"]'

simaotwx commented 8 months ago

Will attempt to implement this using DynamicPseudoType in the next round of updates.

simaotwx commented 1 month ago

I have implemented partial support for this in the 0.16.0 release. Please let me know if it's enough to support your use case.

simaotwx commented 3 weeks ago

Version 0.16.0 supports string lists as this issue asks for. I'm going to close this issue for that reason.