How to instruct module to use particular provider in terraform?

Advertisements

I want to create a module that is linked to my artifactory provider. In my config.tf where I have configured my providers, I also have hashicorp/aws defined like below…

terraform {
  required_version = "~> 1.2.4"
    
  required_providers {
    aws = {
      version = "4.64.0"
      source  = "hashicorp/aws"
    }
    artifactory = {
      version = "6.22.3"
      source  = "registry.terraform.io/jfrog/artifactory"
    }
  }
}
    
// ARTIFACTORY // 
    
provider "artifactory" {
  url           = "<my artifactory url>"
  access_token  = "<my token>"
}
    
// AWS // 
   
provider "aws" {
  region              = var.region
  allowed_account_ids = [var.account_id]
  assume_role {
    role_arn = var.terraform_role_arn
  }
}

When I attempt to create a module using artifactory provider like the below inside a main.tf file….

module "artifactory_resources" {
  source     = "./modules/artifactory_resources"
  provider = artifactory
}
    
module "model_store_s3" {
  source     = "./modules/storage/s3"
  aws_region = local.aws_region
}

I get the following error…

Could not retrieve the list of available versions for provider hashicorp/artifactory: provider registry registry.terraform.io does not have a provider named registry.terraform.io/hashicorp/artifactory

Did you intend to use jfrog/artifactory? If so, you must specify that source address in each module which requires that provider. To see which modules are currently depending on hashicorp/artifactory, run the following command: terraform providers

And when I run terraform providers I can see my module is trying to use a non-existent provider…

Providers required by configuration:
.
├── provider[registry.terraform.io/jfrog/artifactory] 6.22.3
├── provider[registry.terraform.io/hashicorp/aws] 4.64.0
├── module.model_store_s3
│   └── provider[registry.terraform.io/hashicorp/aws]
├── module.artifactory_resources
│   ├── provider[registry.terraform.io/hashicorp/local]
│   └── provider[registry.terraform.io/hashicorp/artifactory]

I can see the wrong provider is being used in the module, how can I change providers fromprovider[registry.terraform.io/hashicorp/artifactory to registry.terraform.io/jfrog/artifactory?

>Solution :

Each module has to have required providers defined as per the documentation:

Each Terraform module must declare which providers it requires, so that Terraform can install and use them. Provider requirements are declared in a required_providers block.

Furthermore, there is one really important thing to note:

Each module must declare its own provider requirements. This is especially important for non-HashiCorp providers.

This means you have to declare the Artifactory provider (as it is a non-HashiCorp provider) inside of the module code, i.e., in the artifactory_resources module. The way provider configuration is inherited is detailed in the documentation as well.

Leave a ReplyCancel reply