
Declarative nature
Terraform (and HCL) is declarative: you describe the desired state of your infrastructure, not the imperative steps to reach it. Think of it like ordering at a restaurant — you state the final dish you want, and the chef handles the preparation. HCL’s readability makes configurations easier to understand, review, and maintain, improving collaboration across teams.HCL structure overview
HCL code is composed of blocks. Blocks group related configuration and follow a predictable pattern:- A block type (keyword) — e.g.,
resource,data,provider. - Zero or more labels — e.g., resource type and name:
"aws_vpc" "vpc". - A body in curly braces
{ ... }containing attributes (arguments) or nested blocks.
- Single-line:
# commentor// comment - Multi-line:
/* multi-line comment */
Example (main.tf)
Terraform files use the.tf extension (for example, main.tf). The example below shows data blocks (read-only lookups) and a resource block (declares infrastructure to create/manage).
datablocks perform lookups of existing information (no additional arguments required in this example).- The
resourceblock defines an AWS VPC with thecidr_blockattribute and atagsmap. - Inline comments (prefixed with
#) document intent — recommended but optional.
Block anatomy (detailed)
Breakdown of theresource block:
resource— block type: declares infrastructure managed by Terraform."aws_vpc"— resource type: specific provider resource."vpc"— resource name: a local identifier used to reference this resource in the configuration (e.g.,aws_vpc.vpc).{ ... }— block body that contains attributes and nested blocks.- Inside the block are attributes (arguments) such as
cidr_blockand thetagsmap.
aws_vpc.vpc. If you create multiple VPCs, give each resource a unique name (for example, aws_vpc.prod and aws_vpc.test).
Style and conventions
Consistent formatting and naming make HCL easier to read and collaborate on. Below are common conventions used across the Terraform ecosystem:
Example alignment style inside a block:
Use
terraform fmt or your editor extension to automatically format HCL to standard style.Best practices and tips
- Keep variables and outputs well-documented with
descriptionfields. - Group related resources in modules for reuse and separation of concerns.
- Avoid committing sensitive secrets directly — use secure variable management or secret backends.
- Prefer small, focused modules and clear naming to make state management and drift detection easier.
- Use
terraform validateandterraform planto catch errors before applying changes.
Summary
HCL is a readable, declarative language tailored for Terraform and infrastructure-as-code:- Describe the desired state; Terraform determines the steps.
- Organize configurations into blocks with types, labels, and bodies.
- Follow conventions: comments,
snake_casenaming, two-space indentation, and aligned equals for clarity. - Use
terraform fmt,terraform validate, and editor tooling to maintain consistency.
Links and references
- HashiCorp Terraform docs: https://developer.hashicorp.com/terraform
- HCL specification: https://github.com/hashicorp/hcl
- Visual Studio Code: https://code.visualstudio.com/
- HashiCorp Terraform extension for VS Code: https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform