terraform-linters / tflint

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

"This value is null, so it does not have any attributes." error from upstream module #1395

Closed spkane closed 2 years ago

spkane commented 2 years ago

I have an upstream module that I am using which causes this error when linting.

TFLint in _tf_modules/k8s/:

Failed to prepare rule checking; failed to eval an expression in .terraform/modules/k8s-addons/modules/kubernetes-addons/helm-addon/main.tf:67; Attempt to get attribute from null value: This value is null, so it does not have any attributes.

The module has this variable defined:

variable "irsa_config" {
  type = object({
    kubernetes_namespace              = string
    create_kubernetes_namespace       = optional(bool)
    kubernetes_service_account        = string
    create_kubernetes_service_account = optional(bool)
    irsa_iam_policies                 = optional(list(string))
  })
  description = "Input configuration for IRSA module"
  default     = null
}

which is then referenced here:

module "irsa" {
  count                             = var.irsa_config != null ? 1 : 0
  source                            = "../../irsa"
  create_kubernetes_namespace       = try(var.irsa_config.create_kubernetes_namespace, true)
  create_kubernetes_service_account = try(var.irsa_config.create_kubernetes_service_account, true)
  kubernetes_namespace              = var.irsa_config.kubernetes_namespace
  kubernetes_service_account        = var.irsa_config.kubernetes_service_account
  irsa_iam_policies                 = var.irsa_config.irsa_iam_policies
  addon_context                     = var.addon_context
}

and this generates the above error during linting.

The terraform code works in general.

Is there a way to fix this in tflint, ignore this error, or ignore this specific module or file?

Version

$ tflint -v
TFLint version 0.36.2
+ ruleset.aws (0.13.4)

$ terraform -v
Terraform v1.2.1
on darwin_amd64
wata727 commented 2 years ago

This error seems to make sense. I think this is because null is passed as an argument that should not be passed when calling the module (to be exact, the default value is used by omitting the argument). You probably get the same error when you run terraform plan.

spkane commented 2 years ago

So, I actually get the warning twice. Once for a module that we have, which actually directly uses the upstream module, and is located in _tf_modules/k8s/, and once for the terraform code that calls that module located in environments/cluster-automation-sandbox/k8s/poc/.

TFLint in _tf_modules/k8s/:
Failed to prepare rule checking; failed to eval an expression in .terraform/modules/k8s-addons/modules/kubernetes-addons/helm-addon/main.tf:66; Attempt to get attribute from null value: This value is null, so it does not have any attributes.

TFLint in environments/cluster-automation-sandbox/k8s/poc/:
Failed to prepare rule checking; failed to eval an expression in .terraform/modules/k8s-addons/modules/kubernetes-addons/helm-addon/main.tf:67; Attempt to get attribute from null value: This value is null, so it does not have any attributes.

I can run terraform plan just fine inside of environments/cluster-automation-sandbox/k8s/poc/ and DO NOT get the null error.

Since _tf_modules/k8s/ is just a module, I can't directly run terraform plan inside it, as the provider is unconfigured and some variables would need setting.

wata727 commented 2 years ago

Understood. In this case, I recommend removing the default value from the irsa_config variable. The default value is null, but obviously it's not supposed to use as null (it looks like a de facto required variable).

My recommendation is to fix the upstream module, but if it's difficult, I have no choice but to ignore inspection for the module.

wata727 commented 2 years ago

I checked the module again, but it seems that the problem is that TFLint evaluates module arguments without considering the count argument.

I haven't confirmed the behavior, but Terraform may not evaluate arguments if the module's count is 0. In that case, TFLint should also be fixed to follow the semantics.

spkane commented 2 years ago

I have just tested this and indeed it appears to have solved the original issue. Thank you very much for getting this in!