【发布时间】:2026-01-24 05:35:01
【问题描述】:
我正在尝试使用 terraform 创建一个谷歌云项目。我将此链接作为参考... https://femrtnz.medium.com/automating-gcp-projects-with-terraform-d571f0d94742
我遵循了媒体帖子中关于项目创建和 IAM 角色的说明。从看起来你需要一个单独的项目和服务帐户来创建带有 terraform 的项目。我还参考了有关该主题的谷歌文档... https://cloud.google.com/community/tutorials/managing-gcp-projects-with-terraform
所以我最终在 main.tf 中得到了这个
# This is the provider used to spin up the gcloud instance
provider "google" {
credentials = "/path/to/seed-credentials.json"
}
# Locks the version of Terraform for this particular use case
terraform {
required_version = "0.14.6"
}
resource "random_id" "id" {
byte_length = 4
prefix = var.project_name
}
resource "google_project" "project" {
name = var.project_name
project_id = random_id.id.hex
billing_account = var.billing_account
}
output "project_id" {
value = google_project.project.project_id
}
我创建了一个远程后端
terraform {
backend "gcs" {
bucket = "seed-bucket"
prefix = "terraform/state"
credentials = "/path/to/seed-credentials.json"
}
}
这是我的 variables.tf 文件
variable "project_name" {
type = string
}
variable "billing_account" {
type = string
}
最后但并非最不重要的是我的 terraform.tfvars
project_name = "test-project"
billing_account = "1234-5678-90xxx"
Terraform init 可以配置远程后端。 Terraform 计划没有给我任何错误。但是,当我运行 terraform apply 时,我收到以下错误“错误:先决条件失败:缺少对“billingAccounts/1234-5678-9xxx”的权限:billing.resourceAssociations.create” 现在我没有此帐户的组织。我假设这就是给我错误的原因? Medium 博客文章的作者说了一些关于“首先你需要在你的域中创建一个组织” 我从来没有在我的谷歌项目中使用过组织。我进入我的谷歌控制台,它说我需要域验证才能为我的帐户获取组织。这似乎很麻烦。我真的不想为此费心去获得一个新的域名。现在我的代码正确吗?我假设错误是因为我没有“组织”。有没有一种简单的方法可以让组织无需域验证?
【问题讨论】:
标签: google-cloud-platform terraform terraform-provider-gcp google-cloud-iam google-cloud-billing