Overview
Identity and Access Management (IAM) is a critical component of Google Cloud Platform (GCP) that allows you to manage access to your resources securely. IAM enables you to define who (identity) has what access (role) to which resource.
Key Concepts
- Identities
Identities are entities that can be authenticated and authorized to access GCP resources. They include:
- Google Accounts: Individual user accounts.
- Service Accounts: Special accounts used by applications and virtual machines (VMs) to make authorized API calls.
- Google Groups: Collections of Google accounts and service accounts.
- Cloud Identity or G Suite domains: All the users in a domain.
- Roles
Roles are collections of permissions. They determine what actions an identity can perform on a resource. There are three types of roles:
- Primitive Roles: Basic roles that include Owner, Editor, and Viewer.
- Predefined Roles: Granular roles that provide more specific permissions.
- Custom Roles: User-defined roles that allow you to specify a custom set of permissions.
- Policies
Policies bind one or more members to a role. A policy is attached to a resource and defines who has what type of access to that resource.
- Permissions
Permissions determine what operations are allowed on a resource. Permissions are not assigned directly to users; they are assigned to roles, which are then granted to users.
Practical Example
Setting Up IAM Roles and Permissions
-
Navigate to the IAM & Admin Console:
- Go to the GCP Console.
- Select the project you want to manage.
- Navigate to "IAM & Admin" > "IAM".
-
Add a Member:
- Click on "Add".
- Enter the email address of the member you want to add.
- Select a role from the dropdown menu. For example, choose "Viewer" to grant read-only access.
-
Assign a Predefined Role:
- To assign a predefined role, select a role that matches the required permissions. For example, "Compute Engine Admin" for managing Compute Engine resources.
-
Create a Custom Role:
- Navigate to "IAM & Admin" > "Roles".
- Click on "Create Role".
- Provide a name, description, and specify the permissions you want to include in the role.
- Save the role and assign it to a member as described above.
Example Code: Assigning a Role Using gcloud CLI
# Assign the 'Viewer' role to a user gcloud projects add-iam-policy-binding [PROJECT_ID] \ --member='user:[email protected]' \ --role='roles/viewer'
Explanation:
[PROJECT_ID]
: Replace with your GCP project ID.user:[email protected]
: Replace with the email of the user you want to grant access to.roles/viewer
: The role you are assigning to the user.
Practical Exercise
Exercise: Assigning Roles to a Service Account
-
Create a Service Account:
- Navigate to "IAM & Admin" > "Service Accounts".
- Click on "Create Service Account".
- Provide a name and description for the service account.
- Click "Create".
-
Assign a Role to the Service Account:
- After creating the service account, click on the service account email.
- Click on "Add Key" > "Create New Key".
- Choose JSON and click "Create". Save the key file securely.
- Navigate back to "IAM".
- Click on "Add" and enter the service account email.
- Assign a role, such as "Storage Admin".
-
Verify the Permissions:
- Use the service account key file to authenticate and verify that the service account has the correct permissions.
Solution
# Create a service account gcloud iam service-accounts create my-service-account \ --description="My service account" \ --display-name="my-service-account" # Assign the 'Storage Admin' role to the service account gcloud projects add-iam-policy-binding [PROJECT_ID] \ --member='serviceAccount:my-service-account@[PROJECT_ID].iam.gserviceaccount.com' \ --role='roles/storage.admin'
Explanation:
my-service-account
: The name of the service account.[PROJECT_ID]
: Replace with your GCP project ID.roles/storage.admin
: The role you are assigning to the service account.
Common Mistakes and Tips
- Over-privileging: Avoid granting overly broad permissions. Use the principle of least privilege.
- Not using groups: Use Google Groups to manage permissions for multiple users efficiently.
- Neglecting service accounts: Ensure service accounts have only the necessary permissions and rotate keys regularly.
Conclusion
In this section, you learned about the fundamental concepts of IAM in GCP, including identities, roles, policies, and permissions. You also practiced assigning roles to users and service accounts using both the GCP Console and the gcloud CLI. Understanding and properly configuring IAM is crucial for maintaining the security and integrity of your GCP resources. In the next module, we will delve into networking and security, starting with VPC Networks.
Google Cloud Platform (GCP) Course
Module 1: Introduction to Google Cloud Platform
- What is Google Cloud Platform?
- Setting Up Your GCP Account
- GCP Console Overview
- Understanding Projects and Billing
Module 2: Core GCP Services
Module 3: Networking and Security
Module 4: Data and Analytics
Module 5: Machine Learning and AI
Module 6: DevOps and Monitoring
- Cloud Build
- Cloud Source Repositories
- Cloud Functions
- Stackdriver Monitoring
- Cloud Deployment Manager
Module 7: Advanced GCP Topics
- Hybrid and Multi-Cloud with Anthos
- Serverless Computing with Cloud Run
- Advanced Networking
- Security Best Practices
- Cost Management and Optimization