HCL blocks and arguments
Read your first HCL block
HCL (HashiCorp Configuration Language) supplies Terraform’s native configuration syntax. Terraform adds rules about which blocks and arguments are allowed. A block groups related settings; an argument gives a named setting a value. The following small declaration describes a built-in resource, not a real server. Read it on paper for now; the next chapter covers running a configuration safely.
resource "terraform_data" "lesson" { input = "training"}The intended input value is the text training. Reading or saving this declaration does not create anything.
Identify the type, labels and body
In the opening line, resource is the block type. The two quoted labels, terraform_data and lesson, identify the resource type and its local name. The braces contain the body of the block. input is an argument name, = separates the name from its expression, and "training" is a string literal. A literal is a value written directly in the configuration.
Not every block has two labels: a terraform block has none, and an output block has one. The expected labels and body are determined by the kind of block. Labels are not the same as ordinary argument values.
Expressions, identifiers and attributes
An expression describes a value. It can be a literal such as 42, true, or "demo", or a reference or calculation that Terraform evaluates. An identifier is a name in the language; ordinary HCL identifiers can contain letters, digits, underscores and hyphens, but cannot start with a digit. Terraform names are case-sensitive. Prefer short, descriptive lowercase names with underscores for local resource names.
In Terraform discussions, an argument is a value supplied in configuration, while a resource attribute is a named value exposed by the resource, including values calculated by Terraform or its provider. Some attributes can also be set as arguments. Do not assume every readable attribute is configurable.
Example: a collection and a nested block
Here the single input argument contains an object. The ports field is a bracketed sequence, and labels is another object. These are literal shapes; the later type chapter distinguishes lists from tuples and maps from objects. The lifecycle block is separate from the input object. It contains a Terraform-managed lifecycle setting, not application data.
resource "terraform_data" "inventory" { # Synthetic training data only. input = { name = "demo" enabled = true ports = [8080, 8081] labels = { team = "learning" } }
lifecycle { create_before_destroy = true }}The intended input includes the name demo, a boolean flag, two numbers and a team label. The lifecycle setting asks Terraform to create a replacement before destroying the old instance when replacement is necessary; it is not a command that runs immediately, and it is not suitable for every resource. This example is for annotation only, not for deployment.
Comments, spacing and common syntax mistakes
Use # or // for a comment through the end of a line. Use /* ... */ for a block comment. Comments do not supply resource settings. Indent nested contents for readability. Newlines matter to native HCL grammar: do not pack several arguments onto one line as if it were JSON.
A common error is writing input: "training" directly in a resource body; use input = "training" instead. A second error is leaving the closing brace out. A third is writing a valid-looking argument that the resource does not support. Formatting or parsing alone cannot establish that a provider accepts every setting.
Keep strings quoted; an unquoted name in an expression can be interpreted as a reference rather than literal text.
Practice and next step
Annotate the resource example: circle the block type, underline both labels, identify the input expression, and distinguish the nested lifecycle block from the nested object. Explain why changing the source text has not yet changed state. Next, learn which configuration files Terraform reads together. Related reference: HCL blocks and arguments.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HCL blocks and arguments — knowledge check
Check the concepts and decisions introduced in this lesson.
In resource "terraform_data" "lesson", which token is the block type?
Which line is a valid argument in a resource body?
What is the right-hand side of input = [8080, 8081]?
How does lifecycle { ... } differ from input = { ... }?
An argument is syntactically valid but unsupported by the resource. What should you conclude?