All IAM code samples

This page contains code samples for Identity and Access Management. To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.

Add a new role binding

Demonstrates adding a new role binding to an IAM policy.

Add a principal to a role binding

Demonstrates adding a principal to an existing role binding in an IAM policy.

Create a custom role

Demonstrates creating a custom role.

View in documentation

Create a deny policy

Creates a deny policy based on the configuration provided.

View in documentation

Create a service account

Demonstrates creating a service account.

View in documentation

Create a service account key

Demonstrates creating a service account key.

Create a token for workload identity federation with AWS

Creates a token that IAM workload identity federation can verify without revealing the AWS secret access key.

Create project-level service agents

Create all project-level service agents for an API.

Delete a custom role

Demonstrates deleting a custom role.

View in documentation

Delete a deny policy

Delete the policy if you no longer want to enforce the rules in a deny policy.

View in documentation

Delete a service account

Demonstrates deleting a service account.

View in documentation

Delete a service account key

Demonstrates deleting a service account key.

Disable a custom role

Demonstrates disabling a custom role.

Disable a service account

Demonstrates disabling a service account.

View in documentation

Disable a service account key

Demonstrates disabling an IAM service account key.

Edit a custom role

Demonstrates editing a custom role.

View in documentation

Enable a service account

Demonstrates enabling a service account.

View in documentation

Enable a service account key

Demonstrates enabling a disabled IAM service account key.

Get a deny policy

Retrieve information about a deny policy.

View in documentation

Get a role

Demonstrates retrieving a role.

View in documentation

Get a service account

Demonstrates retrieving a service account.

Get a service account key

Demonstrates retrieving a service account key.

Get an IAM policy

Demonstrates retrieving an IAM policy.

List deny policies

List the deny policies in the resource provided.

View in documentation

List roles

Demonstrates listing roles.

View in documentation

List service account keys

Demonstrates listing service account keys.

View in documentation

List service accounts

Demonstrates listing service accounts.

View in documentation

Query testable permissions

Demonstrates listing the permissions that are valid for a resource.

View in documentation

Quickstart

Demonstrates basic usage of IAM.

View in documentation

Remove a member from a role binding

Demonstrates removing a member from a role binding in an IAM policy.

Rename a service account

Demonstrates renaming a service account.

View in documentation

Restore a deleted custom role

Demonstrates restoring a custom role.

View in documentation

Test permissions

Demonstrates testing whether the caller has specified permissions.

Update a deny policy

Update the deny rules and/ or its display name after policy creation.

View in documentation

Update a policy

Demonstrates updating a policy.

View grantable roles

Demonstrates viewing the roles that you can grant on a specific resource.