terraform-linters / tflint-ruleset-terraform

TFLint ruleset for Terraform Language
Mozilla Public License 2.0
61 stars 21 forks source link

required_version warnings for module not installed from registry #25

Closed levinotik closed 2 years ago

levinotik commented 2 years ago

I am using tflint via pre-commit-terraform and running into an issue with some of the rules. I'm not sure if this is due to the way I'm running tflint or if this is truly a bug.

The issue I'm running into is warnings about missing required_version for modules that are defined locally, not from any registry. According to the terraform docs:

Version constraints are supported only for modules installed from a module registry

Yet, for each of my own local modules, tflint produces the following error:

Warning: terraform "required_version" attribute is required (terraform_required_version)

  on  line 0:
   (source code not available)

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

\e[0m\e[33mTFLint in vpc/:\e[0m
1 issue(s) found:

For example, each of the following produces this warning:

module "vpc" {
  source = "./vpc"
  vpc_id = var.vpc.vpc_id
}

# Creating security groups
module "security_group" {
  source      = "./security_group"
  environment = var.environment
  vpc         = module.vpc.vpc
}

module "rds" {
  source          = "./rds"
  subnet_ids      = module.vpc.private_subnets
  security_groups = [module.security_group.sg_db.security_group_id]
  depends_on      = [
    module.security_group
  ]
}
module "sqs" {
  source      = "./sqs"
  environment = var.environment
}
# .... etc...

I may very well be missing something basic. Please let me know if I am. Thanks!

wata727 commented 2 years ago

This is not an issue for modules. See the following documentation for details. https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_required_version.md

levinotik commented 2 years ago

I'm sorry, I meant to paste the following error related to terraform_module_version, not terraform_required_version:

Warning: module "db_security_group" should specify a version (terraform_module_version)

  on main.tf line 29:
  29: module "db_security_group" {

where this points to a module I have defined (not from a registry):

module "security_group" {
  source      = "./security_group"
  environment = var.environment
  vpc         = module.vpc.vpc
  depends_on = [
    module.vpc
  ]
}
wata727 commented 2 years ago

Hmm, I cannot reproduce it. Do you have a minimal reproducible code?

% cat .tflint.hcl
config {
  disabled_by_default = true
}

rule "terraform_module_version" {
  enabled = true
}

% cat main.tf
module "security_group" {
  source      = "./security_group"
  environment = var.environment
  vpc         = module.vpc.vpc
  depends_on = [
    module.vpc
  ]
}

module "latest" {
  source  = "terraform-aws-modules/vpc/aws"
}

% tflint
1 issue(s) found:

Warning: module "latest" should specify a version (terraform_module_version)

  on main.tf line 10:
  10: module "latest" {

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

% tflint -v
TFLint version 0.40.1
+ ruleset.terraform (0.1.1-bundled)
levinotik commented 2 years ago

Wow, you know what...this was me just having a total brain fart and misreading the modules that tflint was complaining about. The modules in question are, in fact, modules from the registry and so the warnings are legitimate and helpful! So sorry for the confusion! Closing issue.