IAM & Security
Users, roles, policies, and least-privilege access — the security foundation every AWS account must get right
Skills taught: aws.iam, aws.users, aws.roles, aws.policies, aws.least-privilege, aws.mfa
IAM — the gatekeeper of your AWS account
IAM (Identity and Access Management) is the service that controls who can do what in your AWS account. Every API call to AWS — whether from the console, CLI, or your application code — is checked against IAM policies before it's allowed. Getting IAM right is the single most important thing you can do for AWS security.
The four IAM concepts
1. USERS — A person or application with long-term credentials
- Has a username + password (for console access)
- Has an Access Key ID + Secret Access Key (for CLI/SDK access)
- Example: 'alice' (a developer), 'deploy-bot' (a CI/CD service account)
⚠ Never use root user for daily work — create IAM users instead
2. GROUPS — A collection of users with shared permissions
- 'Developers' group → can read/write S3 and EC2
- 'Admins' group → full access
- Assign a user to a group instead of attaching policies to each user
3. ROLES — A temporary identity that can be assumed
- An EC2 instance assumes a role to access S3 without storing credentials
- A Lambda function assumes a role to write to DynamoDB
- A GitHub Actions workflow assumes a role to deploy to ECS
✓ Preferred over users for application/service access — no long-term secrets
4. POLICIES — JSON documents that define permissions
- Attached to users, groups, or roles
- Allow or Deny specific actions on specific resources
Writing IAM policies
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::my-app-bucket",
"arn:aws:s3:::my-app-bucket/*"
]
}
]
}
// ARN format: arn:partition:service:region:account-id:resource
// arn:aws:s3:::my-app-bucket ← S3 bucket (no region/account — S3 is global)
// arn:aws:s3:::my-app-bucket/* ← all objects INSIDE the bucket
// Both lines needed: one for the bucket itself, one for objects inside
// Common actions:
// s3:GetObject, s3:PutObject, s3:DeleteObject, s3:ListBucket
// ec2:DescribeInstances, ec2:StartInstances, ec2:StopInstances
// lambda:InvokeFunction
// rds:DescribeDBInstances
// Use '*' for all actions (only for admin roles)
IAM Roles for applications — the right way
// 1. Create a role with a Trust Policy (who can assume this role):
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": { "Service": "ec2.amazonaws.com" },
"Action": "sts:AssumeRole"
}]
}
// This says: EC2 instances can assume this role
// 2. Attach a Permission Policy (what they can do):
{
"Statement": [{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::my-bucket/*"
}]
}
// 3. Attach the role to the EC2 instance at launch
// 4. In your application code — NO credentials needed:
import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
// SDK automatically uses the EC2 instance role credentials
const s3 = new S3Client({ region: 'us-east-1' });
const result = await s3.send(new GetObjectCommand({ Bucket: 'my-bucket', Key: 'file.txt' }));
// The SDK fetches temporary credentials from the EC2 metadata service
// (http://169.254.169.254/latest/meta-data/iam/security-credentials/)
// They auto-rotate every hour — no manual secret management needed
Least privilege — the golden rule
Grant only the minimum permissions needed to perform a task. A Lambda function that reads from DynamoDB should have dynamodb:GetItem and dynamodb:Query permissions on that specific table — not dynamodb:* on all tables, and definitely not *:* (full admin). This limits the blast radius if credentials are ever compromised.
# Day 1 AWS security checklist:
# 1. NEVER use root for daily work
# Root: the god account created when you sign up
# Create an IAM admin user for yourself, then don't touch root
# 2. Enable MFA on root (and all IAM users)
# AWS Console → IAM → Security credentials → MFA
# 3. Don't create root access keys
# If they exist, delete them immediately
# 4. Create IAM users for people, roles for services
# Never put AWS credentials in code or environment variables
# on servers — use IAM roles instead
# 5. Use IAM Access Analyzer to find over-permissive policies
aws accessanalyzer list-analyzers
# 6. Enable CloudTrail (audit log of all API calls)
# Free for management events — see who did what and when
aws cloudtrail describe-trails
Warning: Never commit AWS credentials to git. Access Key IDs and Secret Access Keys in a public GitHub repository are scraped by bots within minutes. The attacker spins up EC2 instances for crypto mining, and you get a $10,000 AWS bill. Use IAM roles for services, aws configure for local dev (stores in ~/.aws/credentials), and environment secrets for CI/CD. If you accidentally commit credentials, delete the key in IAM immediately, then rotate all affected secrets.
Frequently Asked Questions
What is the difference between IAM policies and S3 bucket policies?
IAM policies are attached to identities (users, roles) and say what that identity can do. S3 bucket policies are attached to the bucket and say who can access that bucket. They work together: both must allow for access to succeed (unless one is explicitly denied). Bucket policies are necessary for cross-account access or allowing public read access to a bucket.