Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

HCL block schema: what Terraform and Packer accept

10 min read
Improve this page

An HCL block only makes sense to the tool that reads it: resource exists in Terraform and not in Packer, source exists in Packer and not in Terraform. The syntax of the two files is the same, but each tool declares its own schema: the list of allowed blocks, the number of labels each one takes and the arguments it accepts. This lesson compares these two schemas, shows which files each tool reads in a folder, and teaches you to read the rejection messages. The messages and behaviors shown in the output were tested on 2026-11-19 with Terraform 1.17.2 and Packer 1.16.2, in containers without network access.

What you will learn

  • Explain why the same block is accepted by one tool and rejected by the other.
  • Recognize the root blocks of Terraform and Packer.
  • Know which files each tool reads in a folder.
  • Read schema error messages and work out the fix.

Prerequisites

The lesson HCL syntax (in French), for the notions of block, label and attribute, and the lesson Variables and locals (in French), which already compares the two tools on their named values. No knowledge of Terraform or Packer is needed beyond that.

Why can an HCL-valid block still be rejected?

The lesson HCL syntax (in French) laid it out: HCL checks the form, and the tool decides on the meaning with its schema. The same text can therefore be well written and still be rejected.

The clearest case is to give each tool a block that belongs to the other. A source block, normal in Packer, is rejected by Terraform:

source "null" "essai" {}
terraform validate
Error: Unsupported block type
on main.tf line 1:
1: source "null" "essai" {}
Blocks of type "source" are not expected here. Did you mean "resource"?

The reverse gives the same message on the Packer side. A resource block in a .pkr.hcl file is rejected with Blocks of type "resource" are not expected here. Did you mean "source"?. In both cases, the phrase "not expected here" signals a schema rejection, not a syntax error. Each tool even suggests the closest name in its own schema.

Which root blocks does each tool declare?

A root block is a block written at the top level of the file, outside any other block. The two tools share a few types and each has some of its own. The table below groups them by role, which helps when moving from one tool to the other.

RoleTerraformPacker
settings, required versions and pluginsterraform (with required_providers)packer (with required_plugins)
what the tool buildsresource (two labels: type, name)source (two labels: type, name) then build
read existing datadatadata
inputs and computed valuesvariable, localsvariable, locals, local
outputsoutputnone: Packer rejects output (Unsupported block type)
configure a pluginproviderno root block, the settings go in source
reuse a configurationmoduleno equivalent

The data block plays the same role in both tools, but each has its own data types: Packer 1.16.2 knows http and null without a plugin, for example. The singular local block, which declares a single value that can be marked sensitive = true, exists only in Packer. The variable and locals blocks are written identically. Tested: a file that contains only a variable "region" block and a locals block passes terraform validate and packer validate -syntax-only. The full packer validate rejects it nonetheless, with Missing build block: Packer requires at least one build block for a configuration to be complete.

How many labels and which arguments does a block accept?

The schema also fixes the number of labels of each block. A Terraform resource block needs two, the type then the name. With a single label, the message says exactly what is missing:

terraform validate
Error: Missing name for resource
on main.tf line 1, in resource "terraform_data":
1: resource "terraform_data" {
All resource blocks must have 2 labels (type, name).

Finally, the schema fixes the arguments allowed in a block body. An unknown argument is rejected with Unsupported argument. Tested with a couleur argument added to the built-in terraform_data resource:

terraform validate
Error: Unsupported argument
on main.tf line 2, in resource "terraform_data" "x":
2: couleur = "rouge"
An argument named "couleur" is not expected here.

These three messages read the same way: the title says which rule of the schema is violated (block type, labels, argument), and the last line says what is wrong, often with what was expected.

Which files do Terraform and Packer read in a folder?

For its configuration, each tool reads all the files in the folder that carry its extension, and only those. An arbitrary .hcl file is ignored without any warning. That is a trap when you rename a file or copy it from one tool to the other.

ToolConfiguration files readTested
Terraform*.tf and *.tf.jsona faulty extra.hcl is ignored, validate succeeds; a faulty extra.tf.json is read and rejected
Packer*.pkr.hcl and *.pkr.jsona folder that contains only a simple.hcl is rejected

For Packer, the message is explicit when no file matches:

packer validate .
Error: Could not find any config file in .
A config file must be suffixed with `.pkr.hcl` or `.pkr.json`. A folder can be
referenced.

Terraform, for its part, says nothing about an arbitrary .hcl file placed next to the .tf files: it does not read it, and a faulty block inside it will never be reported. Keep this in mind when a change seems to have "no effect": check the file extension first.

Values files follow another rule: each tool loads some of them automatically, based on their name. Tested: Terraform loads terraform.tfvars and *.auto.tfvars, but not a plain x.tfvars; Packer loads *.auto.pkrvars.hcl, but neither a plain x.pkrvars.hcl nor a terraform.tfvars. The lesson Variables and locals (in French) details how to supply these values.

How does each tool reference its own blocks?

A reference points to a block from another place in the configuration, and its form also depends on the tool. In Terraform, a resource is referenced by its type and name, with no prefix: terraform_data.web.id. In Packer, a build block cites its sources with a string that starts with source.: sources = ["source.null.essai"].

Both tools check that a reference points to a declared block. Terraform rejects a missing resource at validate:

terraform validate
Error: Reference to undeclared resource
on main.tf line 2, in output "o":
2: value = terraform_data.absent.id
A managed resource "terraform_data" "absent" has not been declared in the
root module.

Packer does the same check, but only with the full validate. Tested with a source that is cited and never declared: packer validate -syntax-only . answers Syntax-only check passed. Everything looks okay., and packer validate . rejects it with Error: Unknown source null.essai, followed by Known: [], the list of declared sources, empty here. The -syntax-only mode therefore checks the form and the block types, not the references between blocks.

Which command catches which error?

The schema and the references are not checked by the same commands. The table sums up the cases tested in this lesson, and says which command to run to be sure to catch them.

Errorterraform validatepacker validate -syntax-onlypacker validate
unknown block type (source in Terraform, resource in Packer)rejectedrejectedrejected
reference to an undeclared blockrejectedacceptedrejected
Packer configuration without a build blocknot applicableacceptedrejected

To replay these examples, run terraform init -backend=false then terraform validate in a Terraform folder, and packer validate . in a Packer folder. The lesson Validate and lint (in French) details what each command checks.

Where do you learn the complete schema of each tool?

This lesson shows how a schema manifests itself, not the full list of blocks and their arguments. That list lives in each tool's documentation and training. On the Packer side, the lesson Packer HCL2 templates details the packer, source and build blocks and how to split a template into several files. On the Terraform side, the lesson Declare resources (in French) introduces the resource block and its arguments.

Key takeaways

  • HCL checks the form; the tool's schema decides which blocks, labels and arguments are allowed.
  • Unsupported block type, Missing name for resource and Unsupported argument all signal a schema rejection, not a syntax error.
  • resource is a Terraform block, source and build are Packer blocks; variable and locals are written identically in both.
  • For its configuration, Terraform reads *.tf and *.tf.json, Packer reads *.pkr.hcl and *.pkr.json: an arbitrary .hcl file is ignored by Terraform and is not enough for Packer.
  • packer validate -syntax-only does not check the references between blocks: only the full validate catches an unknown source.

Knowledge check

Knowledge check

Check what you have learned with this interactive quiz

6 questions
6 min
70% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer