About this question
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?