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

Terraform跨模块依赖配置:Artifactory本地库优先创建问题

How to Use depends_on to Ensure Local Repositories Are Created Before Virtual Repositories in Terraform

Got it, let's break down how to solve this dependency problem for your Artifactory repository setup. The goal is to make sure all local repositories are fully created before Terraform starts provisioning your virtual repository—and depends_on is exactly the tool for this.

First, Let's Cover Two Common Scenarios Based on Your Setup

Scenario 1: You're Using Modules (Which Looks Like Your Case)

Your directory structure has separate modules for local and virtual repositories, and you're calling these modules from your root Terraform files. Here's how to wire up the dependency:

  1. First, define your local repository module instances in your root local-repository-xxx.tf file. For example:

    module "local_repo_maven" {
      source = "./modules/local-repository"
      key    = "maven-local"
      type   = "maven"
    }
    
    module "local_repo_npm" {
      source = "./modules/local-repository"
      key    = "npm-local"
      type   = "npm"
    }
    
    # Add more local repo modules as needed
    
  2. Then, in your virtual repository module call (in virtual-repository-xxx.tf), add a depends_on block that lists all your local repository modules. This tells Terraform to wait for all those local repos to be ready before creating the virtual one:

    module "virtual_repo_all" {
      source = "./modules/virtual-repository"
      key         = "all-virtual"
      type        = "generic" # Match your package type here
      repositories = [module.local_repo_maven.key, module.local_repo_npm.key]
    
      # Explicitly depend on all local repo modules
      depends_on = [
        module.local_repo_maven,
        module.local_repo_npm
        # Add other local repo modules here if you have them
      ]
    }
    

Scenario 2: You're Defining Resources Directly in the Root (No Modules)

If you skipped the modules and defined resources directly in your root .tf files, the approach is similar—but you'll reference the actual resource objects instead of modules:

# local-repository-xxx.tf
resource "artifactory_local_repository" "maven_local" {
  key          = "maven-local"
  package_type = "maven"
}

resource "artifactory_local_repository" "npm_local" {
  key          = "npm-local"
  package_type = "npm"
}

# virtual-repository-xxx.tf
resource "artifactory_virtual_repository" "all_virtual" {
  key          = "all-virtual"
  package_type = "generic"
  repositories = [
    artifactory_local_repository.maven_local.key,
    artifactory_local_repository.npm_local.key
  ]

  # Explicitly depend on all local repo resources
  depends_on = [
    artifactory_local_repository.maven_local,
    artifactory_local_repository.npm_local
  ]
}

A Quick Note About Implicit vs. Explicit Dependencies

You might notice that when you reference a local repo's key in the virtual repo's repositories list, Terraform creates an implicit dependency. However, adding an explicit depends_on is still a good practice here—especially if you have many local repos, or if your module structure makes the implicit dependency less obvious. It removes any ambiguity and ensures Terraform follows the exact order you need.

Final Tips

  • Make sure to include all local repository modules/resources in the depends_on list—if you miss one, Terraform might try to create the virtual repo before that local repo is ready.
  • If you ever add new local repos later, don't forget to add them to the depends_on list of your virtual repo module/resource.

内容的提问来源于stack exchange,提问作者Pierre Malherbe

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 10:27:46