> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Cloud IAM Roles and Permissions

> Guide to creating a custom Google Cloud IAM role, assigning it to a service account, generating JSON keys, and verifying Cloud Storage read permissions.

Hello and welcome back.

In this lesson we create a custom IAM role in Google Cloud, attach it to a service account, and verify the permissions. The primary steps are:

* Create a custom IAM role with specific Cloud Storage read permissions.
* Create a service account and attach the custom role.
* Generate and download a service account key (JSON) for external use.
* Verify access from Cloud Shell or a local client.

<Callout icon="lightbulb" color="#1CB2FE">
  This guide demonstrates the Console workflow and provides equivalent `gcloud` commands so you can automate or repeat the steps. Replace `PROJECT_ID`, `ROLE_ID`, `SA_NAME`, `SA_EMAIL`, `BUCKET`, and `OBJECT` with your actual values.
</Callout>

First, open the GCP Console and go to "IAM & Admin". You can reach it via the Quick access links or by searching the console search bar. Click the IAM entry to open the IAM page; this view shows principals and their assigned roles. Switch to the Roles tab to view custom and predefined roles.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/c09lTzXDcSD-W6MM/images/Google-Cloud-Professional-Data-Engineer-Certification/Identity-and-Access-Management-IAM-in-GCP/Demo-Cloud-IAM-Roles-and-Permissions/gcp-iam-kodekloud-permissions.jpg?fit=max&auto=format&n=c09lTzXDcSD-W6MM&q=85&s=d3f952151cbef78716340e741d2db4eb" alt="A screenshot of the Google Cloud Console IAM page showing permissions for the project &#x22;KodeKloud-GCP-Training,&#x22; with tabs to view roles and options to grant or remove access. The roles listed include Cloud Data Fusion Runner, Dataproc Worker, Editor, kodekloud.storage.viewer, and Owner." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/Identity-and-Access-Management-IAM-in-GCP/Demo-Cloud-IAM-Roles-and-Permissions/gcp-iam-kodekloud-permissions.jpg" />
</Frame>

## 1. Create a custom IAM role (Console)

* In the left-hand panel click **Roles**, then click **Create Role**.
* Enter a descriptive **Title** (example: `KodeKloudStorageViewer`), **Description** (example: `KodeKloudCustomRole for viewing storage bucket and object`), and an **ID** (lowercase, URL-safe string such as `kodekloudstoragerviewer1`) — the console may suggest an ID automatically.
* Choose the stage (e.g., General Availability) and proceed to add permissions.

When building a read-only storage role, add only the permissions required. For this demo we add these four read permissions:

| Permission             | Purpose                       |
| ---------------------- | ----------------------------- |
| `storage.buckets.list` | List buckets in a project     |
| `storage.buckets.get`  | Get bucket metadata           |
| `storage.objects.list` | List objects in a bucket      |
| `storage.objects.get`  | Read object data and metadata |

Search for each permission in the permissions selector and click **Add** for each.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/c09lTzXDcSD-W6MM/images/Google-Cloud-Professional-Data-Engineer-Certification/Identity-and-Access-Management-IAM-in-GCP/Demo-Cloud-IAM-Roles-and-Permissions/google-cloud-iam-kodekloud-storage-viewer.jpg?fit=max&auto=format&n=c09lTzXDcSD-W6MM&q=85&s=4de0392bba1b8bdb91a3766e6b85ad09" alt="A screenshot of the Google Cloud Console IAM &#x22;Create role&#x22; page with a custom role named &#x22;kodekloud.storage.viewer&#x22; being created. The form is populated and shows four selected storage permissions (storage.buckets.get, storage.buckets.list, storage.objects.get, storage.objects.list)." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/Identity-and-Access-Management-IAM-in-GCP/Demo-Cloud-IAM-Roles-and-Permissions/google-cloud-iam-kodekloud-storage-viewer.jpg" />
</Frame>

Important: these four permissions are read-only — they do not allow uploading, modifying, or deleting buckets or objects. For write or delete capabilities add `storage.objects.create`, `storage.objects.delete`, or `storage.buckets.create` as needed, or use a predefined role such as Storage Object Viewer / Storage Object Admin.

