Validate HCL Block Structure
Validate Terraform HCL code for structural correctness including matched braces, brackets, parentheses, and proper block nesting without running terraform validate.
Detailed Explanation
Structural HCL Validation
Before running terraform validate (which requires provider plugins and initialization), you can catch many common errors with structural validation. The formatter checks for balanced delimiters and proper nesting.
Common Structural Errors
Unclosed brace:
resource "aws_instance" "web" {
ami = "ami-12345678"
instance_type = "t3.micro"
tags = {
Name = "web"
# Missing closing brace for tags
}
Mismatched brackets:
variable "allowed_cidrs" {
type = list(string)
default = [
"10.0.0.0/8",
"172.16.0.0/12",
# Missing closing bracket
}
Unclosed parenthesis:
locals {
subnet = cidrsubnet(var.vpc_cidr, 8, 0
}
What the Validator Checks
| Check | Description |
|---|---|
Matched {} |
Every opening brace has a closing brace |
Matched [] |
Every opening bracket has a closing bracket |
Matched () |
Every opening parenthesis has a closing parenthesis |
| Nesting order | Closing delimiters match the most recent opening delimiter |
| String boundaries | Unclosed string literals are detected |
What It Does NOT Check
- Provider-specific attribute requirements
- Resource argument types and constraints
- Variable type compatibility
- Module source availability
- State file consistency
For full semantic validation, use terraform init followed by terraform validate. The structural check here is a fast first pass that catches syntax errors without needing provider plugins.
Use Case
Quick syntax checking of Terraform files before committing to version control, catching structural errors early without needing a full terraform init and validate cycle.