google-cloud-platformvirtual-machineterraformhashicorp-vaultgoogle-provisioning-api

Provision a GCP VM instance with Static IP via Terraform


I have edited my main.tf and variable.tf file as suggested by @Claire Bellivier but still getting the same error, please have a look. Main.tf:

# Path to the authentification to GCP json file
provider "google" {
 credentials = "${file("${var.path_gcp_auth_json_file}")}"
 version     = "~> 2.2"

}

resource =  "google_compute_address" "test-static-ip-address" {
 count  = "${var.gcp_ip_count}"
 name   = "${var.gcp_project_id}-gke-ip-${count.index}"
 region = "${var.region}"
 }

resource "google_compute_instance" "tests" {
 name         = "project-tests"
 project      = "xyz"
 machine_type = "f1-micro"
 zone         = "us-west1-a"

 tags = ["gcp"]

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

network_interface {
 network = "default"

  access_config {
   nat_ip = "${google_compute_address.test-static-ip-address.address}"

   }
 }

  metadata {
   sshKeys = "local:${file(var.ssh_public_key_filepath)}"
  }

}

resource "google_compute_firewall" "firewalls" {
 name    = "firewalls"
 project = "video-library-228319"
 network = "default"

 allow {
  protocol = "tcp"
  ports = ["80", "443"]
 }

  source_ranges = ["0.0.0.0/0"]
}

Variable.tf

# Path to the authentification to GCP json file
variable "path_gcp_auth_json_file" {
  description = "Path to the authentication JSON file"
 default = "account.json"
}


variable "ssh_public_key_filepath" {
 description = "Filepath to local ssh public key"
 type = "string"

 default = "local.pub"
}

variable "gcp_ip_count" {
 default = "1"
}

variable "gcp_project_id" {
  default = "xyz"
}

variable "region" {
 default ="us-west1-a"
}

Error: Unknown root level key: test-static-ip-address Error: resource 'google_compute_instance.tests' config: unknown resource 'google_compute_address.test-static-ip-address' referenced in variable google_compute_address.test-static-ip-address.address

Please help


Solution

  • Could you copy paste this one and delete the second block?

    resource "google_compute_address" "test-static-ip-address" {
      count  = "${var.gcp_ip_count}"
      name   = "${var.gcp_project_id}-gke-ip-${count.index}"
      region = "${var.region}"
    }
    

    As mentioned there is a = too many, so it cannot work.

    The pattern is always for the main.tf file:

    resource "<kind of GCP Resource>" "<the name of your resources> {
      <list of arguments you need>
      # ...
    }
    

    A little trick if you need help with the Terraform syntax, you can proceed some tests with those commands: terraform format to get the proper indentation, and terraform validate to make sure everything is right in your code.