Terraform跨模块依赖配置:Artifactory本地库优先创建问题
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:
First, define your local repository module instances in your root
local-repository-xxx.tffile. 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 neededThen, in your virtual repository module call (in
virtual-repository-xxx.tf), add adepends_onblock 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_onlist—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_onlist of your virtual repo module/resource.
内容的提问来源于stack exchange,提问作者Pierre Malherbe

