Variables not being automatically loaded in a *.auto.pkrvars.hcl file

While following the steps in this guide , the ami_prefix variable loads fine if I use the --var-file flag, but once I change the file name from [NAME].pkrvars.hcl to [NAME].auto.pkrvars.hcl , and remove the flag, the command stops working (aka only uses the default value)

myTest.pkr.hcl

[...]

variable "ami_prefix" {
  type    = string
  default = "my-packer-test"
}

[...]

variables.auto.pkrvars.hcl

ami_prefix = "my-packer-test-var-from-file-auto"

Commands tried:

packer build --var-file variables.auto.pkrvars.hcl myTest.pkr.hcl → results in the correct result
packer build myTest.pkr.hcl → results in the incorrect result (in this case, it reads the default value, and not the file)

Can anyone provide insight into why the auto isn’t working as expected? Am looking to learn, so don’t hesitate to comment. Thanks for your time

Hi @diogoabreuribeiro,

For Packer to automatically load the contents of your .auto.pkrvars.hcl file, you need to invoke it on the contents of the directory, not on the template itself.

So for example in your case (assuming only this build is visible), you can replace your command with packer build . in the directory where your template and auto.pkrvars are located, and it should then process the var file automatically.

I’m a bit annoyed at how simple this is. Rookie mistake :stuck_out_tongue_winking_eye: Thank you for your quick answer @lbajolet-hashicorp :raised_hands: