terraformterraform-provider-awsterragruntterraform-modules

Terraform: remote module not following providers block - Warning: Reference to undefined provider


I'm trying to use a (private) remote terraform module, and trying to pass a different provider to it. For the remote module, there are no providers defined and to my understanding, it will use a the local provider instead.

I can't seem to be able to get it to use a provider alias - there are a few files at play here:

# main.tf
provider "aws" {
  region = var.aws_region

}

provider "aws" {
  alias  = "replica_region"
  region = var.replica_region
}

terraform {
  backend "s3" {
  }
}
# s3.tf
module "some-remote-module" {
  source = 'git::ssh.......'
  providers = {
    aws = aws.replica_region
  }
}

Whenever I plan (with terragrunt), The region is that of the primary aws provider config. I get the following warning, too:

│ Warning: Reference to undefined provider
│
│   on s3.tf line 12, in module "some-remote-module":
│   12:     aws = aws.replica_region
│
│ There is no explicit declaration for local provider name "aws" in
│ module.some-remote-module, 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".
╵

Am I passing the providers in incorrectly? Is this even something that terraform is capable of? I'm using terraform 1.3. The remote module doesn't have any provider config.


Solution

  • The last paragraph of this message is suggesting that you modify the child module to include the following declaration so that it's explicit that when you say "aws" it means hashicorp/aws rather than a provider in some other namespace that might coincidentally also be called "aws":

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

    This is what the error message means by a required_providers entry named "aws" with the source address "hashicorp/aws".

    This allows Terraform to see for certain (rather than guessing) that the short name "aws" refers to the same provider in both the calling module and the called module.