{"record":{"id":"a6ce8c9ac85b4566","repo":"hashicorp/packer","slug":"required-variable-not-set-s","errorCode":null,"errorMessage":"required variable not set: %s","messagePattern":"required variable not set: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packer/core.go","lineNumber":887,"sourceCode":"\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\n\t\t\t\t\"min_version is invalid: %s\", err)\n\t\t}\n\n\t\tif versionActual.LessThan(versionMin) {\n\t\t\treturn fmt.Errorf(\n\t\t\t\t\"This template requires Packer version %s or higher; using %s\",\n\t\t\t\tversionMin,\n\t\t\t\tversionActual)\n\t\t}\n\t}\n\n\t// Validate variables are set\n\tvar err error\n\tfor n, v := range c.Template.Variables {\n\t\tif v.Required {\n\t\t\tif _, ok := c.variables[n]; !ok {\n\t\t\t\terr = multierror.Append(err, fmt.Errorf(\n\t\t\t\t\t\"required variable not set: %s\", n))\n\t\t\t}\n\t\t}\n\t}\n\n\t// TODO: validate all builders exist\n\t// TODO: ^^ provisioner\n\t// TODO: ^^ post-processor\n\n\treturn err\n}\n\nfunc isDoneInterpolating(v string) (bool, error) {\n\t// Check for whether the var contains any more references to `user`, wrapped\n\t// in interpolation syntax.\n\tfilter := `{{\\s*user\\s*\\x60.*\\x60\\s*}}`\n\tmatched, err := regexp.MatchString(filter, v)\n\tif err != nil {","sourceCodeStart":869,"sourceCodeEnd":905,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/packer/core.go#L869-L905","documentation":"Packer variables declared with `required = true` (JSON templates) must be supplied by the caller via `-var`, `-var-file`, or environment-injected values. During `Core.validate()` (called from `initialize`), every template variable flagged Required is looked up in the `c.variables` map populated from the CLI/var-files; any missing key is appended to a multierror with this message. The build never starts until all required variables are provided.","triggerScenarios":"A JSON template contains `{\"variables\": {\"ami_name_prefix\": {\"required\": true}}}` (or the SDK marks a variable required) and `packer build`/`validate` runs without that key present in `c.variables` (no `-var 'key=...'`, no `-var-file`, no default), so the `_, ok := c.variables[n]` lookup fails for each missing key.","commonSituations":"Forgetting `-var` on the command line; a var-file path typo or empty var-file; relying on an env var that isn't actually exported in CI; running `packer validate` without the variables you normally pass to `packer build`; shell var expansion (`${VAR}`) evaluating to empty before reaching Packer.","solutions":["Pass the missing variable explicitly: `packer build -var 'name=value' template.pkr.hcl` (the error lists each missing name).","Create a var-file with the required values and pass `-var-file=vars.pkrvars.hcl` (or place `auto/*.auto.pkrvars.hcl` for auto-loading).","If the variable should be optional, give it a `default` in the template instead of `required: true`.","Check that CI exports the expected environment variables or that your `-var \"key=${ENV}\"` shell expansion is non-empty."],"exampleFix":"// before\n$ packer build template.json\n// required variable not set: aws_region\n// after\n$ packer build -var 'aws_region=us-east-1' -var-file=prodvars.json template.json","handlingStrategy":"validation","validationCode":"// check required vars before invoking packer\nrequired := []string{\"aws_region\", \"ami_name_prefix\"}\nfor _, r := range required {\n    if os.Getenv(r) == \"\" {\n        panic(\"missing required variable: \" + r)\n    }\n}","typeGuard":null,"tryCatchPattern":"// Go: inspect the multierror for missing-variable entries\nif err := core.Initialize(); err != nil {\n    if merr, ok := err.(*multierror.Error); ok {\n        for _, e := range merr.Errors {\n            if strings.HasPrefix(e.Error(), \"required variable not set:\") {\n                log.Printf(\"supply -var for: %s\", e)\n            }\n        }\n    }\n    return err\n}","preventionTips":["Always invoke builds through a wrapper script that injects `-var-file` for the target environment.","Give non-critical variables defaults so only truly mandatory ones are `required: true`.","Run `packer validate` with the same var-files as `packer build` in CI to catch missing vars before a build.","Avoid `-var \"k=${ENV}\"` when the env var may be unset; check it in shell first (`${ENV:?unset}`)."],"tags":["packer","variables","template-validation"],"backgroundTag":"required-variable-not-set","analyzedSha":"eb36e3c3e48a036f3e8cc94087636ee72e1303c9","analyzedAt":"2026-09-05T13:20:43.127Z","contentChangedAt":"2026-09-05T13:20:43.127Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}