Why `Reference to undefined provider` warning is coming?

Hi All,
I am getting below warning on passing explicit providers in module :

 Warning: Reference to undefined provider
│ 
│   on managed_kafka_redis.tf line 30, in module "create_msk_ap-southeast-3":
│   30:     aws = aws.ap-southeast-3
│ 
│ There is no explicit declaration for local provider name "aws" in module.create_msk_ap-southeast-3, 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".

Files structure:

  • providers.tf
  • main.tf
    -variables.tf
    -modules
    -sampleModule.tf

providers.tf

terraform {
  required_version = ">= 1.0"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 4.11.0"
      configuration_aliases = [ aws.us-east-1] 
    }
  }
provider "aws" {
  region     = var.aws_region
  access_key = var.aws_access_key_id
  secret_key = var.aws_secret_key
}

provider "aws" {
  region     = "us-east-1"
  access_key = var.aws_access_key_id
  secret_key = var.aws_secret_key
  alias      = "us-east-1"
}
}

main.tf

module "create_resource" {
  source = "./modules"
  for_each = {for x in var.sampleInput: x=> x }
}
module "create_resource_us-east" {
  source = "./modules"
  providers = {
    aws = aws.us-east-1
  } 
  for_each = {for x in var.sampleInput: x=> x }
}

As the module is having for_each loop, terraform does not allows specifying providers configuration inside the module. Ref- Providers Within Modules - Configuration Language | Terraform | HashiCorp Developer

I had verified the code and appropriate provider configuration is getting passed to each of the module.

But I am not sure why I am getting the warning Reference to undefined provider and how to resolve it. Any insights on this?

Terraform is informing you that you have not included a required_providers block within the module, and so it is having to make an assumption, via legacy compatibility code, that by aws you mean hashicorp/aws.

You should include a required_providers block in every Terraform module. They do not get inherited from one directory to another.

You do not need to re-specify the version constraints in every directory, so a simple

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

will suffice.

2 Likes

I added required_providers inside module and it worked. Thank you.

We encounter similar warning :

Warning: Reference to undefined provider

on secondary.tf line 5, in module “secondary”:
5: aws = aws.staging-secondary

There is no explicit declaration for local provider name “aws” in module.secondary, so Terraform is assuming you mean to pass a configuration for “hashicorp/aws”.


In provision/staging/_provider.tf, we have:

provider “aws” {
alias = “staging-secondary”
region = “us-east-2”
profile = “staging”

Will the warning affect the operation of above alias in disaster recovery scenario ?

Thanks