After adding permissions, click **Create**. If you previously created and deleted a similar role you may see multiple entries; ensure you pick the enabled role and use a unique ID when creating new roles.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/c09lTzXDcSD-W6MM/images/Google-Cloud-Professional-Data-Engineer-Certification/Identity-and-Access-Management-IAM-in-GCP/Demo-Cloud-IAM-Roles-and-Permissions/gcp-console-iam-roles-kodekloud.jpg?fit=max&auto=format&n=c09lTzXDcSD-W6MM&q=85&s=11ce1a77cbba473cc964f561ece3f60e" alt="A screenshot of the Google Cloud Console showing the IAM & Admin &#x22;Roles&#x22; page for the &#x22;KodeKloud-GCP-Training&#x22; project. It lists role names, where they're used, and their status (enabled/deleted) in a table." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/Identity-and-Access-Management-IAM-in-GCP/Demo-Cloud-IAM-Roles-and-Permissions/gcp-console-iam-roles-kodekloud.jpg" />
</Frame>

Alternative: create the same custom role with `gcloud` (example)

```bash theme={null}
gcloud iam roles create kodekloudstoragerviewer1 \
  --project=PROJECT_ID \
  --title="KodeKloudStorageViewer" \
  --description="KodeKloudCustomRole for viewing storage bucket and object" \
  --permissions="storage.buckets.list,storage.buckets.get,storage.objects.list,storage.objects.get" \
  --stage=GA
```

## 2. Attach the role to a user or service account

You can attach the custom role to a user via IAM -> Grant Access, or assign it to a service account. This demo creates a service account and assigns the custom role during creation.

* In the side panel select **Service Accounts** and click **Create Service Account**.
* Provide a **Name** (example: `kodekloud-test-sa`) — the Service account ID is auto-generated and editable — add a description, then click **Create and Continue**.
* On the Permissions step, search for your custom role (e.g., `kodekloud.storage.viewer` or your custom role ID) and assign it. If a role appears deleted or disabled you may get an error such as "Failed to add project roles" — choose the enabled version.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/c09lTzXDcSD-W6MM/images/Google-Cloud-Professional-Data-Engineer-Certification/Identity-and-Access-Management-IAM-in-GCP/Demo-Cloud-IAM-Roles-and-Permissions/gcp-create-service-account-permissions-failed.jpg?fit=max&auto=format&n=c09lTzXDcSD-W6MM&q=85&s=12ed50190fa5b79525d619ed870fa692" alt="A Google Cloud Console screenshot of the &#x22;Create service account&#x22; IAM page showing the Permissions step with the role &#x22;kodekloud.storage.viewer&#x22; selected. A red warning reads &#x22;Failed to add project roles&#x22; and buttons like Continue and Done are visible." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/Identity-and-Access-Management-IAM-in-GCP/Demo-Cloud-IAM-Roles-and-Permissions/gcp-create-service-account-permissions-failed.jpg" />
</Frame>

Finish creation and refresh the Service Accounts list — the new service account should now be visible.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/c09lTzXDcSD-W6MM/images/Google-Cloud-Professional-Data-Engineer-Certification/Identity-and-Access-Management-IAM-in-GCP/Demo-Cloud-IAM-Roles-and-Permissions/kodekloud-gcp-training-service-accounts.jpg?fit=max&auto=format&n=c09lTzXDcSD-W6MM&q=85&s=d6d836fe7095c56e04780b23e73388b6" alt="A screenshot of the Google Cloud Console showing the IAM & Admin &#x22;Service accounts&#x22; page for the project &#x22;KodeKloud-GCP-Training.&#x22; It displays a table of service account emails, their statuses (Enabled), names, descriptions, and OAuth2 client IDs." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/Identity-and-Access-Management-IAM-in-GCP/Demo-Cloud-IAM-Roles-and-Permissions/kodekloud-gcp-training-service-accounts.jpg" />
</Frame>

Equivalent `gcloud` commands to create the SA and bind the custom role:

