hashicorp / hashicorp/hcl

Unknown token: IDENT , when decoding/parsing terraform file

Open
#468 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
5.8k
Forks
657
Avg merge
20h 36m
Merged PRs (30d)
6

Description

I am parsing main.tf file , so that I can use the arguments passed into the module in my go lang program.

**Terraform 0.13 ( main.tf )**

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.vpc_id
}

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

From the above main.tf file, I would like to parse various arguments ( i.e env, owner, region,cidr etc etc ) that is passed on to the modules.

I am using below go program to try to do some parsing going.

main.go

package main

import (
"fmt"
"io/ioutil"

"github.com/hashicorp/hcl"
)

func main() {

FileContent, err := ioutil.ReadFile("main.tf") // just pass the file name
if err != nil {
fmt.Print(err)
}

var out interface{}
FileContentString := string(FileContent) // convert content to a 'string'

err = hcl.Decode(&out, FileContentString)
if err != nil {
fmt.Println(err)
}
fmt.Println(out)

}

However above program is erroring out with the following error message - **`At 33:11: Unknown token: 33:11 IDENT module.dev-c2an8q6a0brja8jaq3k0-Network.vpc_id`**

If I change the interpolation syntax on the above main.tf file to match with terraform 0.11 and earlier, the program main.go just works fine.

**Terraform 0.11 ( main.tf )**

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.vpc_id}"
}

module "dev-c2an8q6a0brja8jaq3k0-Network" {
source = "gitsource"
cidr = "10.0.0.0/16"
cluster_id = "c2an8q6a0brja8jaq3k0"
env = "dev"
owner = "me"
region = "us-west-2"
super_cluster = "dev"
platform_api = "api"
proj = "virt"
}

main.go program output

map[module:[map[dev-c2an8q6a0brja8jaq3k0-Network:[map[cidr:10.0.0.0/16 cluster_id:c2an8q6a0brja8jaq3k0 env:dev owner:me platform_api:api proj:virt region:us-west-2 source:gitsource super_cluster:dev]]]] output:[map[aws_vpc_main:[map[value:${module.dev-c2an8q6a0brja8jaq3k0-Network.vpc_id}]]]] provider:[map[null:[map[version:2.1]]] map[random:[map[version:2.3]]] map[template:[map[version:2.1]]] map[archive:[map[version:1.3]]] map[aws:[map[region:us-west-2 version:<= 4.0]]]] terraform:[map[backend:[map[s3:[map[bucket:bucket dynamodb_table:terraform-lock key:c2an8q6a0brja8jaq3k0.tfstate region:us-west-2 skip_region_validation:true]]]]]]]

Which, I can parse later on with various methods.

I have looked at the [terraform-config-inspect](https://pkg.go.dev/github.com/hashicorp/terraform-config-inspect@v0.0.0-20210318070130-9a80970d6b34/tfconfig#Module) package , which doesn't support parsing of the arguments pass to the modules.

I am just out of ideas as to how to parse the arguments provided to the modules..

Thank you.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with main.go's hcl.Decode call and the Terraform 0.13 main.tf example, then compare the parser behavior with the older interpolation form. Determine whether the library should accept the direct module reference or whether the issue needs usage guidance; done means a documented or tested way to parse module arguments without the reported unknown-token error.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.