Identity and Access Management (IAM) in AWS

Controlling Who Can Do What, When, and Where in Your AWS Environment

AWS Identity and Access Management (IAM) is the cornerstone of security and access control in AWS. It lets you define, enforce, and audit permissions for users, services, and resources across your cloud infrastructure.

What is IAM?

IAM is a global AWS service that allows you to:

  • Create users, groups, and roles
  • Define policies to grant or deny permissions
  • Enable secure access for humans and machines
  • Use MFA, password policies, and temporary credentials

🧠 IAM follows the principle of least privilege: only give what’s necessary and nothing more.

Core Components of IAM

ComponentDescription
UsersIndividual AWS identities (email, developers, admins)
GroupsCollections of users sharing the same policies
RolesIdentities with temporary credentials assumed by users/services
PoliciesJSON documents defining allowed/denied actions
Trust PolicyWho can assume a role
MFAMulti-Factor Authentication (TOTP hardware/app-based)

🧍 IAM Users

  • Created for individual people needing AWS access
  • Can be assigned:
    • Console login
    • Programmatic access (access key/secret key)
  • Should never be root user for daily operations

βœ… Example:

Bash
aws iam create-user --user-name dev-user

πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦ IAM Groups

  • Logical way to apply the same permissions to multiple users
  • Can attach policies to the group instead of each user
  • Example groups: Admins, Developers, Auditors

πŸ‘€ IAM Roles

Roles are temporary, assumable identities used by:

  • EC2 instances
  • Lambda functions
  • Applications or services in other AWS accounts
  • Federated identities (SSO, external IdPs)

βœ… Role Assumption Examples:

  • Lambda uses a role to access S3
  • EC2 assumes a role to send logs to CloudWatch
  • AWS CodePipeline assumes a role to deploy via ECS

πŸ›‘οΈ IAM Policies – Granting and Restricting Access

Policies are written in JSON and attached to users, groups, or roles.

βœ… Policy Elements:

FieldPurpose
EffectAllow or Deny
ActionAWS operation (e.g., s3:PutObject)
ResourceARN of target resource
ConditionOptional logic (e.g., based on IP, tag, time)

βœ… Example Policy: Read-Only Access to S3

JSON
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": ["s3:GetObject", "s3:ListBucket"],
    "Resource": "*"
  }]
}

Managed vs Inline Policies

TypeDescription
AWS-ManagedPredefined by AWS (e.g., AmazonS3FullAccess)
Customer-ManagedYou create and manage policies
InlineEmbedded directly in a user/group/role (used sparingly)

🎯 Best Practice: Use customer-managed for flexibility; avoid inline unless necessary.

Trust Policies – Defining Who Can Assume Roles

Trust policies allow another AWS service, user, or account to assume the role.

βœ… Example: Allow EC2 to Assume Role

JSON
{
  "Effect": "Allow",
  "Principal": { "Service": "ec2.amazonaws.com" },
  "Action": "sts:AssumeRole"
}

Multi-Factor Authentication (MFA)

  • Adds an extra layer of security for IAM users
  • Can use Google Authenticator, Authy, or hardware tokens
  • Recommended for:
    • Root user
    • IAM admins
    • Billing access

βœ… Enable MFA via Console:

IAM β†’ Users β†’ Security Credentials β†’ MFA β†’ Assign MFA Device

βš™οΈ IAM Access Analyzer

  • Identifies public or cross-account access to resources
  • Useful for audits and security reviews
  • Integrated into AWS Console and CLI

βœ… Sample Findings:

  • S3 bucket open to public
  • KMS key shared with external account
  • IAM role trusted by multiple services

IAM Policy Simulator

  • Test how a policy behaves without deploying it
  • Check if a user can perform an action
  • Avoids security risks from misconfigured policies

πŸ§ͺ Try iam:SimulatePrincipalPolicy via CLI or Console.

Plaintext
            +----------------+
            |   IAM User     |
            +--------+-------+
                     |
             +-------v--------+
             |     Group      |
             +-------+--------+
                     |
             +-------v--------+      +---------------+
             |   Attached     |----->|  IAM Policy    |
             |   Permissions  |      +---------------+
                     |
             +-------v--------+
             |   IAM Role     |
             +-------+--------+
                     |
             +-------v--------+
             |  EC2, Lambda   |
             +----------------+

IAM Best Practices

PracticeReason
πŸ”’ Enable MFA for all usersPrevent compromised logins
🧾 Use IAM roles, not access keysTemporary credentials are safer
🧼 Rotate credentials regularlyReduces breach exposure
⚠️ Never use root for daily opsRisk of full account compromise
🧠 Apply least privilege principleMinimize potential damage
πŸ“¦ Tag IAM resources (Owner, Env)Easier auditing and billing
πŸ” Use IAM Access AnalyzerDetect unintended access
βœ… Review permissions quarterlyRemove unused permissions

πŸ” Real-World Use Cases

ScenarioIAM Solution
Web app writing to S3 bucketEC2 role with s3:PutObject
CI/CD pipeline deploying ECSCodeBuild role with ECS permissions
Federated login via SSOIAM Role + SAML Identity Provider
Cross-account resource sharingIAM Role with trust to another account
Audit log access controlIAM group with cloudtrail:DescribeTrails

Summary

ElementDescription
IAM UsersIndividual logins (human users)
IAM RolesTemporary identities (services, cross-account)
IAM GroupsApply common permissions to multiple users
PoliciesJSON permission rules
MFAExtra layer of login security
AnalyzerDetect overexposure of resources
SimulatorTest policy behavior without risk