通过 Terraform 使用静态 IP 配置 GCP VM 实例

Provision a GCP VM instance with Static IP via Terraform

我已经按照@Claire Bellivier的建议编辑了我的main.tf和variable.tf文件,但仍然出现同样的错误,请看一下。 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"
}

错误:未知的根级别密钥:test-static-ip-address 错误:资源 'google_compute_instance.tests' 配置:未知资源 'google_compute_address.test-static-ip-address' 在变量 google_compute_address.test-static-ip-address.address

中引用

请帮忙

首先,您可以尝试像这样配置 Google 云提供商:

# Configure the Google Cloud provider
provider "google" {
  credentials = "${file("${var.path_gcp_auth_json_file}")}"
  version     = "~> 2.2"
}

variables.tf个文件

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

如果您想快速完成并且不将 default 值添加到 terraform.tfvars 文件。

其次,您错过了 tests 资源末尾的 {

resource "google_compute_instance" "tests" {
  name         = "project-tests"
  project      = "video-library-228319"
  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}"
    }
  }
}

然后,要生成 IP,您需要向 Terraform 正确声明计算资源:

# Generate IPs
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}"
}

每个"${var.[...]需要参考前面提到的variables.tfcount 值取决于您需要多少个 IP。希望对你有帮助。

你能复制粘贴这个并删除第二块吗?

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}"
}

如前所述,= 太多,因此无法正常工作。

模式始终用于 main.tf 文件:

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

一个小技巧,如果您需要 Terraform 语法方面的帮助,您可以使用这些命令进行一些测试:terraform format 以获得正确的缩进,terraform validate 以确保一切正确你的代码。