6

我已经使用 Terraform 在 GCP 中创建了一个服务帐户和一个自定义角色。如何将此自定义角色附加到服务帐户?我可以使用 GCP Console 来做到这一点,但这不是这里的需要,因为我必须使用 Terraform 来做到这一点。请在下面找到我用来创建服务帐户和自定义规则的代码片段。

resource "google_service_account" "mservice_infra_service_account" {
  account_id   = "mserviceinfra-service-account"
  display_name = "Infrastructure Service Account"
}

resource "google_project_iam_custom_role" "mservice_infra_admin" {
  role_id     = "mservice_infra_admin"
  title       = "mservice_infra_admin"
  description = "Infrastructure Administrator Custom Role"
  permissions = ["compute.disks.create", "compute.firewalls.create", "compute.firewalls.delete", "compute.firewalls.get", "compute.instanceGroupManagers.get", "compute.instances.create", "compute.instances.delete", "compute.instances.get", "compute.instances.setMetadata", "compute.instances.setServiceAccount", "compute.instances.setTags", "compute.machineTypes.get", "compute.networks.create", "compute.networks.delete", "compute.networks.get", "compute.networks.updatePolicy", "compute.subnetworks.create", "compute.subnetworks.delete", "compute.subnetworks.get", "compute.subnetworks.setPrivateIpGoogleAccess", "compute.subnetworks.update", "compute.subnetworks.use", "compute.subnetworks.useExternalIp", "compute.zones.get", "container.clusters.create", "container.clusters.delete", "container.clusters.get", "container.clusters.update", "container.operations.get"]
}

如果有人能找到基于 Terraform 的解决方案来解决这个问题,我们将不胜感激。谢谢

4

1 回答 1

8

使用资源google_project_iam_binding

所以完整的代码如下:

data "google_project" "project" {}

resource "google_service_account" "mservice_infra_service_account" {
  account_id   = "mserviceinfra-service-account"
  display_name = "Infrastructure Service Account"
}

resource "google_project_iam_custom_role" "mservice_infra_admin" {
  role_id     = "mservice_infra_admin"
  title       = "mservice_infra_admin"
  description = "Infrastructure Administrator Custom Role"
  permissions = ["compute.disks.create", "compute.firewalls.create", "compute.firewalls.delete", "compute.firewalls.get", "compute.instanceGroupManagers.get", "compute.instances.create", "compute.instances.delete", "compute.instances.get", "compute.instances.setMetadata", "compute.instances.setServiceAccount", "compute.instances.setTags", "compute.machineTypes.get", "compute.networks.create", "compute.networks.delete", "compute.networks.get", "compute.networks.updatePolicy", "compute.subnetworks.create", "compute.subnetworks.delete", "compute.subnetworks.get", "compute.subnetworks.setPrivateIpGoogleAccess", "compute.subnetworks.update", "compute.subnetworks.use", "compute.subnetworks.useExternalIp", "compute.zones.get", "container.clusters.create", "container.clusters.delete", "container.clusters.get", "container.clusters.update", "container.operations.get"]
}

resource "google_project_iam_binding" "mservice_infra_binding" {
  role = "projects/${data.google_project.project.project_id}/roles/${google_project_iam_custom_role.mservice_infra_admin.role_id}"

  members = [
    "serviceAccount:${google_service_account.mservice_infra_service_account.email}",
  ]
}
于 2019-03-29T00:05:25.703 回答