Minify Terraform HCL Code
Minify Terraform HCL code by removing comments, blank lines, and unnecessary whitespace. Useful for embedding HCL in scripts or reducing file transfer size.
Minification
Detailed Explanation
Minifying HCL Code
While Terraform itself does not benefit from minified code, there are scenarios where compact HCL is useful: embedding in CI/CD pipelines, storing in configuration management databases, or reducing payload size for API-based Terraform Cloud operations.
Before Minification
# VPC Configuration
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
# Standard tags for all resources
tags = {
Name = "main-vpc"
Environment = "production"
ManagedBy = "terraform"
}
}
# Public subnet
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
tags = {
Name = "public-subnet"
}
}
After Minification
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "main-vpc"
Environment = "production"
ManagedBy = "terraform"
}
}
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
tags = {
Name = "public-subnet"
}
}
What Minification Removes
| Element | Removed? |
|---|---|
Single-line comments (#, //) |
Yes |
Multi-line comments (/* */) |
Yes |
| Blank lines | Yes |
| Leading/trailing whitespace | Yes |
| Alignment padding | Yes |
| String contents | Preserved |
| Heredoc contents | Preserved |
When NOT to Minify
- Version control: Always store formatted code in git
- Code review: Reviewers need readable code
- Debugging: Terraform error messages reference line numbers
- terraform fmt: The official formatter expects readable code
Use Case
Embedding Terraform snippets in documentation generators, CI/CD pipeline scripts, or configuration management tools where compact representation is preferred.