aws-iam-best-practices – DevOps & Infrastructure skill for AI agents
IAM policy review, hardening, and least privilege implementation
Install the aws-iam-best-practices skill with one command
Pick the tool you use, copy the command and paste it in your terminal. The skill lands in that tool's skills folder and is ready in your next session.
Claude Code
mkdir -p ~/.claude/skills && curl -fsSL https://skill.dn1nguoi.com/z/aws-iam-best-practices-b25fe.tgz | tar xz -C ~/.claude/skills
Call it in Claude Code: /aws-iam-best-practices – then describe the task you want done.
Codex
mkdir -p ~/.codex/skills && curl -fsSL https://skill.dn1nguoi.com/z/aws-iam-best-practices-b25fe.tgz | tar xz -C ~/.codex/skills
Call it in Codex: $aws-iam-best-practices – then describe the task you want done.
Hermes Agent
mkdir -p ~/.hermes/skills && curl -fsSL https://skill.dn1nguoi.com/z/aws-iam-best-practices-b25fe.tgz | tar xz -C ~/.hermes/skills
Call it in Hermes Agent: /aws-iam-best-practices – then describe the task you want done.
DeepSeek
mkdir -p ~/.deepcode/skills && curl -fsSL https://skill.dn1nguoi.com/z/aws-iam-best-practices-b25fe.tgz | tar xz -C ~/.deepcode/skills
Call it in DeepSeek: /aws-iam-best-practices – then describe the task you want done.
SKILL.md content
AWS IAM Best Practices
Review and harden IAM policies following AWS security best practices and least privilege principles.
Review scope and execution boundaries
Select the exact account/role, partition, regions and control/framework version before collecting evidence; verify identity with aws sts get-caller-identity. Read-only assessment is the default. Commands that create MFA devices, change policies/keys, enable services, rotate secrets or create alarms require an approved change scope and rollback plan. Keep evidence and secret material out of shared logs.
These are partial examples, not certification or complete benchmark implementations. Control numbers are illustrative historical mappings, not a pinned current benchmark. Record PASS/FAIL/UNKNOWN/NOT-APPLICABLE per control with evidence and coverage. AccessDenied, API errors, missing permissions, absent pages and unimplemented checks are UNKNOWN, never PASS. Use paginators for SDK listings and cover all selected regions/accounts; never infer completeness from no findings. Shell snippets require Bash/GNU date, AWS CLI, jq and error checking (set -euo pipefail in complete scripts). Generate the IAM credential report first, wait for COMPLETE and decode its base64 Content; parse CSV by header in production. Credential reports are sensitive.
When to Use
Use this skill when you need to review IAM policies, implement least privilege access, or harden IAM security.
Core Principles
Least Privilege
- Grant minimum permissions needed
- Use managed policies when possible
- Avoid wildcard (*) permissions
- Regular access reviews
Defense in Depth
- Enable MFA for all users
- Use IAM roles instead of access keys
- Implement service control policies (SCPs)
- Enable CloudTrail for audit
Separation of Duties
- Separate admin and user roles
- Use different roles for different environments
- Implement approval workflows
- Regular permission audits
IAM Security Checks
Find Overly Permissive Policies
# Policy-name triage only: a name does not establish effective permissions
aws iam list-policies --scope Local \
--query 'Policies[*].[PolicyName,Arn]' --output table | \
grep -i admin
# Find policies with wildcard actions
aws iam list-policies --scope Local --query 'Policies[*].Arn' --output text | tr '\t' '\n' | \
while read arn; do
version=$(aws iam get-policy --policy-arn "$arn" \
--query 'Policy.DefaultVersionId' --output text)
doc=$(aws iam get-policy-version --policy-arn "$arn" \
--version-id "$version" --query 'PolicyVersion.Document')
if echo "$doc" | grep -q '"Action": "\*"'; then
echo "Wildcard action in: $arn"
fi
done
# Find inline policies (should use managed policies)
aws iam list-users --query 'Users[*].UserName' --output text | tr '\t' '\n' | \
while read user; do
policies=$(aws iam list-user-policies --user-name "$user" \
--query 'PolicyNames' --output text)
if [ -n "$policies" ]; then
echo "Inline policies on user $user: $policies"
fi
doneMFA Enforcement
# List users without MFA
aws iam get-credential-report --query Content --output text | base64 --decode | \
awk -F, 'NR>1 && $8=="false" {print $1}'
# Check if MFA is required in policies
aws iam list-policies --scope Local --query 'Policies[*].Arn' --output text | tr '\t' '\n' | \
while read arn; do
version=$(aws iam get-policy --policy-arn "$arn" \
--query 'Policy.DefaultVersionId' --output text)
doc=$(aws iam get-policy-version --policy-arn "$arn" \
--version-id "$version" --query 'PolicyVersion.Document')
if echo "$doc" | grep -q "aws:MultiFactorAuthPresent"; then
echo "MFA condition mentioned in: $arn (inspect Effect/operator/applicability)"
fi
done
# Create a virtual device only; enable-mfa-device with two valid codes is separate.
# Store the bootstrap seed privately and delete after verified enrollment.
umask 077
MFA_DIR=$(mktemp -d)
aws iam create-virtual-mfa-device \
--virtual-mfa-device-name user-mfa \
--outfile "$MFA_DIR/qr.png" \
--bootstrap-method QRCodePNGAccess Key Management
# Find old access keys (>90 days)
aws iam list-users --query 'Users[*].UserName' --output text | tr '\t' '\n' | \
while read user; do
aws iam list-access-keys --user-name "$user" \
--query 'AccessKeyMetadata[*].[AccessKeyId,CreateDate,Status]' \
--output text | \
while read key_id create_date status; do
age_days=$(( ($(date +%s) - $(date -d "$create_date" +%s)) / 86400 ))
if [ $age_days -gt 90 ]; then
echo "$user: Key $key_id is $age_days days old"
fi
done
done
# Rotate access key
OLD_KEY="<AWS_ACCESS_KEY_ID>"
IAM_USER="myuser"
# Create new key into a private new file; never print the secret.
umask 077
KEY_DIR=$(mktemp -d)
aws iam create-access-key --user-name "$IAM_USER" > "$KEY_DIR/new-key.json"
echo "New key created. Update applications, then run:"
echo "aws iam delete-access-key --user-name $IAM_USER --access-key-id $OLD_KEY"
# Deactivate old key (test first)
aws iam update-access-key \
--user-name "$IAM_USER" \
--access-key-id "$OLD_KEY" \
--status InactiveRole and Policy Analysis
# List unused roles (no activity in 90 days)
aws iam list-roles --query 'Roles[*].[RoleName,RoleLastUsed.LastUsedDate]' \
--output text | tr '\t' '\n' | \
while read role last_used; do
if [ "$last_used" = "None" ]; then
echo "No reported use in tracking period: $role; verify GetRole and retention"
fi
done
# Find roles with trust relationships to external accounts
aws iam list-roles --query 'Roles[*].RoleName' --output text | tr '\t' '\n' | \
while read role; do
trust=$(aws iam get-role --role-name "$role" \
--query 'Role.AssumeRolePolicyDocument')
if echo "$trust" | grep -q '"AWS":'; then
echo "AWS principal trust present: $role; compare account IDs and conditions"
fi
done
# Analyze policy permissions
aws iam simulate-principal-policy \
--policy-source-arn arn:aws:iam::123456789012:user/myuser \
--action-names s3:GetObject s3:PutObject \
--resource-arns arn:aws:s3:::mybucket/*IAM Policy Templates
Least Privilege S3 Access
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::my-bucket/user-data/${aws:username}/*"
},
{
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::my-bucket",
"Condition": {
"StringLike": {
"s3:prefix": "user-data/${aws:username}/*"
}
}
}
]
}The username policy variable is for IAM-user principals; role/federated sessions need a validated principal-tag/identity design. These explicit-deny policies can lock out API clients and service calls; test the applicable MFA/SourceIp context and recovery access before attachment. Policy simulation alone is not a live authorization guarantee.
MFA-Required Policy
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Action": "*",
"Resource": "*",
"Condition": {
"BoolIfExists": {
"aws:MultiFactorAuthPresent": "false"
}
}
}
]
}Time-Based Access
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ec2:*",
"Resource": "*",
"Condition": {
"DateGreaterThan": {
"aws:CurrentTime": "2026-01-01T00:00:00Z"
},
"DateLessThan": {
"aws:CurrentTime": "2026-12-31T23:59:59Z"
}
}
}
]
}IP-Restricted Access
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Action": "*",
"Resource": "*",
"Condition": {
"NotIpAddress": {
"aws:SourceIp": [
"203.0.113.0/24",
"198.51.100.0/24"
]
}
}
}
]
}IAM Hardening Checklist
User Management
- [ ] Enable MFA for all users
- [ ] Remove unused IAM users
- [ ] Rotate access keys every 90 days
- [ ] Use IAM roles instead of long-term credentials
- [ ] Implement password policy (length, complexity, rotation)
Policy Management
- [ ] Replace inline policies with managed policies
- [ ] Remove wildcard (*) permissions
- [ ] Implement least privilege
- [ ] Use policy conditions (MFA, IP, time)
- [ ] Regular policy reviews
Role Management
- [ ] Use roles for EC2 instances
- [ ] Implement cross-account roles properly
- [ ] Review trust relationships
- [ ] Remove unused roles
- [ ] Use session tags for fine-grained access
Monitoring
- [ ] Enable CloudTrail for IAM events
- [ ] Set up CloudWatch alarms for IAM changes
- [ ] Use AWS IAM Access Analyzer
- [ ] Regular access reviews
- [ ] Monitor for privilege escalation
Automated IAM Hardening
#!/usr/bin/env python3
# iam-hardening.py
import boto3
from datetime import… (full content is in the download package)
Related skills
- aws-lambda – Build and deploy serverless functions on AWS Lambda. Configure triggers, manage…
- aws-penetration-testing – Provide comprehensive techniques for penetration testing AWS cloud environments. Covers…
- aws-rds – Provision and manage RDS databases. Configure backups, replication, and security. Use…
- aws-s3 – Configure S3 buckets, policies, and lifecycle rules. Implement versioning, replication…
- aws-secrets-manager – Store and rotate secrets in AWS Secrets Manager.
- helm-chart-scaffolding – Design, organize, and manage Helm charts for templating and packaging Kubernetes…
- aws-secrets-rotation – Automate AWS secrets rotation for RDS, API keys, and credentials
- aws-serverless – Specialized skill for building production-ready serverless applications on AWS. Covers…