Google cloud platform 通过Terraform为GCP VM实例提供静态IP

Google cloud platform 通过Terraform为GCP VM实例提供静态IP,google-cloud-platform,virtual-machine,terraform,hashicorp-vault,google-provisioning-api,Google Cloud Platform,Virtual Machine,Terraform,Hashicorp Vault,Google Provisioning Api,我已经按照@Claire Bellivier的建议编辑了main.tf和variable.tf文件,但仍然得到相同的错误,请看一看。 Main.tf: 变量.tf 错误:未知根级别密钥:测试静态ip地址 错误:在变量google\u compute\u address.test-static-ip-address.address中引用了资源'google\u compute\u instance.tests'配置:未知资源'google\u compute\u address.test静态ip

我已经按照@Claire Bellivier的建议编辑了main.tf和variable.tf文件,但仍然得到相同的错误,请看一看。 Main.tf:

变量.tf

错误:未知根级别密钥:测试静态ip地址 错误:在变量google\u compute\u address.test-static-ip-address.address中引用了资源'google\u compute\u instance.tests'配置:未知资源'google\u compute\u address.test静态ip address'


请帮助

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

# Configure the Google Cloud provider
provider "google" {
  credentials = "${file("${var.path_gcp_auth_json_file}")}"
  version     = "~> 2.2"
}
使用variables.tf文件

如果您希望快速且不将默认值添加到terraform.tfvars文件中

第二,您在测试资源的末尾遗漏了一个{:

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.tf。
计数值取决于您需要多少IP。希望能有所帮助。

您能复制粘贴此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}"
}
如前所述,a=太多,因此无法工作

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


如果您需要Terraform语法方面的帮助,您可以使用以下命令进行一些测试:Terraform format以获得正确的缩进,Terraform validate以确保代码中的所有内容都正确。

嗨,Claire,我已经按照您的建议编辑了我的帖子,但仍然收到相同的错误,请检查。
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}"
    }
  }
}
# 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}"
}
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 "<kind of GCP Resource>" "<the name of your resources> {
  <list of arguments you need>
  # ...
}