tflint icon indicating copy to clipboard operation
tflint copied to clipboard

Add local values support

Open wata727 opened this issue 3 years ago • 0 comments

Fixes https://github.com/terraform-linters/tflint/issues/571

As a result of the https://github.com/terraform-linters/tflint/pull/1510 refactoring, it's easier to add support for local values 🎉

Previously, the type of reference in the expression determined whether it could be evaluated or not, so it's hard to support local values that might resolve to unsupportable named values in a step of evaluation. However, unsupported named values are now evaluated to unknown, so static local values are resolved to final values, and unsupported named values are resolved to unknown values.

variable "foo" {
  default = "variable value"
}

locals {
  static   = "static value"
  variable = var.foo
  local    = local.static
  unknown  = aws_instance.main.arn
}

local.static   # => "static value"
local.variable # => "variable value"
local.local    # => "static value"
local.unknown  # => unknown

Note that the internal implementation takes a different approach than Terraform. Terraform references state for resolving local values. https://github.com/hashicorp/terraform/blob/v1.3.0/internal/terraform/evaluate.go#L334

TFLint cannot refer to the state, so it simply evaluates the local value's expression and recursively get cty.Value. Note that this implementation will fall into an infinite loop if the expression contains circular references. So we need a mechanism to detect circular references from the graph, just like Terraform.

wata727 avatar Sep 23 '22 18:09 wata727