通过 terraform 创建 gcp vm 实例时出错

时间:2021-07-18 15:15:08

标签: google-cloud-platform terraform virtual-machine terraform-provider-gcp

服务帐户拥有创建 vm 实例的所有权限(服务帐户用户、项目所有者、项目编辑器)。当我运行 terraform 时,会发生这种情况:

│ Error: Error waiting for instance to create: The user does not have access to service account 'mymail@gmail.com'.  User: 'terraform@direct-keel-275713.iam.gserviceaccount.com'.  Ask a project owner to grant you the iam.serviceAccountUser role on the service account
│ 
│ 
│   with module.vm.google_compute_instance.icinga,
│   on modules/vm/main.tf line 23, in resource "google_compute_instance" "icinga":
│   23: resource "google_compute_instance" "icinga" {

enter image description here

1 个答案:

答案 0 :(得分:3)

您的 Terraform 设置方式有问题。

错误消息包含文本用户无权访问服务帐户“mymail@gmail.com”。

身份 mymail@gmail.com 不是服务帐户。

正确设置凭据后,Terraform 用于授权的身份必须具有 roles/iam.serviceAccountUser 或类似角色。您选择的角色必须具有 iam.serviceAccounts.actAs 权限。

Service Accounts Roles

注意:roles/compute.admin 等角色没有 iam.serviceAccounts.actAs 权限。

Terraform 默认会查找环境变量 GOOGLE_APPLICATION_CREDENTIALS。该变量应指向服务帐号 JSON 密钥文件的完整路径。

接下来,Terraform 将查找由 gcloud auth 应用程序默认登录创建的 CLI/SDK 凭据。

我更喜欢在 Terraform HCL 中指定服务帐户(通常在变量文件中)。

provider "google" {
  project = "PROJECT_ID"
  credentials = "/path/to/service-account.json"
}