No explicit declaration for local provider name "aws"

I know there were similar issues raised before BUT we do have the required provider set in the env folder in each environment and still getting the warning message in all of our modules - we are trying to upgrade from 1.3.2 to 1.9.5 and cleaning up warnings and deprecations in the code

│ Warning: Reference to undefined provider
│
│   on module-us-east-1.tf line 13, in module "policy_us-east-1":
│   13:     aws = aws.us-east-1
│
│ There is no explicit declaration for local provider name "aws" in module.policy_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".
│
│ (and 166 more similar warnings elsewhere)

Files structure:

TF
|env

||development
providers.tf
main.tf
variables.tf
module-us-east-1.tf
module-eu-west-2.tf

||preproduction
providers.tf
main.tf
variables.tf
module-us-west-2.tf

|modules

||testing
test.tf
variables.tf
outputs.tf

providers.tf

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

  assume_role {
    role_arn     = xxxx
    session_name = xxxx
  }
}
provider "aws" {
  alias  = "eu-west-2"
  region = "eu-west-2"

  assume_role {
    role_arn     = xxxxxx
    session_name = xxxxxxx
  }
}
  }
}

main.tf

terraform {
  required_version = "=1.3.2"
  backend "s3" {
    bucket   = 
    region   = "us-east-1"
  }
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = xxxxx
    }
    tls = {
      source  = "hashicorp/tls"
      version = xxxxx
    }
  }
}

module-us-east-1.tf

module "testing" {
  source            = "../../modules/testing"
  name              = "Testing"
  instance_type     = "c5.2xlarge"
  cidr_block        = "0.0.0.0"
  security_group_id = "sg-12345678"
  policy_arn        = "arn:aws:iam::12345678:policy/some_policy"
  providers = {
    aws = aws.us-east-1
  }
}

if I will put the required provides in module-us-east-1.tf like everyone is suggesting (deleting it from main.tf), I am getting this error

│ Error: Duplicate required providers configuration
│
│   on modules_us-east-1.tf line 6, in terraform:
│    6:   required_providers {
│
│ A module may have only one required providers configuration. The required providers were previously configured at main.tf:9,3-21.

This warning is also in the single region modules and if I do configuration_aliases

Hi @annaJ

The warning does not mean add the required_providers to the individual file with the module call, all files are part of the same module, so which file it’s in doesn’t matter. It is trying to explain that you should also have required_providers within the module (../../modules/testing), so that the module can declare exactly what providers it requires.

Thank you for editing your answer, it makes so much more sense now :wink:

It is a little counterproductive to have the same config in 2 places as I tested one of the environments and it does in fact not have the warning when both modules are updated with the required provider
We will have to update over 124 modules to get rid of the warning

Can you tell me why those were changed? We running the show from env/module_region.tf and it was inherited by the ../..modules in the version before 1.0

The warning was added quite some time ago, probably before v1.0 when the behavior was stabilized. The reason is that each module is independent and can require different providers, and use different local names within the module. Because nested modules aren’t meant to depend on the parent module in which they are used, they must declare their requirements explicitly to avoid incompatibilities or unexpected behavior.