0Pricing
Cloud & IT Cert Prep · Lesson

Cloud Identity: IAM Roles and Service Accounts

Configure least-privilege IAM roles and service accounts in cloud platforms and avoid common mistakes like wildcard permissions and long-lived keys.

Cloud Identity: IAM Roles and Service Accounts is a free Cloud & IT Cert Prep lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cloud & IT Cert Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Cloud Identity Fundamentals

In cloud environments, identity is the new perimeter. Every action — starting a VM, reading a database, calling an API — is authorized based on the calling identity. Cloud IAM (Identity and Access Management) systems define who can do what on which resources. Unlike on-premises environments where network location provided implicit trust, cloud IAM treats every request as requiring explicit authorization regardless of where it originates.

Users, Groups, and Roles in AWS IAM

AWS IAM has three primary identity types. IAM Users represent individual humans or applications with long-term credentials (access key + secret key). IAM Groups bundle users and assign shared permissions. IAM Roles are identities with temporary credentials that can be assumed by users, AWS services (EC2, Lambda), or other accounts. Roles are preferred over long-term access keys because their credentials expire automatically, reducing the risk of credential exposure.

# IAM role trust policy — allows EC2 to assume this role
{
  'Version': '2012-10-17',
  'Statement': [{
    'Effect': 'Allow',
    'Principal': { 'Service': 'ec2.amazonaws.com' },
    'Action': 'sts:AssumeRole'
  }]
}

# EC2 instance with this role attached can call AWS APIs
# using temporary credentials from the instance metadata service

Least Privilege in IAM Policies

IAM policies define what actions an identity may perform on which resources. The least privilege principle demands that policies grant only the specific actions needed for the task. Common violations: using * wildcards for actions (grants all actions in a service), using * for resources (grants access to all resources), and attaching overly broad managed policies like AdministratorAccess to service accounts. Every wildcard should be justified and regularly reviewed.

# Overly permissive policy (AVOID)
{
  'Effect': 'Allow',
  'Action': 's3:*',      # all S3 actions
  'Resource': '*'         # all buckets
}

# Least-privilege policy (PREFERRED)
{
  'Effect': 'Allow',
  'Action': ['s3:GetObject', 's3:ListBucket'],
  'Resource': [
    'arn:aws:s3:::my-specific-bucket',
    'arn:aws:s3:::my-specific-bucket/*'
  ]
}

Service Accounts in GCP

In Google Cloud Platform (GCP), non-human workloads authenticate using service accounts — managed identity entities with JSON key files or Workload Identity Federation. Each service account should follow least privilege: bind it only to the GCP services it needs to call. Service account keys (JSON files downloaded from the console) are long-lived credentials that must be treated like passwords — rotated regularly and never committed to source code or uploaded to public repositories.

# Check service account permissions (gcloud)
gcloud projects get-iam-policy my-project \
  --flatten='bindings[].members' \
  --format='table(bindings.role, bindings.members)' \
  --filter='bindings.members:serviceAccount'

# Prefer Workload Identity over service account keys
# (no downloadable key files — uses workload federation tokens)

Azure Managed Identities

Azure Managed Identities (formerly MSI) are the Azure equivalent of AWS IAM roles for services — they allow Azure resources (VMs, App Services, Functions) to authenticate to Azure APIs without storing credentials. There are two types: System-assigned managed identities are tied to a specific resource and deleted when the resource is deleted. User-assigned managed identities are standalone objects that can be shared across multiple resources. Managed identities eliminate the need for any stored keys or secrets.

# Azure CLI — assign managed identity to a VM
az vm identity assign \
  --name myVM \
  --resource-group myRG \
  --identities /subscriptions/.../userAssignedIdentities/myIdentity

# The VM can now call Azure Key Vault without any stored credentials:
# Token is fetched automatically from the Instance Metadata Service

Long-Lived Credentials: The Risk

Long-lived credentials — static access keys, API tokens, and service account key files that never expire — are one of the highest-risk elements in cloud environments. If leaked (via GitHub, S3 bucket, logs, or a compromised developer laptop), these credentials grant immediate access until manually revoked. Organizations should: audit all long-lived credentials, rotate them on a schedule, prefer role-based or federated access that produces short-lived tokens, and alert immediately when credentials appear in public repositories.

# Find IAM access keys older than 90 days (AWS)
aws iam generate-credential-report
aws iam get-credential-report --query 'Content' --output text | \
  base64 -d | grep -v 'N/A' | \
  awk -F',' '$10 > 90 {print $1, $10}'

# Keys older than 90 days should be rotated or deleted

IAM Role Chaining and Privilege Escalation

