How to parse terraform .tf files and its module parameters


I have been trying to prase the terraform files since last few days atleast with not much success.

So I want to post a question over here , if something I am missing or can be improve upon.

My file is as below.

terraform {
  backend "s3" {
    bucket                 = "bucket"
    key                    = "c2an8q6a0brja8jaq3k0.tfstate"
    region                 = "us-west-2"
    dynamodb_table         = "terraform-lock"
    skip_region_validation = true

provider "null" {
  version = "2.1"

provider "random" {
  version = "2.3"

provider "template" {
  version = "2.1"

provider "archive" {
  version = "1.3"

provider "aws" {
  version = "<= 4.0"
  region  = "us-west-2"

output "aws_vpc_main" {
  value =

module "dev-c2an8q6a0brja8jaq3k0-Network" {
  source        = "gitsource"
  cidr          = ""
  cluster_id    = "c2an8q6a0brja8jaq3k0"
  env           = "dev"
  owner         = "me"
  region        = "us-west-2"
  super_cluster = "dev"
  platform_api  = "api"
  proj          = "this"

Rest of the other modules..

I want to parse the above .tf file and want to use the parameters that is getting passed on to the Network module ( i.e cidr,env,region,super_cluster etc etc ).

I just want to know some pointers about parsing this file and the arguments we are passing to the module

I have looked at the terraform-config-inspect package, however it doesn’t support parsing the parameters/body passed on the modules. GitHub - hashicorp/terraform-config-inspect: A helper library for shallow inspection of Terraform configurations

I have some POC code, that can use LoadFile method from config · , however that is not supported from Terraform 0.14 and onwards.

I have looked at the other packages in recent version of terraform like configs · and its Parser struct type , however I am not getting much success there either.

Any pointer, would be much appreciated.

I am happy to share the work / code , I have written so far upon further request.

Thank You.

Hi @aashitvyas,

I would suggest using terraform-config-inspect as a starting point here. If you don’t care about supporting older versions of Terraform then you can skip the parts that deal with the legacy version of HCL, but ultimately your program to read module arguments will follow a similar pattern than terraform-config-inspect up to the point where decodes a particular module block.

At that point terraform-config-inspect discards the second return value from block.Body.PartialContent because, as you noticed, it doesn’t consider the module-specific arguments and focuses only on the meta-arguments like source and version. That other return value is where you’ll find the arguments you are looking for, so you could use a technique something like this:

_, remain, contentDiags := block.Body.PartialContent(moduleCallSchema)
diags = append(diags, contentDiags...)

attrs, contentDiags := remain.JustAttributes()
diags = append(diags, contentDiags...)

for name, attr := range attrs {
    // Do something for each individual attribute here

You didn’t mention what exactly you want to do with these attributes, so I don’t know what goes inside that for loop, but hopefully that’s a useful pointer for you to get the rest of the way. You can refer to the HCL v2 main package documentation to see what operations and fields are available on the various types involved here.