Search code examples
google-cloud-platformcloudterraformnat

Terraform: unknown module referenced


Hope everyone is having a great weekend.

I'm feeling a little puzzled as to the best way to implement a nat gateway in my GCP Project.

I was looking to simply implement this: https://github.com/GoogleCloudPlatform/terraform-google-nat-gateway

I've thrown this in my main.tf:

module "nat" {
  source     = "GoogleCloudPlatform/nat-gateway/google"
  region     = "us-central1"
  network    = "default"
  subnetwork = "default"
}

And I've added this tag (as per the readme), to my compute instances: ${module.nat.routing_tag_regional}

However, I'm seeing the following (which kinda makes sense I guess):

Error: resource 'google_compute_instance.ds3-build' config: reference to undefined module "nat"

As I have the module in my root/main.tf, and the instances are being build via another custom written module. Do I need to output the value from root/main.tf for it to be available within another module?

root/
├── app1-deploy/
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
├── app2-deploy
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
├── app3-deploy
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
├── creds
│   └── account.json
├── scripts
│   └── startup.sh
├── main.tf
├── outputs.tf
├── variables.tf
└── terraform.tfvars

Machines are provisioned inside app1-deploy/main.tf app2-deploy/main.tf etc... and the module "nat" is in root/main.tf


Solution

  • 1) Are the compute resources separate Terraform projects? If so, they cannot see module.nat because your root project is a different project with its own state.

    2) If your nat module is created in the root project, I'd pass along ${module.nat.routing_tag_regional} using a variable on your compute modules.