Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jkinkead opened this issue Feb 12, 2023 · 3 comments · Fixed by #36
Closed

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

jkinkead opened this issue Feb 12, 2023 · 3 comments · Fixed by #36

Comments

@jkinkead
Copy link

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
Copy link
Contributor

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

aws = ">= 3.75"

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
Copy link
Contributor

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

terraform-linters/tflint-ruleset-terraform#64

@bendrucker
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants