IAM Best Practices
IAM mistakes cause more cloud security incidents than anything else. The good news: a short list of habits prevents most of them.
| Practice | Why |
|---|---|
| Lock away the root user, enable MFA on it | Root can do anything and can't be restricted by IAM policies |
| Use temporary credentials (Identity Center, roles) | Nothing long-lived to leak |
| Require MFA for people | A stolen password alone is not enough |
| Grant least privilege | Each identity gets only the permissions it needs |
| Rotate or remove unused access keys | Old keys are forgotten keys |
| Use groups (or permission sets), not per-user policies | Easier to review and change |
| Use conditions for extra guard rails | e.g. allow only from the company network |
| Review regularly: IAM Access Analyzer, last-accessed data, credential report | Permissions grow over time |
The credential report
AWS can generate a CSV report of every user and the state of their credentials:
aws iam generate-credential-report
aws iam get-credential-report --query Content --output text | base64 --decode > report.csvSample output (first lines of report.csv, columns shortened):
user,password_enabled,mfa_active,access_key_1_active,access_key_1_last_rotated
<root_account>,not_supported,true,false,N/A
grace,true,true,false,N/A
linus,true,false,true,2025-04-11T09:30:00+00:00
build-bot,false,false,true,2024-11-02T14:00:00+00:00Model: auditing the credential report
This audit reads the CSV (inlined here) and flags common problems: console users without MFA, active keys older than 90 days, and root access keys.
import csv
import io
from datetime import date
REPORT = """user,password_enabled,mfa_active,access_key_1_active,access_key_1_last_rotated
<root_account>,not_supported,true,false,N/A
grace,true,true,false,N/A
linus,true,false,true,2025-04-11
build-bot,false,false,true,2024-11-02
mia,true,true,true,2026-02-20
"""
today = date(2026, 3, 1)
for row in csv.DictReader(io.StringIO(REPORT)):
problems = []
if row["user"] == "<root_account>" and row["access_key_1_active"] == "true":
problems.append("root has an access key")
if row["password_enabled"] == "true" and row["mfa_active"] != "true":
problems.append("console access without MFA")
if row["access_key_1_active"] == "true":
age = (today - date.fromisoformat(row["access_key_1_last_rotated"])).days
if age > 90:
problems.append(f"access key is {age} days old")
print(f"{row['user']:<15} {'; '.join(problems) or 'ok'}")<root_account> ok
grace ok
linus console access without MFA; access key is 324 days old
build-bot access key is 484 days old
mia ok
Model: least privilege from usage
IAM records when each service was last accessed by an identity. If a role is allowed to use ten services but only used two in the last 90 days, the other eight are candidates for removal. This model compares granted and used actions:
granted = {
"s3:GetObject", "s3:PutObject", "s3:DeleteObject", "s3:ListBucket",
"dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:DeleteTable",
"sqs:SendMessage", "iam:PassRole",
}
used_last_90_days = {"s3:GetObject", "s3:ListBucket", "dynamodb:GetItem", "sqs:SendMessage"}
unused = sorted(granted - used_last_90_days)
risky = {"s3:DeleteObject", "dynamodb:DeleteTable", "iam:PassRole"}
print(f"granted {len(granted)}, used {len(used_last_90_days)}, unused {len(unused)}")
for action in unused:
flag = " <- high risk" if action in risky else ""
print(f" remove? {action}{flag}")
print(f"privilege used: {len(used_last_90_days) / len(granted):.0%}")granted 9, used 4, unused 5
remove? dynamodb:DeleteTable <- high risk
remove? dynamodb:PutItem
remove? iam:PassRole <- high risk
remove? s3:DeleteObject <- high risk
remove? s3:PutObject
privilege used: 44%
Note: Don't remove permissions blindly: a quarterly batch job may not have run in the last 90 days. Look at the access history first, then tighten.
Password policy
For IAM users who sign in to the console, set an account password policy:
aws iam update-account-password-policy --minimum-password-length 14 \
--require-symbols --require-numbers --require-uppercase-characters \
--require-lowercase-characters --password-reuse-prevention 5import string
POLICY = {"min_length": 14, "symbols": True, "numbers": True, "upper": True, "lower": True}
def check_password(pw):
errors = []
if len(pw) < POLICY["min_length"]:
errors.append(f"shorter than {POLICY['min_length']}")
if POLICY["symbols"] and not any(c in string.punctuation for c in pw):
errors.append("no symbol")
if POLICY["numbers"] and not any(c.isdigit() for c in pw):
errors.append("no number")
if POLICY["upper"] and not any(c.isupper() for c in pw):
errors.append("no uppercase")
if POLICY["lower"] and not any(c.islower() for c in pw):
errors.append("no lowercase")
return errors or ["ok"]
for pw in ["summer2026", "Correct-Horse-7-Battery", "ALLCAPS-AND-123456"]:
print(f"{pw:<25} {', '.join(check_password(pw))}")summer2026 shorter than 14, no symbol, no uppercase
Correct-Horse-7-Battery ok
ALLCAPS-AND-123456 no lowercase
Test yourself
What does the principle of least privilege mean?
- Give everyone admin rights to save time
- Grant only the permissions an identity needs for its job
- Use the fewest IAM users possible
- Deny all access
Which report lists every IAM user with their MFA and access key status?
- Cost and Usage Report
- The IAM credential report
- AWS Artifact
- CloudFront logs
Why should you be careful when removing permissions that were not used in the last 90 days?
- Removing permissions costs money
- A rarely run job may still need them
- IAM forbids it
- Unused permissions are free