Update: As of TF 0.13x, depends_on
is supported to create dependencies between modules. Using depends_on
should be preferable over using the method detailed below.
The previous most popular answer is out of date with Terraform in version 0.12.24.
depends_on
is a protected variable, and cannot be used in a module
.
In addition there are a few syntax differences.
I've updated the example below.
# ROOT level main.tf
# -------------------------------------------------------------------
# Create NAT Gateway - Associates EIP as well
# -------------------------------------------------------------------
module "vpc_nat_gateway" {
source = "./vpc_nat_gateway"
vpc_id = module.vpc.id
public_subnet_ids = module.vpc_subnets.public_subnet_ids
private_cidr = var.private_cidr
common_tags = local.common_tags
}
# -------------------------------------------------------------------
# Create Private Routes
# -------------------------------------------------------------------
module "vpc_private_route" {
source = "./vpc_private_route"
vpc_id. = module.vpc.id
nat_gateway_id = module.vpc_nat_gateway.nat_gateway_id
common_tags = local.common_tags
# Depends is a custom variable, depends_on is a reserved keyword.
depends = [module.vpc_nat_gateway.nat_gateway_id]
}
# vpc_private_route module - main.tf
variable "depends" {
default = []
}
resource "null_resource" "depends_on" {
triggers = {
depends_on = "${join("", var.depends)}"
}
}
data "aws_nat_gateway" "az1" {
vpc_id = var.vpc_id
depends_on = [
null_resource.depends_on
]
}
data "aws_nat_gateway" "az2" {
vpc_id = var.vpc_id
depends_on = [
null_resource.depends_on
]
}