如何使用 google_compute_instance_from_template?

huangapple go评论65阅读模式
英文:

How to use google_compute_instance_from_template?

问题

这是我尝试使用Terraform的第一天,需要一些帮助。

我有一个GCP实例模板,我想在我的.tf文件中指定并创建一个实例。

resource "google_compute_instance_from_template" "tpl" {
  name = "k8-node-4-from-tf"
  source_instance_template = "k8-node"
  project = "myproject"
  zone = "us-west4-b"
}

当我应用时,我遇到了这个错误:

googleapi: Error 404: 未找到资源'projects/myproject/regions/us-west4/instanceTemplates/k8-node',notFound

我看到实例模板的self链接是这样的:

"selfLink": "projects/myproject/global/instanceTemplates/k8-node"

如何解决这个错误?在.tf中指定一个区域是强制性的。
当我尝试使用"global"作为区域时,会出现错误:区域"global"不存在。

英文:

This is my day 1 trying out terraform and needed some help.

I have a gcp instance template which I want to specify in my .tf and create an instance

resource "google_compute_instance_from_template" "tpl" {
  
  name = "k8-node-4-from-tf"
  source_instance_template = "k8-node"
  project = "myproject"
  zone = "us-west4-b"

}

I encountered this error when applying this

googleapi: Error 404: The resource 'projects/myproject/regions/us-west4/instanceTemplates/k8-node' was not found, notFound

The self link I see for the instance template is this,

 "selfLink": "projects/myproject/global/instanceTemplates/k8-node"

How can I resolve this error ?
Specifying a zone on .tf is mandatory.
When I try "global" for zone, this errors out : Zone global doesn't exist

答案1

得分: 2

"zone" 指的是 VM 将被创建的位置,而不是模板的位置。

因此,您可以将模板引用为:

resource "google_compute_instance_from_template" "tpl" {
  name                    = "k8-node-4-from-tf"
  source_instance_template = "projects/myproject/global/instanceTemplates/k8-node"
  project                 = "myproject"
  zone                    = "us-west4-b"
}

或者如果模板在您的 TF 文件中定义:

resource "google_compute_instance_from_template" "tpl" {
  name                    = "k8-node-4-from-tf"
  source_instance_template = google_compute_instance_template.<your_instance_template_name>.self_link_unique
  project                 = "myproject"
  zone                    = "us-west4-b"
}

文档中包含了所有信息,因此当您开始使用新东西时,强烈建议您首先阅读文档,因为大多数情况下,您会在那里找到示例。

英文:

The zone refers to where the VM will be created, not where the template is.

So you can refer to the template as:

resource &quot;google_compute_instance_from_template&quot; &quot;tpl&quot; {
  
  name = &quot;k8-node-4-from-tf&quot;
  source_instance_template = &quot;projects/myproject/global/instanceTemplates/k8-node&quot;
  project = &quot;myproject&quot;
  zone = &quot;us-west4-b&quot;

}

or if the template is defined in your TF files:

resource &quot;google_compute_instance_from_template&quot; &quot;tpl&quot; {
  
  name = &quot;k8-node-4-from-tf&quot;
  source_instance_template = google_compute_instance_template.&lt;your_instance_template_name&gt;.self_link_unique
  project = &quot;myproject&quot;
  zone = &quot;us-west4-b&quot;

}

Everything is in the documentation so when you start with something new, it's highly recommended that you read that first since most of the time you'll find samples there.

huangapple
  • 本文由 发表于 2023年6月1日 20:57:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定