terraform-linters / tflint

A Pluggable Terraform Linter
Mozilla Public License 2.0
4.86k stars 353 forks source link

tflint throws a deprecated interpolation warning when terraform requires it #1473

Closed nirojan closed 2 years ago

nirojan commented 2 years ago

Introduction

Tflint warns about using deprecated terraform interpolation when Terraform suggests that I do use the old interpolation when using a variable as key in locals.

locals {
  environment_settings = {
    "${var.env}" = {
      a = "b"
      c = "d"
    }
  }
}

Expected Behavior

tflint doesn't warn about old interpolation behaviour in this case as that is what Terraform suggests I do, or it will error.

Actual behavior

Tflint warns interpolation that is required by Terraform.

❯ tflint
1 issue(s) found:

Warning: Interpolation-only expressions are deprecated in Terraform v0.12.14 (terraform_deprecated_interpolation)

  on locals.tf line 3:
   3:     "${var.env}" = {

Reference: https://github.com/terraform-linters/tflint/blob/v0.39.2/docs/rules/terraform_deprecated_interpolation.md

Step to Reproduce

  1. Create locals.tf with following content
    locals {
    environment_settings = {
    "${var.env}" = {
      a = "b"
      c = "d"
    }
    }
    }
  2. Create vars.tf file with following
    variable "env" {
    default     = "dev"
    }
  3. Run tflint

Additional Context

❯ terraform --version
Terraform v1.2.6
on darwin_amd64
❯ tflint --version
TFLint version 0.39.2
+ ruleset.aws (0.15.0)

Terraform error if I make the change suggested by tflint

❯ terraform validate
╷
│ Error: Ambiguous attribute key
│
│   on locals.tf line 3, in locals:
│    3:     var.env = {
│
│ If this expression is intended to be a reference, wrap it in parentheses. If it's instead intended as a literal name containing periods, wrap
│ it in quotes to create a string literal.
bendrucker commented 2 years ago

Duplicate of #1462

kawashinji commented 1 year ago

I resolved as below.

locals {
  environment_settings = {
    (var.env) = {
      a = "b"
      c = "d"
    }
  }
}

See: https://www.terraform.io/language/expressions/types#maps-objects

You can use a non-literal string expression as a key by wrapping it in parentheses, like (var.business_unit_tag_name) = "SRE".