terraform-linters / tflint

A Pluggable Terraform Linter
Mozilla Public License 2.0
4.98k stars 357 forks source link

Override files are not compliant with the Terraform spec #2114

Closed wata727 closed 1 month ago

wata727 commented 2 months ago

Summary

TFLint supports Terraform override files, but is not fully compliant with the spec.

For these reasons, using override files can sometimes produce different results than Terraform would, or can lead to inconsistent results like those in https://github.com/terraform-linters/tflint-ruleset-terraform/issues/205.

Command

tflint

Terraform Configuration

# main.tf
terraform {}
provider "aws" {}

# main2.tf
terraform {
  required_providers {}
}

# main_override.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 5.64.0"
    }
  }
  required_version = "1.0.0"
}

TFLint Configuration

# empty file

Output

@wata727 ➜ /tmp/work $ tflint
@wata727 ➜ /tmp/work $ tflint
@wata727 ➜ /tmp/work $ tflint
1 issue(s) found:

Warning: Missing version constraint for provider "aws" in `required_providers` (terraform_required_providers)

  on main.tf line 3:
   3: provider "aws" {}

Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.9.1/docs/rules/terraform_required_providers.md

This inconsistent result is caused by which the terraform block is overwritten and which block is retrieved by the terraform_required_providers rule. If the block is retrieved that was not overwritten, this rule will emit an issue.

Ideally, the Terraform blocks in main.tf and main2.tf should be merged first.

TFLint Version

0.53.0

Terraform Version

No response

Operating System