virtuald/pyhcl

Failure to load references or resolve them

braxtone opened this issue · 4 comments

I'm using locals in my main.tf file for tagging values and then using them to simplify tagging of other resources like so:

$ cat main.tf
...
locals {
  tagging_standard = {
    "Environment" = "Development"
    "Description" = "<some description>"
    "Application" = "app1"
    "Creator"     = "joebob"
...
  }
}

$ cat vpc.tf
...
resource "aws_default_route_table" "r" {
  default_route_table_id = "${aws_vpc.vpc.default_route_table_id}"

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = "${aws_internet_gateway.gw.id}"
  }

  tags = "${local.tagging_standard}"
}

When I try to use pyhcl to parse the hcl/tf files, it doesn't resolve them, and instead includes them as strings:

$ hcltool /path/to/terraform/vpc.tf
{
    "resource": {
        "aws_default_route_table": {
            "r": {
                "default_route_table_id": "${aws_vpc.vpc.default_route_table_id}",
                "route": {
                    "cidr_block": "0.0.0.0/0",
                    "gateway_id": "${aws_internet_gateway.gw.id}"
                },
                "tags": "${local.tagging_standard}"
            }
        },
...

Expected behavior:

$ hcltool /path/to/terraform/vpc.tf
{
    "resource": {
        "aws_default_route_table": {
            "r": {
                "default_route_table_id": "${aws_vpc.vpc.default_route_table_id}",
                "route": {
                    "cidr_block": "0.0.0.0/0",
                    "gateway_id": "${aws_internet_gateway.gw.id}"
                },
                "tags": {
                    "Environment" = "Development",
                    "Description" = "<some description>",
                    "Application" = "app1",
                    "Creator"     = "joebob"
                }
            }
        },
...

Is there another way to get pyhcl to traverse a whole project and do this translation like the terraform cli does? Thanks!

pyhcl only consumes HCL and does not do application-specific interpolation such as terraform does (nor should it, as this is application-specific).

I would recommend creating your own library (perhaps call it pyterraform?) with this functionality, I'm sure other terraform users would find it quite useful.

Thanks for the response. You wouldn't happen to know if there are other projects that already do this would you?

I don't know of any, no.

Worth a shot. It looks like terraform_validate has a substitute_variable_values_in_string method that's close enough to duplicate for locals expansion. Thanks for the great library to build off of!