observeinc / terraform-aws-collection

Terraform module which streamlines collection from multiple sources within AWS
https://docs.observeinc.com/en/latest/content/integrations/aws/aws.html#installation
Other
3 stars 3 forks source link

Adding a provider produces a warning, possibly due to missing `source` #31

Closed jkinkead closed 1 year ago

jkinkead commented 1 year ago

When submitting a provider to this module, terraform produces a warning about undefined providers.

For example, terraform like:

provider "aws" {   
  region = "us-east-1"
  alias  = "us-east-1"
} 

module "observe_us_east_1" {
  source           = "observeinc/collection/aws"
  version          = "1.2.0"
  observe_customer = "<cust ID>"                                                   
  observe_token    = "<token>"                                              
  providers = {                                                                       
    aws = aws.us-east-1
  }                                                                                   
}  

Will produce a warning like:

│ Warning: Reference to undefined provider
│ 
│   on observe.tf line 123, in module "observe_us_east_1":
│ 123:   providers             = { aws = aws.us-east-1 }
│ 
│ There is no explicit declaration for local provider name "aws" in module.observe_us_east_1, so Terraform is assuming you mean to pass a configuration for
│ "hashicorp/aws".
│ 
│ If you also control the child module, add a required_providers entry named "aws" with the source address "hashicorp/aws".

I think the suggested remediation (add a "source") should solve this (docs).

bendrucker commented 1 year ago

Agreed, the error message is a bit confusing, but I think this comes down to the legacy required_providers specification:

https://github.com/observeinc/terraform-aws-collection/blob/78a6420b8a60ddca095444d7c778afcd00d3d29e/versions.tf#L5

In a literal sense there is a declaration for an aws provider, Terraform is just not considering it. This example helps highlight why:

main.tf

terraform {
  required_providers {
    null = {
      source  = "hashicorp/null"
    }
  }
}

provider "null" {}

module "m" {
  source = "./module"

  providers = {
    null = null
   }
}

module/main.tf

terraform {
  required_providers {
    null = {
      # a random fork
      source = "hc-doc-sparkle/null"
    }
  }
}

resource "null_resouce" "this" {}

This configuration produces an error on terraform init:

╷
│ Error: Provider type mismatch
│
│   on main.tf line 15, in module "m":
│   15:     null = null
│
│ The local name "null" in the root module represents provider "hashicorp/null", but "null" in module.m represents "hc-doc-sparkle/null".
│
│ Each provider has its own distinct configuration schema and provider types, so this module's "null" can be assigned only a configuration for hc-doc-sparkle/null, which is not required by
│ module.m.
╵

Terraform refuses to pass a provider instance for a requirement with a different source, even if the two providers have identical interfaces (schemas).

If a child module doesn't specify source, Terraform seems to effectively ignore the child module's required_providers entry. Fix incoming!

bendrucker commented 1 year ago

And on a related note, this is something we'll be enforcing across all our modules soon!

https://github.com/terraform-linters/tflint-ruleset-terraform/pull/64

bendrucker commented 1 year ago

Thanks for reporting this @jkinkead! Please try version = "1.2.3", that should resolve this. In the very unlikely event it doesn't, go ahead and reopen this and we can investigate.