简体   繁体   English

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

[英]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. 我已经按照@ Claire Bellivier的建议编辑了main.tf和variable.tf文件,但仍然收到相同的错误,请看看。 Main.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"
}

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 错误:未知的根级密钥:test-static-ip-address错误:资源'google_compute_instance.tests'配置:变量google_compute_address.test-static-ip-address中引用的未知资源'google_compute_address.test-static-ip-address'。地址

Please help 请帮忙

First of all, you can try to configure the Google Cloud provider like that: 首先,您可以尝试像这样配置Google Cloud提供商:

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

With a variables.tf file 使用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"
}

if you want to be quick and do not add you default values to a terraform.tfvars file. 如果您想快速操作并且不将default值添加到terraform.tfvars文件中。

Secondly, you missed a { in the end of the tests resource: 其次,您在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}"
    }
  }
}

And then, to generat IPs you need to properly declare the compute resource to Terraform: 然后,要生成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}"
}

Each "${var.[...] need to be refered to the variables.tf previously mentionned. The count value depends on how many IPs you need. Hope it'll help. 每个"${var.[...]需要引用到前面提到的variables.tf 。tf。 count数值取决于您需要的IP数量。希望对您有所帮助。

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: 该模式始终用于main.tf文件:

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. 如果您需要有关Terraform语法的帮助,可以使用一些技巧,可以使用这些命令进行一些测试: terraform format以获取正确的缩进,而terraform validate以确保所有内容在您的代码中都是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM