Terraform GCP 项目创建

时间:2021-03-15 19:14:05

标签: google-cloud-platform terraform terraform-provider-gcp google-cloud-iam google-cloud-billing

我正在尝试使用 terraform 创建一个谷歌云项目。我指的是这个链接作为参考... https://femrtnz.medium.com/automating-gcp-projects-with-terraform-d571f0d94742

我遵循了媒体帖子中有关项目创建和 IAM 角色的说明。从表面上看,您需要一个单独的项目和服务帐户才能使用 terraform 创建项目。我还参考了有关该主题的 googles 文档... 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 博客文章的作者说了一些关于“首先你需要在你的域中创建一个组织” 我从来没有在我的谷歌项目中使用过组织。我进入我的谷歌控制台,它说我需要域验证才能为我的帐户获取一个组织。那看起来很麻烦。我真的不会为了这个而烦恼获得一个新域。现在我的代码正确吗?我假设错误是因为我没有“组织”。是否有一种无需域验证即可获得组织的简单方法?

3 个答案:

答案 0 :(得分:3)

错误 missing permission on "billingAccounts/1234-5678-9xxx": billing.resourceAssociations.create" 表示服务帐号无权将结算帐号关联到新项目。

  • 转到 Google Cloud Console 中的 Billing
  • 在窗口的右上角,点击“显示信息面板”。
  • 选择结算帐户,然后点击“添加成员”。
  • 输入服务帐户电子邮件地址。
  • 选择角色 Billing Account User
  • 点击保存。

服务帐号现在有权将结算帐号附加到新项目。

Overview of Cloud Billing access control

答案 1 :(得分:0)

我按照您说的做了,但是在点击 terraform apply 时出现新错误。

Error: error creating project test-project12345 (test-project): googleapi: Error 403: Service accounts cannot create projects without a parent., forbidden. If you received a 403 error, make sure you have the `roles/resourcemanager.projectCreator` permission

stackoverflow 上有一些关于它的东西...... Unable to create google project with Terraform

我做了一些谷歌搜索,显然有一个 roles/resourcemanager.projects.create。

现在,当我进入我的 Web 控制台并尝试将角色添加到服务帐户时,我发现它在下拉菜单中不可用。看来这个角色是可用的。我只是因为某种原因不能使用它!我可以用命令看到它..

gcloud iam roles describe roles/resourcemanager.projectCreator

##output##
description: Access to create new GCP projects.
etag: AA==
includedPermissions:
- resourcemanager.organizations.get
- resourcemanager.projects.create
name: roles/resourcemanager.projectCreator
stage: GA
title: Project Creator

当我尝试将角色添加到服务帐户时..

gcloud projects add-iam-policy-binding service-acct-12345 --member serviceAccount:service-acct-12345@service-acct-12345.iam.gserviceaccount.com --role roles/resourcemanager.projectCreator

## output

ERROR: Policy modification failed. For a binding with condition, run "gcloud alpha iam policies lint-condition" to identify issues in condition.
ERROR: (gcloud.projects.add-iam-policy-binding) INVALID_ARGUMENT: Role roles/resourcemanager.projectCreator is not supported for this resource.

这很令人困惑。感谢您对之前帖子的帮助

答案 2 :(得分:0)

按照上述建议分配计费管理员权限后,即可使用以下流程授予 Project Creator 角色。

转到 GCP Console 并搜索管理资源页面 (Google Console) --> 选择组织 --> 权限选项卡(右侧窗口)--> 添加成员 --> 将项目创建者角色分配给 Terraform 服务帐户(保存)

验证:您将在此页面右侧面板(资源管理器页面)的“角色/成员”部分下的“项目创建者”中看到 Terraform 服务帐户。