HCL Configuration Parser

0.4.5 · active · verified Thu Apr 09

pyhcl is a Python library for parsing HCL (HashiCorp Configuration Language) files and strings into Python dictionaries. It aims to be compatible with HCL versions commonly used by tools like Terraform. The library is currently at version 0.4.5 and is actively maintained with bug fixes and compatibility updates, though releases are irregular.

Warnings

Install

Imports

Quickstart

Parses an HCL string into a Python dictionary. The `hcl.loads()` function handles string input, while `hcl.load()` is used for file-like objects.

import hcl

hcl_string = '''
variable "region" {
  description = "AWS region"
  type        = string
  default     = "us-east-1"
}

resource "aws_instance" "web" {
  ami           = "ami-0abcdef1234567890"
  instance_type = "t2.micro"
  tags = {
    Name = "HelloWorld"
  }
}
'''

parsed_data = hcl.loads(hcl_string)
print(parsed_data.get('resource'))
print(parsed_data.get('variable'))

view raw JSON →