0

Trying to implement a Data Module for referencing a 'Robot Account' for Terraform.

I get the folowing errors:

Error: Reference to undeclared resource

  on main.tf line 7, in provider "google":
   7:   credentials   = data.google_secret_manager_secret_version.secret

A data resource "google_secret_manager_secret_version" "secret" has not been
declared in the root module.
Error: Reference to undeclared input variable

  on datamodule\KeydataModule.tf line 3, in data "google_secret_manager_secret_version" "secret":
   3:   secret = "${var.Terra_Auth}"

An input variable with the name "Terra_Auth" has not been declared. This
variable can be declared with a variable "Terra_Auth" {} block.

With the following main.tf:

module "KeydataModule" {
  source = "./datamodule"

}

provider "google" {
  credentials   = data.google_secret_manager_secret_version.secret
  project       = "KubeProject"
  region        = "us-central1"
  zone          = "us-central1-c"
}

resource "google_compute_instance" "vm_instance" {
  name         = "terraform-instance"
  machine_type = "f1-micro"

  boot_disk {
    initialize_params {
      image = "ubuntu-cloud/ubuntu-1804-lts"
    }
  }

  network_interface {
    # A default network is created for all GCP projects
    network       = google_compute_network.vpc_network.self_link
    access_config {
    }
  }
}

resource "google_compute_network" "vpc_network" {
  name                    = "terraform-network"
  auto_create_subnetworks = "true"
}

The keydataModule.tf:

data "google_secret_manager_secret_version" "secret" {
  provider = google-beta
  secret = "${var.Terra_Auth}"
}

The following variables.tf for creating the 'Terra Auth' variable:

variable "Terra_Auth" {
   type = string
   description = "Access Key for Terraform Service Account" 
}

And finally a terraform.tfvars file, which in this case houses the secret name within my GCP account:

Terra_Auth = "Terraform_GCP_Account_Secret" 
BadLuckNick
  • 61
  • 1
  • 8
  • 2
    What problems are you having? Does it error? If so can you edit your question to include the full error output please? If it doesn't error but doesn't do what you want can you share what it does and how that doesn't do what you want please? – ydaetskcoR Apr 01 '20 at 14:27
  • 2
    Your module needs to output the value of the data source, then you can access it in the main.tf via module.KeydataModule.whatever_you_named_it. – sethvargo Apr 01 '20 at 14:29
  • Thanks @sethvargo . Would that mean the 'credentials' under 'provider' would be filled with the module.keydatamodule.whatever_i_name_it? :) – BadLuckNick Apr 01 '20 at 15:13
  • 1
    Terraform only has access to resources and data sources defined at the root. The "google_secret_manager_secret_version` resource is inside a module, so you cannot access it from the root. Your module needs to define an output with that value. – sethvargo Apr 01 '20 at 15:54
  • @sethvargo, got some time to retry this, still stuck. I referenced the data module in main.tf and put the credentials as: module.KeydataModule.PWsecret Referencing the output in the module: output "PWsecret" { value = google_secret_manager_secret_version.secret sensitive = true } The error still states 'secret' in google_secret_manager_version has not been declared. – BadLuckNick Apr 04 '20 at 10:18

0 Answers0