```bash theme={null}
# Create a service account
gcloud iam service-accounts create kodekloud-test-sa \
  --display-name="KodeKloud Test Service Account" \
  --project=PROJECT_ID

# Grant the custom role to the service account
gcloud projects add-iam-policy-binding PROJECT_ID \
  --member="serviceAccount:kodekloud-test-sa@PROJECT_ID.iam.gserviceaccount.com" \
  --role="projects/PROJECT_ID/roles/kodekloudstoragerviewer1"
```

## 3. Create and download a service account key (JSON)

If you plan to use this service account from outside GCP (for local development, CI, or other systems), generate a key:

* Click the service account to open its details page.
* Select the **Keys** tab, click **Add Key -> Create new key**, choose **JSON**, and click **Create**.
* The JSON key will be downloaded to your machine.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/c09lTzXDcSD-W6MM/images/Google-Cloud-Professional-Data-Engineer-Certification/Identity-and-Access-Management-IAM-in-GCP/Demo-Cloud-IAM-Roles-and-Permissions/google-cloud-iam-key-json-download.jpg?fit=max&auto=format&n=c09lTzXDcSD-W6MM&q=85&s=24145e4e8c4bbc3406f62ad4f8371236" alt="A Google Cloud Console screenshot showing the IAM & Admin service accounts keys page with a popup saying &#x22;Private key saved to your computer&#x22; and a downloaded JSON key file. The page warns about security risks of service account keys and provides options to add keys." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/Identity-and-Access-Management-IAM-in-GCP/Demo-Cloud-IAM-Roles-and-Permissions/google-cloud-iam-key-json-download.jpg" />
</Frame>

You can also create a key using `gcloud`:

```bash theme={null}
gcloud iam service-accounts keys create key.json \
  --iam-account=kodekloud-test-sa@PROJECT_ID.iam.gserviceaccount.com \
  --project=PROJECT_ID
```

<Callout icon="warning" color="#FF6B6B">
  Service account JSON keys are long-lived credentials that grant access to your project. Treat these files like secrets: store them securely, rotate them regularly, and avoid committing them to source control. When possible prefer Workload Identity (for GKE), Workload Identity Federation, or short-lived tokens instead of downloading keys.
</Callout>

## 4. Verify permissions (Cloud Shell or local client)

To authenticate using the downloaded JSON key:

```bash theme={null}
# Option A: Activate the service account for gcloud
gcloud auth activate-service-account --key-file=key.json

# Option B: Set GOOGLE_APPLICATION_CREDENTIALS for SDKs and client libraries
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/key.json"
```

Test read-only access to Cloud Storage (examples):

```bash theme={null}
# List buckets (requires storage.buckets.list)
gsutil ls

# List objects in a bucket (requires storage.objects.list)
gsutil ls gs://BUCKET

# Read an object to stdout (requires storage.objects.get)
gsutil cat gs://BUCKET/OBJECT
```

If you attempt an action not covered by the custom role (for example, `gsutil cp` to upload or `gsutil rm` to delete), you'll receive an error indicating insufficient permissions. Use this to validate that the role is properly scoped as read-only.

## Wrap-up

You now have:

* A custom read-only Cloud Storage role,
* A service account with that role attached,
* A JSON key downloaded for external authentication,
* Commands to verify permissions locally or in Cloud Shell.

Next: test access in your application or CI environment using the JSON key or adopt Workload Identity / Workload Identity Federation for better security posture.

## Links and references

* [IAM roles and permissions overview (Google Cloud)](https://cloud.google.com/iam/docs/understanding-roles)
* [Creating and managing custom roles](https://cloud.google.com/iam/docs/creating-custom-roles)
* [Service accounts overview](https://cloud.google.com/iam/docs/service-accounts)
* [Best practices for service accounts and keys](https://cloud.google.com/iam/docs/best-practices-for-managing-service-account-keys)
* gsutil documentation: [https://cloud.google.com/storage/docs/gsutil](https://cloud.google.com/storage/docs/gsutil)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/google-cloud-professional-data-engineer-certification/module/9b9dd8e9-0075-430e-92db-757c9a6b738a/lesson/95badab2-e79b-40b1-a4fb-795d2df473e0" />
</CardGroup>
