You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Terraform模块中指定使用jfrog/artifactory提供商

问题描述

我需要创建一个关联Artifactory提供商的Terraform模块。根目录的config.tf已配置了AWS和Artifactory提供商,内容如下:

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
  }
}

在main.tf中调用Artifactory模块时:

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

出现以下错误:

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

执行terraform providers后,发现子模块依赖了不存在的提供商:

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]

如何将模块使用的提供商从provider[registry.terraform.io/hashicorp/artifactory]改为registry.terraform.io/jfrog/artifactory?


解决方案

解决这个问题的核心是让子模块明确指定Artifactory提供商的源地址,具体步骤如下:

  1. 在子模块中添加提供商声明
    进入./modules/artifactory_resources目录,创建或修改providers.tf(或直接在模块的main.tf开头),添加terraform块的required_providers配置,指定Artifactory提供商的源和版本,和根模块保持一致:

    terraform {
      required_providers {
        artifactory = {
          version = "6.22.3"
          source  = "registry.terraform.io/jfrog/artifactory"
        }
        # 如果模块用到了local提供商,也可以在这里声明,保持版本一致
        local = {
          version = "~> 2.4.0" # 根据实际使用版本调整
          source  = "hashicorp/local"
        }
      }
    }
    
  2. 验证提供商依赖
    回到根目录,执行terraform providers,此时应该看到module.artifactory_resources依赖的是registry.terraform.io/jfrog/artifactory,而非hashicorp的版本。

  3. 重新初始化Terraform
    执行terraform init -upgrade,让Terraform重新拉取正确的提供商版本,确保配置生效。


内容的提问来源于stack exchange,提问作者Sam Comber

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 13:50:32