Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform - dependency between modules

Tags:

terraform

I'm trying tell terraform the resource is dependent on other one. The problem is the resources are in separate modules. The dependent resource looks like that:

variable dependency {
  type = "list"
  default = []
}

resource "docker_container" "web" {
  depends_on = "${var.dependency}"
...

Then I 'call' the module:

module "wordpress" {
  source = "../modules/wordpress"
  dependency = [ "${module.provision.res}" ]
}

And I got error:

  on ../modules/wordpress/main.tf line 11, in resource "docker_container" "web":
  11:   depends_on = "${var.dependency}"

A static list expression is required.

It looks like I cannot use variable in 'depends_on'. How to create dependency between modules?

PS: The resource I depend on is a null_resource which provides some some provisioning. I need to rebuild some stuff every time it changes.

like image 779
Maciej Wawrzyńczuk Avatar asked Jan 26 '23 21:01

Maciej Wawrzyńczuk


1 Answers

To solve the error: "A static list expression is required."

You need to wrap the var.dependency with []:

resource "docker_container" "web" {
  depends_on = ["${var.dependency}"]
...

Update: The syntax above is for terraform<0.12. For terraform >=0.12, as @Maciej Wawrzyńczuk points out, [var.dependency] will just work in this case. ["${var.dependency}"] will also work in 0.12 as backward compatibility but if you run tf 0.12 you probably want to do it the new way.

like image 177
congbaoguier Avatar answered Jan 28 '23 11:01

congbaoguier