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

36 lines
1.1 KiB
Plaintext
Raw Normal View History

2020-05-26 09:29:47 -04:00
## A variable value must be known:
Take the following variable for example:
```hcl
variable "foo" {
type = string
2020-10-14 16:35:21 -04:00
}
2020-05-26 09:29:47 -04:00
```
Here `foo` must have a known value but you can default it to `null` to make
this behavior optional :
| | no default | `default = null` | `default = "xy"` |
| :---------------------------: | :--------------------------: | :--------------: | :--------------: |
| foo unused | error, "foo needs to be set" | - | - |
| var.foo | error, "foo needs to be set" | null¹ | xy |
| `PKR_VAR_foo=yz`<br />var.foo | yz | yz | yz |
| `-var foo=yz`<br />var.foo | yz | yz | yz |
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 wont be set.