0
0
GCPcloud~10 mins

IAM policy binding in GCP - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - IAM policy binding
Start: Define IAM Policy
Add Binding: Role + Member
Attach Policy to Resource
Resource Enforces Permissions
User Access Granted or Denied
This flow shows how an IAM policy is created, a role and member are bound, the policy is attached to a resource, and then permissions are enforced.
Execution Sample
GCP
resource = "projects/my-project"
policy = getIamPolicy(resource)
policy.bindings.append({"role": "roles/viewer", "members": ["user:alice@example.com"]})
setIamPolicy(resource, policy)
This code gets the current IAM policy for a project, adds a binding for a viewer role to a user, and updates the policy.
Process Table
StepActionPolicy Bindings BeforePolicy Bindings AfterResult
1Get current IAM policy[][]Policy fetched with no bindings
2Add binding: roles/viewer to user:alice@example.com[][{"role": "roles/viewer", "members": ["user:alice@example.com"]}]Binding added to policy
3Set updated IAM policy on resource[{"role": "roles/viewer", "members": ["user:alice@example.com"]}][{"role": "roles/viewer", "members": ["user:alice@example.com"]}]Policy updated on resource
4User alice tries to access resource[{"role": "roles/viewer", "members": ["user:alice@example.com"]}][{"role": "roles/viewer", "members": ["user:alice@example.com"]}]Access granted based on binding
5User bob tries to access resource[{"role": "roles/viewer", "members": ["user:alice@example.com"]}][{"role": "roles/viewer", "members": ["user:alice@example.com"]}]Access denied, no binding for bob
💡 Execution stops after policy is updated and access is enforced based on bindings.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
policy.bindings[][][{"role": "roles/viewer", "members": ["user:alice@example.com"]}][{"role": "roles/viewer", "members": ["user:alice@example.com"]}][{"role": "roles/viewer", "members": ["user:alice@example.com"]}]
Key Moments - 3 Insights
Why does user bob get denied access even though the policy has a binding?
Because the binding only includes user alice@example.com as a member for the viewer role. Bob is not listed in any binding (see execution_table step 5).
What happens if you add the same role with the same member twice?
The policy bindings list will have duplicate entries, but GCP treats them as one effective permission. However, best practice is to avoid duplicates (see execution_table step 2).
Why do we need to fetch the current policy before adding a binding?
Because IAM policies are full sets of bindings. You must get the current policy, modify it, then set it back to avoid overwriting existing bindings (see execution_table step 1 and 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the policy.bindings after adding the binding?
A[{"role": "roles/editor", "members": ["user:bob@example.com"]}]
B[{"role": "roles/viewer", "members": ["user:alice@example.com"]}]
C[]
Dnull
💡 Hint
Check the 'Policy Bindings After' column at step 2 in the execution_table.
At which step does the user alice get access granted?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Result' column for user alice's access in the execution_table.
If we add user bob to the same role binding at step 2, how would the policy.bindings change?
A[{"role": "roles/viewer", "members": ["user:alice@example.com", "user:bob@example.com"]}]
B[{"role": "roles/viewer", "members": ["user:bob@example.com"]}]
C[]
Dnull
💡 Hint
Consider how members list inside a binding updates when adding another user.
Concept Snapshot
IAM Policy Binding Quick Reference:
- IAM policy is a set of bindings.
- Each binding links a role to one or more members.
- To add a binding: fetch policy, add binding, update policy.
- Permissions are granted if user is in a binding for a role.
- Always avoid overwriting existing bindings unintentionally.
Full Transcript
IAM policy binding in Google Cloud means connecting a role to users or groups so they get permissions. The process starts by getting the current policy of a resource. Then you add a binding that says, for example, 'user alice@example.com has viewer role.' After updating the policy on the resource, when alice tries to access, she is allowed because of the binding. Others not listed, like bob, are denied. It's important to fetch the current policy before adding bindings to avoid losing existing permissions. Bindings are lists of roles and members. Adding the same member twice is redundant but allowed. This visual trace shows each step and how the policy changes, helping beginners understand how access control works in GCP.