Undefined provider

This is only a “warning”, but it is driving me crazy.

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

module "aws" {
  source = "./modules/aws"

  providers = {
    aws           = aws,
    aws.us-east-1 = aws.us-east-1,
    aws.us-west-1 = aws.us-west-1,
    aws.us-west-2 = aws.us-west-2,
  }

  required_providers {
    aws = {
      source = "./modules/aws"
      configuration_aliases = [
        aws.us-east-1,
        aws.us-west-1,
        aws.us-west-2,
      ]
    }
  }

  environment    = var.environment
  bucket_names   = var.bucket_names
  bucket_regions = var.bucket_regions
}

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
  required_version = ">= 0.13"
}

Doing terraform init returns the following message:

│ Warning: Reference to undefined provider
│
│   on main.tf line 37, in module "aws":
│   37:     aws           = aws,
│
│ Warning: Reference to undefined provider
│
│   on main.tf line 38, in module "aws":
│   38:     aws.us-east-1 = aws.us-east-1,
|
│ There is no explicit declaration for local provider name "aws" in module.aws, 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".

If I add the suggested required_providers entry in the “module aws” block and do terraform apply, I get:

│ Error: Unsupported block type
│
│   on main.tf line 43, in module "aws":
│   43:   required_providers {
│
│ Blocks of type "required_providers" are not expected here.

What am I doing wrong?

Terraform v1.3.6
on darwin_arm64
+ provider registry.terraform.io/hashicorp/aws v4.48.0

You are specifying the required_providers block in the wrong place - it goes inside a top-level terraform block: Provider Requirements - Configuration Language | Terraform | HashiCorp Developer not inside your module "aws" block. Also, it needs to be moved from there to a file inside your ./modules/aws directory.

2 Likes

Ah, the terraform block goes inside the module!! Wow. How did I miss that one. Thanks for the clarification.