📖 Scenario: You are managing access to a Google Cloud project for a small team. You want to control who can do what to keep your project safe and organized.
🎯 Goal: Build a simple IAM policy dictionary that assigns roles to team members, then add a condition to restrict access by time, and finally apply the policy to a resource.
📋 What You'll Learn
Create a dictionary called iam_policy with members and their roles
Add a condition to restrict access to business hours
Write a function to apply the IAM policy to a resource
💡 Why This Matters
🌍 Real World
Managing who can access and change cloud resources is critical for security and organization in real companies.
💼 Career
Understanding IAM policies and conditions is essential for cloud administrators and security engineers to protect cloud environments.
Progress0 / 4 steps
1
Create the initial IAM policy dictionary
Create a dictionary called iam_policy with these exact entries: 'user:alice@example.com': 'roles/viewer', 'user:bob@example.com': 'roles/editor', and 'user:carol@example.com': 'roles/owner'.
GCP
Hint
Use a Python dictionary with the exact keys and values given.
2
Add a time-based access condition
Add a variable called business_hours_condition that is a dictionary with keys 'title' set to 'Business Hours' and 'expression' set to 'request.time.hour >= 9 && request.time.hour <= 17'.
GCP
Hint
Create a dictionary with the exact keys and values for the condition.
3
Write a function to apply the IAM policy with condition
Write a function called apply_iam_policy that takes two parameters: resource and policy. Inside, create a dictionary called applied_policy with keys 'resource' set to resource, 'policy' set to policy, and 'condition' set to business_hours_condition. Return applied_policy.
GCP
Hint
Define the function with the exact name and parameters, then create and return the dictionary.
4
Apply the IAM policy to a resource
Create a variable called final_policy and set it to the result of calling apply_iam_policy with resource set to 'projects/my-sample-project' and policy set to iam_policy.
GCP
Hint
Call the function with the exact resource string and iam_policy variable.
Practice
(1/5)
1. What is the main purpose of advanced IAM in Google Cloud?
easy
A. To speed up cloud resource deployment
B. To reduce cloud storage costs automatically
C. To control who can access and modify cloud resources
D. To monitor network traffic in real-time
Solution
Step 1: Understand IAM's role in access control
IAM (Identity and Access Management) is designed to manage who can do what with cloud resources.
Step 2: Identify the main goal of advanced IAM
Advanced IAM focuses on precise control of permissions to protect resources and data.
Final Answer:
To control who can access and modify cloud resources -> Option C
Quick Check:
Advanced IAM = Access control [OK]
Hint: Think: IAM means managing who can do what [OK]
Common Mistakes:
Confusing IAM with cost management
Thinking IAM speeds up deployment
Mixing IAM with network monitoring
2. Which of the following is the correct way to assign a role to a user in GCP IAM policy JSON?
easy
A. "members": "roles/viewer", "role": ["user:alice@example.com"]
B. "role": "user:alice@example.com", "members": ["roles/viewer"]
C. "user": "roles/viewer", "member": "alice@example.com"
D. "members": ["user:alice@example.com"], "role": "roles/viewer"
Solution
Step 1: Recall correct IAM JSON structure
IAM bindings use "role" for the role name and "members" as a list of identities.
Step 2: Match correct syntax
"members": ["user:alice@example.com"], "role": "roles/viewer" correctly places the user in "members" and the role in "role" with proper formatting.
Final Answer:
"members": ["user:alice@example.com"], "role": "roles/viewer" -> Option D
Quick Check:
Role = roles/viewer, Members = user list [OK]
Hint: Remember: role is a string, members is a list [OK]