What if one small permission mistake could bring down your entire cloud setup?
Why Least privilege for Terraform service accounts? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a team managing cloud resources manually, and each person uses a shared account with full access to everything.
One day, someone accidentally deletes a critical server or changes settings they shouldn't.
Fixing this takes hours and causes downtime.
Using broad access for service accounts is risky and slow.
It's easy to make mistakes that affect many resources.
Tracking who did what becomes confusing.
Security risks increase because one compromised account can cause big damage.
Applying least privilege means giving Terraform service accounts only the exact permissions they need.
This limits mistakes and damage if something goes wrong.
It also helps track actions clearly and improves overall security.
resource "aws_iam_role" "terraform" { name = "terraform-full-access" assume_role_policy = "..." managed_policy_arns = ["arn:aws:iam::aws:policy/AdministratorAccess"] }
resource "aws_iam_role" "terraform" { name = "terraform-limited-access" assume_role_policy = "..." managed_policy_arns = ["arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess", "arn:aws:iam::aws:policy/AmazonEC2FullAccess"] }
It enables safer, clearer, and more reliable cloud infrastructure management with Terraform.
A company uses a Terraform service account that can only create and update storage buckets but cannot delete databases.
This prevents accidental data loss while allowing automation.
Manual broad access leads to mistakes and security risks.
Least privilege limits permissions to what is needed.
This improves safety, clarity, and control in cloud management.
Practice
Solution
Step 1: Understand least privilege concept
Least privilege means giving only the minimum permissions needed to perform a task.Step 2: Apply to Terraform service accounts
Terraform service accounts should have only the permissions required to manage infrastructure, nothing more.Final Answer:
Give only the permissions Terraform needs to do its job -> Option AQuick Check:
Least privilege = minimal needed permissions [OK]
- Giving Terraform full admin rights unnecessarily
- Sharing credentials widely
- Setting time-based access without need
Solution
Step 1: Identify the role for compute instance management
The role "roles/compute.admin" allows managing compute instances specifically.Step 2: Match the role to the service account in Terraform
The snippet assigns "roles/compute.admin" to the service account, limiting permissions to compute resources only.Final Answer:
The snippet assigning roles/compute.admin to the service account -> Option AQuick Check:
Assign specific roles, not broad ones [OK]
- Using broad roles like editor or admin unnecessarily
- Assigning unrelated roles like storage.admin
- Using viewer role which is read-only
resource "google_project_iam_member" "sa_role" {
project = "my-project"
role = "roles/storage.objectViewer"
member = "serviceAccount:terraform-sa@my-project.iam.gserviceaccount.com"
}Solution
Step 1: Understand the role assigned
The role "roles/storage.objectViewer" grants read-only access to storage objects.Step 2: Determine permission scope
This role does not allow writing or bucket management, only viewing objects.Final Answer:
Read-only access to storage objects only -> Option DQuick Check:
roles/storage.objectViewer = read-only object access [OK]
- Confusing viewer with admin or editor roles
- Assuming bucket write permissions
- Thinking full storage access is granted
resource "google_project_iam_member" "sa_role" {
project = var.project_id
role = "roles/compute.viewer"
member = "serviceAccount:${var.service_account_email}"
member = "serviceAccount:extra@domain.com"
}
What is the problem?Solution
Step 1: Check Terraform resource syntax
Terraform resource blocks cannot have duplicate keys; 'member' is repeated twice here.Step 2: Understand correct way to assign multiple members
To assign multiple members, use 'google_project_iam_binding' or multiple resources, not duplicate keys.Final Answer:
Duplicate 'member' keys cause a syntax error -> Option BQuick Check:
Duplicate keys in resource block = syntax error [OK]
- Using duplicate keys instead of lists or multiple resources
- Assuming role name is invalid without checking
- Ignoring variable definitions
Solution
Step 1: Identify the role for network management
The role 'roles/compute.networkAdmin' grants permissions to manage network resources only.Step 2: Apply least privilege principle
Assigning only this role limits the service account to network tasks, avoiding broad permissions.Step 3: Avoid broad or no permissions
Roles like 'editor' or 'owner' are too broad; no roles means no access.Final Answer:
Assign the role 'roles/compute.networkAdmin' to the service account only -> Option CQuick Check:
Least privilege = specific role only [OK]
- Using broad roles like editor or owner
- Not assigning any role and expecting access
- Assigning multiple unrelated roles
