Reference variable value with a local variable using cidrhost


I’m not sure what I’m doing wrong here. At the top of template.pkr.hcl (main config file that I’m running with packer) I have the following:

locals {
        internal_01_gw = cidrhost(var.internal_01, 1)

In variables.pkr.hcl (inside the same directory) I have this:

variable "internal_01" {
	default = ""
	type = string

I’m running packer as follows:

 packer build -var-file="secrets.pkrvars.hcl" -var-file="values.pkrvars.hcl" template.pkr.hcl

But I keep getting this error:

Error: Unsupported attribute

  on template.pkr.hcl line 2:
  (source code not available)

This object does not have an attribute named "internal_01".

In the documentation related to variables, I see that packer is supposed to read all files ending in pkr.hcl: Input and Local Variables guide | Packer | HashiCorp Developer

That doesn’t seem to be the problem anyway, because, even if I define internal_01 in the same file, I still get the same error, so it literally cannot find the variable and I don’t understand why. Any ideas how I can solve this?

You are calling your file " variables.pkr.hcl"
Yet calling -var-file=“values.pkrvars.hcl”. Could just be a copy paste error.

I would also try the following in the locals file. Might help. Might not. Who knows.

locals { internal_01_gw = cidrhost(${var.internal_01}, 1) }
# Or
locals { internal_01_gw = cidrhost("${var.internal_01}", 1) }