Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Attach Custom GCP Role to a GCP Service Account Using Terraform

I have created a service account and a custom role in GCP using Terraform. How do I attach this custom role to the service account? I could do this using GCP Console but that is not the need here as I have to do it using Terraform. Please find below the code snippets that I have used to create the service account and the custom rule.

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"]
}

If someone can find a Terraform based solution to solve this problem, it is highly appreciated. Thanks

like image 250
R Thottuvaikkatumana Avatar asked Mar 28 '19 14:03

R Thottuvaikkatumana


1 Answers

Using resource google_project_iam_binding

So the full code as below:

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}",
  ]
}
like image 176
BMW Avatar answered Sep 23 '22 03:09

BMW