Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

Commit

Permalink
Merge pull request #32 from gruntwork-io/module_dependencies
Browse files Browse the repository at this point in the history
Standardizing dependencies to a list input
  • Loading branch information
autero1 authored May 4, 2019
2 parents 7367df3 + 2d11fdc commit 24cb0ec
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/mysql-private-ip/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ module "mysql" {
private_network = "${google_compute_network.private_network.self_link}"

# Wait for the vpc connection to complete
wait_for = "${google_service_networking_connection.private_vpc_connection.network}"
dependencies = ["${google_service_networking_connection.private_vpc_connection.network}"]

# Set auto-increment flags to test the
# feature during automated testing
Expand Down
2 changes: 1 addition & 1 deletion examples/postgres-private-ip/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ module "postgres" {
private_network = "${google_compute_network.private_network.self_link}"

# Wait for the vpc connection to complete
wait_for = "${google_service_networking_connection.private_vpc_connection.network}"
dependencies = ["${google_service_networking_connection.private_vpc_connection.network}"]

custom_labels = {
test-id = "postgres-private-ip-example"
Expand Down
15 changes: 10 additions & 5 deletions modules/cloud-sql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ locals {
# ------------------------------------------------------------------------------

resource "google_sql_database_instance" "master" {
depends_on = ["null_resource.wait_for"]
depends_on = ["null_resource.dependency_getter"]

provider = "google-beta"
name = "${var.name}"
Expand Down Expand Up @@ -111,11 +111,16 @@ resource "google_sql_user" "default" {
}

# ------------------------------------------------------------------------------
# CREATE A NULL RESOURCE TO EMULATE DEPENDENCIES
# SET MODULE DEPENDENCY RESOURCE
# This works around a terraform limitation where we can not specify module dependencies natively.
# See https://github.com/hashicorp/terraform/issues/1178 for more discussion.
# By resolving and computing the dependencies list, we are able to make all the resources in this module depend on the
# resources backing the values in the dependencies list.
# ------------------------------------------------------------------------------
resource "null_resource" "wait_for" {
triggers = {
instance = "${var.wait_for}"

resource "null_resource" "dependency_getter" {
provisioner "local-exec" {
command = "echo ${length(var.dependencies)}"
}
}

Expand Down
16 changes: 13 additions & 3 deletions modules/cloud-sql/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,17 @@ variable "resource_timeout" {
default = "60m"
}

variable "wait_for" {
description = "By passing a value to this variable, you can effectively tell this module to wait to deploy until the given variable's value is resolved, which is a way to require that this module depend on some other module. Note that the actual value of this variable doesn't matter."
default = ""
# ---------------------------------------------------------------------------------------------------------------------
# MODULE DEPENDENCIES
# Workaround Terraform limitation where there is no module depends_on.
# See https://github.com/hashicorp/terraform/issues/1178 for more details.
# This can be used to make sure the module resources are created after other bootstrapping resources have been created.
# For example:
# dependencies = ["${google_service_networking_connection.private_vpc_connection.network}"]
# ---------------------------------------------------------------------------------------------------------------------

variable "dependencies" {
description = "Create a dependency between the resources in this module to the interpolated values in this list (and thus the source resources). In other words, the resources in this module will now depend on the resources backing the values in this list such that those resources need to be created before the resources in this module, and the resources in this module need to be destroyed before the resources in the list."
type = "list"
default = []
}

0 comments on commit 24cb0ec

Please sign in to comment.