How to make terraform modules wait for resources to be created instead of using any computed values?
I am trying to build several custom Terraform modules. I want to know how to work around dependencies within modules. But we cannot declare a module dependent on each other. Here is the code:
# 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 }"
}
# vpc_private_route module - main. tf
data "aws_nat_gateway" "az1" {
vpc_id = "${ var.vpc_id }"
tags {
Name = "*NAT GW AZ 1"
}
}
data "aws_nat_gateway" "az2" {
vpc_id = "${ var.vpc_id }"
tags {
Name = "*NAT GW AZ 2"
}
}
The result is:
------ SNIP -----
module.vpc_nat_gateway.aws_nat_gateway.nat[1]: Creation complete after 1m50s (ID: nat-02a7f4279cec3a6c8)
module.vpc_nat_gateway.aws_nat_gateway.nat.0: Still creating... (2m0s elapsed)
module.vpc_nat_gateway.aws_nat_gateway.nat[0]: Creation complete after 2m0s (ID: nat-0695a12d9c0305e4c)
Error: Error applying plan:
3 error(s) occurred:
* module.vpc_private_route.data.aws_subnet_ids.private: data.aws_subnet_ids.private: no matching subnet found for vpc with id vpc-0b530d1885e74067b
* module.vpc_private_route.data.aws_nat_gateway.az2: data.aws_nat_gateway.az2: no matching NAT gateway found: {
Filter: [{
Name: "vpc-id",
Values: ["vpc-0b530d1885e74067b"]
},{
Name: "tag:Name",
Values: ["*NAT GW AZ 2"]
}]
}
* module.vpc_private_route.data.aws_nat_gateway.az1: data.aws_nat_gateway.az1: no matching NAT gateway found: {
Filter: [{
Name: "vpc-id",
Values: ["vpc-0b530d1885e74067b"]
},{
Name: "tag:Name",
Values: ["*NAT GW AZ 1"]
}]
}
The Terraform output shows the files are perfect. The execution of a subsequent Terraform application runs without any issue. What should I do to cause the TF module to wait for resource creation before going for the resource?
Since depends_on is called a protected variable, it is not used in any module. You can also find out the syntax differences in the codes 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
]
}
You need to follow a complicated method for Terraform to wait for resources or to get Terraform to do any module dependencies. You can follow the code below to force the module to become aware of the calling from Terraform.