我使用terraform创建了一个GCP项目,但是它没有出现在项目列表中。为什么?

时间:2018-09-19 19:32:23

标签: google-cloud-platform google-compute-engine terraform terraform-provider-gcp

我正在按照https://cloud.google.com/community/tutorials/managing-gcp-projects-with-terraform

上的教程进行操作

基本上,它可以归结为创建一个拥有terraform-admin权限的roles/resourcemanager.projectCreator默认项目

$ gcloud organizations get-iam-policy $TF_VAR_org_id
bindings:
...snipped...
- members:
  - domain:raverun.com
  - serviceAccount:terraform@terraform-admin.iam.gserviceaccount.com
  role: roles/resourcemanager.projectCreator

我不会在此处复制project.tf文件,但主要内容是:

provider "google" {
    ...snipped...
}

resource "google_project" "proj" {
    ...snipped...
    project_id      = "tf-foobar"
}

resource "google_project_services" "svcs" {
     ...snipped...
}

运行terraform apply

之前
$ gcloud projects list
PROJECT_ID          NAME              PROJECT_NUMBER
terraform-admin     terraform-admin     300000000000

运行terraform apply

之后
$ terraform apply
...snipped...
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

Outputs:

project_id = tf-foobar

$ gcloud projects list
PROJECT_ID          NAME              PROJECT_NUMBER
terraform-admin     terraform-admin     300000000000

我的问题是为什么新项目tf-foobar没有显示?

1 个答案:

答案 0 :(得分:2)

我想我已经找到问题了。我需要提供域所有者的用户帐户  owner@mydomain.com一个项目角色,有权编辑新创建的项目。我尝试过:

resource "google_project_iam_binding" "projiambinding1" {
  project = "${google_project.proj.project_id}"
  role    = "roles/editor"

  members = [
    "user:owner@mydomain.com",
    "serviceAccount:xxxxxxxxx-compute@developer.gserviceaccount.com",
    "serviceAccount:yyyyyyyyyyyyy@cloudservices.gserviceaccount.com",
  ]
}

https://console.cloud.google.com/iam-admin/iam?authuser=1&project=tf-foobar&organizationId=xxxxxxxxxxx

现在,我新创建的项目现在可见:

$ gcloud projects list
PROJECT_ID          NAME                   PROJECT_NUMBER
terraform-admin     terraform-admin        300000000000
tf-foobar           tf-foobar              111111111111