IAM privilege escalation occurs when an identity uses a combination of permissions to grant itself additional permissions. Classic escalation paths include: attaching a more permissive policy to your own user, creating a new IAM user with elevated permissions, passing a role (iam:PassRole) to a service, and updating a Lambda function's execution role. AWS's IAM Access Analyzer can detect these patterns, and IAM permission boundaries can hard-limit the maximum permissions any identity can be granted.

# Dangerous permission combination (enables privilege escalation):
# iam:CreatePolicyVersion + iam:SetDefaultPolicyVersion
# Attacker can create a new policy version with AdministratorAccess

# Or: iam:PassRole + lambda:CreateFunction + lambda:InvokeFunction
# Attacker creates Lambda with a privileged role, invokes it

# Defense: permission boundaries limit maximum grantable permissions

Cross-Account Role Assumption

Cloud organizations often use multiple accounts (dev, staging, prod, security) as blast-radius boundaries. Cross-account role assumption allows identities in one account to assume roles in another — enabling centralized tooling to operate across accounts. Security controls include: requiring an External ID in the trust policy to prevent confused deputy attacks, restricting which accounts can assume a role via the Principal ARN, and logging all cross-account assumptions in CloudTrail for audit purposes.

# Trust policy with External ID (confused deputy protection)
{
  'Effect': 'Allow',
  'Principal': { 'AWS': 'arn:aws:iam::PARTNER-ACCOUNT-ID:root' },
  'Action': 'sts:AssumeRole',
  'Condition': {
    'StringEquals': {
      'sts:ExternalId': 'unique-shared-secret-12345'
    }
  }
}

IMDS and Metadata Service Security

AWS EC2 instances can retrieve their IAM role credentials from the Instance Metadata Service (IMDS) at http://169.254.169.254. The SSRF vulnerability class is particularly dangerous here: if an application is vulnerable to SSRF, an attacker can exfiltrate the instance's IAM role credentials by making the server fetch from the IMDS URL. IMDSv2 (requiring a session token) mitigates SSRF-based credential theft and should be enforced on all EC2 instances.

# Enforce IMDSv2 on a new EC2 instance (requires token for IMDS)
aws ec2 run-instances \
  --metadata-options 'HttpTokens=required,HttpEndpoint=enabled' \
  ...

# IMDSv1 (insecure) just needs a GET request:
# curl http://169.254.169.254/latest/meta-data/iam/security-credentials/
# IMDSv2 requires a PUT to get a session token first

IAM Access Analyzer and Policy Review

IAM Access Analyzer (AWS) automatically identifies resources shared with external principals and IAM policies that grant more than intended. It analyzes bucket policies, role trust policies, and KMS key policies to flag external access that was not explicitly intended. Regular IAM policy reviews — manually or with tools like Cloudsplaining, PMapper, or Permissions Boundary Analyzer — are essential to identifying privilege escalation paths before attackers find them.

Workload Identity Federation

Workload Identity Federation allows external workloads (GitHub Actions, on-premises systems, other cloud providers) to authenticate to cloud IAM using short-lived OIDC tokens instead of long-lived service account keys. A GitHub Actions workflow can assume an AWS IAM role using its OIDC token for the duration of the job, then the token expires. This approach eliminates the entire class of long-lived credential leakage from CI/CD pipelines.

Quick Check

Test your understanding of CompTIA Security+ (SY0-701) concepts from this lesson.

Lesson Recap

In this lesson you learned: IAM roles provide temporary credentials and are preferred over long-lived access keys for cloud workloads, least-privilege policies should avoid wildcards and grant only specific actions on specific resources, and IMDSv2, permission boundaries, and workload identity federation eliminate common credential exposure paths. Next up we explore Cloud Security Posture Management (CSPM).

Frequently asked questions

Is the “Cloud Identity: IAM Roles and Service Accounts” lesson free?

Yes — the full text of “Cloud Identity: IAM Roles and Service Accounts” is free to read here on the web, and the Cloud & IT Cert Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cloud & IT Cert Prep course, upgrade to CoddyKit PRO.

What will I learn in “Cloud Identity: IAM Roles and Service Accounts”?

Configure least-privilege IAM roles and service accounts in cloud platforms and avoid common mistakes like wildcard permissions and long-lived keys. You practise Cloud & IT Cert Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Cloud & IT Cert Prep?

No prior experience is required. Cloud & IT Cert Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Cloud Identity: IAM Roles and Service Accounts” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Cloud & IT Cert Prep lesson?

Yes. Every Cloud & IT Cert Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Shared Responsibility Model: IaaS, PaaS, SaaS
  2. Cloud Storage Security and Data Exposure Risks
  3. Cloud Identity: IAM Roles and Service Accounts
  4. Cloud Security Posture Management (CSPM)
← Back to Cloud & IT Cert Prep