{"id":10291,"library":"tfparse","title":"tfparse","description":"tfparse is a Python library providing a fast, robust parser for HCL (HashiCorp Configuration Language) and Terraform files. It leverages a Go extension for its core parsing capabilities, making it efficient for use cases like security scanning (e.g., AquaSecurity defsec). The library is actively maintained with frequent minor releases, often on a weekly or bi-weekly cadence, reflecting ongoing improvements and bug fixes.","status":"active","version":"0.6.19.post1","language":"en","source_language":"en","source_url":"https://github.com/cloud-custodian/tfparse","tags":["terraform","hcl","parser","security","infrastructure-as-code"],"install":[{"cmd":"pip install tfparse","lang":"bash","label":"Install tfparse"}],"dependencies":[],"imports":[{"symbol":"parse","correct":"from tfparse import parse"},{"symbol":"load_from_path","correct":"from tfparse import load_from_path"}],"quickstart":{"code":"from tfparse import parse\n\n# Example Terraform configuration as a string\nterraform_config = \"\"\"\nresource \"aws_s3_bucket\" \"my_bucket\" {\n  bucket = \"my-unique-bucket-name\"\n  acl    = \"private\"\n\n  tags = {\n    Environment = \"Dev\"\n    Project     = \"tfparse-example\"\n  }\n}\n\nvariable \"region\" {\n  description = \"AWS region\"\n  type        = string\n  default     = \"us-east-1\"\n}\n\"\"\"\n\ntry:\n    # Parse the HCL configuration string\n    parsed_data = parse(terraform_config)\n    print(\"Successfully parsed Terraform configuration:\")\n    \n    # Access some parsed data, e.g., the resource block\n    s3_bucket = parsed_data.get(\"resource\", {}).get(\"aws_s3_bucket\", {}).get(\"my_bucket\")\n    if s3_bucket:\n        print(f\"  Resource 'aws_s3_bucket.my_bucket' found. Bucket name: {s3_bucket.get('bucket')}\")\n        print(f\"  Tags: {s3_bucket.get('tags')}\")\n\n    # Access a variable block\n    region_var = parsed_data.get(\"variable\", {}).get(\"region\")\n    if region_var:\n        print(f\"  Variable 'region' default: {region_var.get('default')}\")\n\nexcept Exception as e:\n    print(f\"Error parsing configuration: {e}\")","lang":"python","description":"Demonstrates parsing a Terraform configuration string and accessing structured data from the result. For parsing entire directories, use `tfparse.load_from_path`."},"warnings":[{"fix":"Review the reference-tracking changes (e.g., in the `c7n-left` project, linked in release notes) and adapt your code to the new data structure for references.","message":"Version 0.6.15 introduced a breaking change to internal reference-tracking, specifically to support multiple references within a single block. Code that directly manipulates or relies on the structure of tracked references will need updating.","severity":"breaking","affected_versions":"0.6.15 and later"},{"fix":"Upgrade to `tfparse >= 0.6.18` which includes fixes for handling partially unknown `for_each` blocks. Ensure that values required for static parsing are resolvable or handle potential `None`/`unknown` gracefully in your application logic.","message":"Parsing configurations with dynamic blocks (`for_each`) or values that are not 'wholly known' (e.g., runtime outputs, sensitive data) can lead to unexpected parsing behavior or errors.","severity":"gotcha","affected_versions":"<0.6.18"},{"fix":"Ensure that any HCL string values intended to be JSON are fully resolved and syntactically correct at the time of parsing. Upgrade to `tfparse >= 0.6.19` which contains a fix for these `unresolved JSON strings`.","message":"HCL strings intended to be JSON, but containing unresolved references or malformed content, may result in `tfparse` returning non-valid JSON strings, leading to subsequent JSON parsing failures.","severity":"gotcha","affected_versions":"<0.6.19"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Update your code to align with the new reference tracking behavior introduced in `tfparse` v0.6.15. Consult the `c7n-left` project's changes for guidance on adapting to the new structure.","cause":"Your code is attempting to access a Terraform reference using an outdated path or structure, likely due to the breaking change in how `tfparse` tracks references since v0.6.15.","error":"KeyError: '<some_reference_path>'"},{"fix":"If possible, ensure all variables and expressions in dynamic blocks are resolvable within your parsing context. Consider upgrading to `tfparse >= 0.6.18` for improved handling of partially unknown `for_each` blocks.","cause":"This error occurs when `tfparse` encounters dynamic blocks (e.g., `for_each`) or expressions that depend on values that are not resolvable during static parsing (i.e., 'unknown').","error":"tfparse.errors.ParseError: Block iteration failed due to unknown value"},{"fix":"Verify the HCL source for the string value; ensure it's a valid and fully resolved JSON string. Upgrade to `tfparse >= 0.6.19` to benefit from fixes related to how `tfparse` handles unresolved JSON strings.","cause":"You are attempting to parse a string from `tfparse` as JSON, but the string is malformed or contains unresolved HCL references, preventing it from being valid JSON.","error":"json.decoder.JSONDecodeError: Expecting value: line X column Y (char Z)"}]}