Back to Packer

Must Be Set

website/content/partials/from-1.5/variables/must-be-set.mdx

1.15.31.1 KB
Original Source

A variable value must be known:

Take the following variable for example:

hcl
variable "foo" {
  type = string
}

Here foo must have a known value but you can default it to null to make this behavior optional :

no defaultdefault = nulldefault = "xy"
foo unusederror, "foo needs to be set"--
var.fooerror, "foo needs to be set"null¹xy
PKR_VAR_foo=yz
var.fooyzyzyz
-var foo=yz
var.fooyzyzyz

1: Null is a valid value. Packer will only error when the receiving field needs a value, example:

hcl
variable "example" {
  type = string
  default = null
}

source "example" "foo" {
  arg = var.example
}

In the above case, as long as "arg" is optional for an "example" source, there is no error and arg won’t be set.