Pierre Malherbe
Pierre Malherbe

Reputation: 81

Terraform depend_on another module in another path

i want to automatize Artifactory repository creation. For that i use the Atlasian artifcatory plugin but i have a problem to declare local-repository befor virtual-repository.

This is my folder :

modules/
├── local-repository/
│   ├── main.tf
│   ├── variables.tf
├── virtual-repository/
│   ├── main.tf
│   └──variables.tf
│── local-repository-xxx.tf
│── virtual-repository-xxx.tf
│── variables.tf
│──provider.tf
│── version.tf

Local-repository.tf :

resource "artifactory_local_repository" "local-repository" {
  key          = var.key
  package_type = var.type
}

Virtual repository :

resource "artifactory_virtual_repository" "virtual-repository" {
  key          = var.key
  package_type = var.type
  repositories = [var.repositories]
}

I want to create all local repositories first, then we want to create virtual repository.

How i use call depend_on in this case ?

Thanks

Upvotes: 6

Views: 18844

Answers (2)

Mr.Koçak
Mr.Koçak

Reputation: 323

I would say

module "x" {
  ...
}

module "y" {
 depends_on = [module.x]
}

Upvotes: 3

Martin Atkins
Martin Atkins

Reputation: 74109

When you return data from a module via an output value or pass data to a module via an input variable, both the value itself and its dependencies pass through.

You can avoid using depends_on altogether here if you populate your child module's variables using references to the objects you want to depend on. For example:

resource "artifactory_local_repository" "local-repository" {
  key          = var.key
  package_type = var.type
}

module "virtual_repository" {
  source = "./virtual-repository"

  # ...

  # If you create the repositories value by referring to
  # artifactory_local_repository.local-repository then
  # any resource in the module that refers to
  # var.repositories will in turn depend on
  # artifactory_local_repository.local-repository
  # automatically.
  repositories = artifactory_local_repository.local-repository[*].id
}

In the unusual case where the references don't specify all of the dependencies automatically, you can use input variables and output values in depends_on. For example:

  # Using var.repositories in a depends_on
  # means that the resource will depend on
  # whatever var.repositories depends on, even
  # if it doesn't actually use the value of
  # the variable.
  depends_on = [var.repositories]
  # Using module.local_repository.id in
  # a depends_on means that the resource will
  # depend on whatever the output "id"
  # declaration in the module depends on.
  depends_on = [module.local_repository.id]

In the even more unusual case where the output itself needs to carry a dependency it wouldn't normally have, you can use depends_on inside an output block:

output "id" {
  value      = "anything"
  depends_on = [artifactory_local_repository.local-repository]
}

It's almost always possible and better to just refer to other objects and let Terraform calculate the dependencies automatically, because then the dependencies will automatically stay correct as your module evolves and you won't have to write a lot of additional dependency annotations. depends_on is only for unusual situations where dependencies that are "hidden" to Terraform, due to there being a relationship between two objects that is not captured by the arguments to those objects. depends_on is a last resort.

Upvotes: 6

Related Questions