如何指示模块在Terraform中使用特定的提供程序?

huangapple go评论62阅读模式
英文:

How to instruct module to use particular provider in terraform?

问题

You can change the provider source from registry.terraform.io/hashicorp/artifactory to registry.terraform.io/jfrog/artifactory by updating the provider configuration in your main.tf file. Here's the corrected code:

module "artifactory_resources" {
  source   = "./modules/artifactory_resources"
  providers = {
    artifactory = "registry.terraform.io/jfrog/artifactory"
  }
}

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

By specifying the providers block within the module configuration and setting the artifactory provider to the correct source, you ensure that the module uses the intended provider. This should resolve the issue you encountered.

英文:

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?

答案1

得分: 1

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.

> 每个 Terraform 模块都必须声明它需要哪些提供程序,以便 Terraform 可以安装并使用它们。提供程序要求在 required_providers 块中声明。

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.

> 每个模块必须声明自己的提供程序要求。对于非 HashiCorp 提供程序,这一点尤为重要。

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.

这意味着您必须在模块代码内部声明 Artifactory 提供程序(因为它是非 HashiCorp 提供程序),即在 artifactory_resources 模块中。提供程序配置的继承方式也在文档中详细说明。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月10日 22:04:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219379.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定