← All articles

Auditing and Rotating AWS IAM Access Keys: CLI Guide

An open blank notebook beside two separate teal cards and a small amber tab.
Keep the inventory review and credential replacement work separately accountable.

Key takeaways

  • Audit key age across an AWS account by looping list-users, list-access-keys, and get-access-key-last-used into one report.
  • Rotate keys without downtime using the create, deploy, verify, deactivate, delete sequence, since AWS allows two keys per user.
  • Always deactivate the old key and watch for 24 to 48 hours before deleting, because deactivation is your free rollback window.
  • IAM cannot block keys past an age threshold, so enforcement means Config rules like access-keys-rotated plus a named owner and deadline per key.
  • Vaults store secret values but do not track ownership and expiry, so a metadata-only lifecycle layer is needed alongside them.

You can audit and rotate aws iam access keys across every user in your account with four command sequences: list-access-keys piped through jq to flag key age, then a create-verify-deactivate-delete rotation that never leaves a gap in working credentials. This reference gives you the exact commands end to end, plus how to turn the output into a 30/60/90-day rotation policy you can actually enforce.

How do I list IAM access keys for all users with the AWS CLI?

The basic list-access-keys command

For a single user, run:

aws iam list-access-keys --user-name alice

The command returns AccessKeyMetadata for that user: AccessKeyId, CreateDate, Status, and UserName. Note that it never returns secret keys, only metadata. Pagination applies: the default page returns up to 100 keys, so pass --pager or handle --max-items and the next-token in scripts. Full syntax is in the AWS CLI reference for list-access-keys.

Looping over all users with list-users

IAM has no "list keys for every user" call, so enumerate users first:

for u in $(aws iam list-users --query 'Users[].UserName' --output text); do
  aws iam list-access-keys --user-name "$u"
done

For AWS Organizations setups, repeat per account with aws sts assume-role into each account, or run the loop from your management tooling.

Formatting output with jq for a readable inventory

Pipe results through jq for a flat inventory:

aws iam list-access-keys --user-name "$u" \
  | jq '.AccessKeyMetadata[] | {user:.UserName, key:.AccessKeyId, created:.CreateDate, status:.Status}'

That one-line-per-key output is what you'll feed into the age audit and rotation tracking later, and what KeyGuardRail ingests as metadata only. For the September 2025 AWS credential-report changes that affect this workflow, see our brief.

How do I identify access keys that are old or never used?

Two fields tell you almost everything: CreateDate on the key and LastUsedDate from get-access-key-last-used. A key older than your 30/60/90-day rotation policy is overdue even if it is busy. A key with no recent activity is a different risk: it may be abandoned and safe to retire.

Checking last-used dates per key

aws iam get-access-key-last-used \
  --access-key-id AKIAEXAMPLE \
  --query 'AccessKeyLastUsed.LastUsedDate'

This returns the last time the key authenticated an AWS API call, plus the service and region. Pair it with the key's CreateDate from list-access-keys to see both age and activity in one line.

A one-liner audit script for key age across the account

for u in $(aws iam list-users --query 'Users[].UserName' --output text); do
  aws iam list-access-keys --user-name "$u" \
    --query "AccessKeyMetadata[].{user:'$u',key:AccessKeyId,created:CreateDate}" --output text
done

Join that output against get-access-key-last-used per key to flag anything older than 90 days or unused for 30.

Interpreting the LastUsedDate field honestly

LastUsedDate only covers AWS API calls, so a key used by a third-party SaaS integration through a proxy can look unused. It also updates on a delay, so treat a fresh timestamp as approximate. Age plus activity together, not either alone, decide whether a key is a rotation candidate or a deletion candidate. Tracking ownership and expiry across accounts and providers is the gap KeyGuardRail exists to close; we handle credential metadata only, never secret values, and you can remove your account and data at any time via our data deletion policy.

What is the correct order of steps to rotate an AWS access key without downtime?

Rotating an AWS IAM access key without downtime is a five-step sequence: create the new key, deploy it to every workload that uses the old one, verify the new key works, deactivate the old key, then delete it. AWS allows a user a maximum of two access keys at a time, which is what makes the overlap possible. Never delete before you deactivate, because deactivation gives you a free rollback window.

Step 1: Create the new key

aws iam create-access-key --user-name deploy-bot

Capture AccessKeyId and SecretAccessKey from the output immediately; this is the only time the secret is visible.

Step 2: Deploy the new secret to workloads

Update every place the old key lives: environment variables in ECS task definitions, Lambda configuration, CI/CD variables, and any local .env files. This is the step most outages come from, because a single missed consumer keeps calling the old key.

Step 3: Verify the new key works

Run a real read-only call with the new credentials, such as aws s3 ls scoped to a bucket the key can read. Check aws iam get-access-key-last-used on the old key: per AWS documentation on LastUsedDate, the timestamp reflects the most recent successful use, so falling usage confirms traffic has moved. Allow for the reporting delay AWS documents before treating silence as proof.

Step 4: Deactivate the old key

aws iam update-access-key --access-key-id AKIAOLDKEY --status Inactive --user-name deploy-bot

Watch error logs and CloudTrail for AccessDenied events for 24 to 48 hours. If something breaks, reactivate rather than reissuing.

Step 5: Delete the old key

aws iam delete-access-key --access-key-id AKIAOLDKEY --user-name deploy-bot

Once deleted, the key is unrecoverable, so only delete after a clean deactivation window. Record the rotation date and next deadline in your tracking layer, whether that is a spreadsheet or a lifecycle tool, so the 30/60/90-day clock is visible to whoever owns the key next.

How do I verify a rotated key works before deleting the old one?

Test the new key with a read-only call before you touch the old one. aws sts get-caller-identity --profile newkey confirms the credentials resolve to the right principal. For a permission check, run something the key actually needs, scoped to read: aws s3 ls s3://your-bucket --profile newkey or aws iam list-access-keys --profile newkey. Both fail loudly if the key is wrong or under-scoped.

Deactivate before deleting: aws iam update-access-key --access-key-id AKIA... --status Inactive. Leave it inactive for a day or two while your apps and CI run. If nothing breaks, delete; if something does, reactivate and fix the consumer first. Deleting immediately removes your only rollback path.

Track each key's owner and verification status so nobody deletes a key that a stray Postman collection still uses. KeyGuardRail monitors key metadata and flags rotation deadlines without holding the secret value.

How do I set an IAM access key rotation policy my team can enforce?

A policy is enforceable when IAM rejects what it can reject, Config flags what it cannot, and every key has a named owner with a deadline. AWS's own guidance is to rotate keys on a schedule and update applications before deleting the old key, per the IAM User Guide on access keys. The 30/60/90-day tiers only work if someone is accountable for each deadline.

What IAM can and cannot enforce on access keys

IAM password policies can force password rotation, but access key age cannot be blocked by policy alone. Use IAM Access Analyzer plus Config rules like access-keys-rotated to flag keys past your threshold, and wire the findings into Slack or a ticket. Enforcement here is detection plus follow-up, not prevention.

Mapping keys to owners and deadlines

Inventory output means nothing without ownership. Build a register like this:

Key prefixOwnerServiceRotation tierDeadline
AKIA…4T2QPayments platformStripe webhook signing30-day2025-07-14
AKIA…9KLMPlatform engineeringCI deploy user90-day2025-08-01

Without a named owner, an expiring key sits unrotated until it fails in production. For why manual tracking spreadsheets break down, see alternatives to manual credential rotation tracking.

Where a lifecycle monitoring layer fits alongside your vault

Your vault (AWS Secrets Manager, Vault, Infisical) stores values and can shorten credential lifetimes, but it does not answer "who owns this key and when does it expire" across AWS, Stripe, and GitHub together. A lifecycle monitoring layer like KeyGuardRail tracks metadata only: expiry dates, owners, and rotation deadlines, with the secret value never leaving your vault. That keeps humans out of the transport layer while the vault stays the system of record.

What CLI commands audit access key age across an entire AWS account?

Three commands do the whole job: list-users enumerates IAM users, list-access-keys pulls each user's key IDs and create dates, and get-access-key-last-used shows when each key last authenticated. Run together in a loop, they produce one report covering key age, last-use date, and status.

The full audit script

This loop writes a line per key: user, key ID, age in days, days since last use, and whether the key is active.

for user in $(aws iam list-users --query 'Users[].UserName' --output text); do
  for key in $(aws iam list-access-keys --user-name "$user" \
      --query 'AccessKeyMetadata[].AccessKeyId' --output text); do
    created=$(aws iam list-access-keys --user-name "$user" \
      --query "AccessKeyMetadata[?AccessKeyId=='$key'].CreateDate" --output text)
    lastused=$(aws iam get-access-key-last-used --access-key-id "$key" \
      --query 'LastUsedDate' --output text)
    age=$(( ($(date +%s) - $(date -d "$created" +%s)) / 86400 ))
    idle=$(( lastused == "None" ? -1 : ($(date +%s) - $(date -d "$lastused" +%s)) / 86400 ))
    echo "$user,$key,$age,$idle"
  done
done

An idle value of -1 means the key has never been used, which is your fastest deletion candidate.

Running it across multiple accounts with role assumption

Wrap the loop in a call to aws sts assume-role per account, export the returned credentials, and run the audit against each role in turn. Aggregate the CSV output into one file so every account reports against the same 30/60/90-day thresholds.

How do I find which IAM users have active access keys at all?

Filtering active keys with jq

Pipe list-access-keys output through jq to keep only Status: Active keys, then join that with list-users so you see every principal holding a live key, not just key age. A user with zero active keys drops out of the rotation conversation entirely, which is the point: your 30/60/90-day policy should only govern credentials that can still authenticate.

Deciding which users should not have keys at all

Cross-reference the active-key list against your human users. Anyone authenticating through SSO, federation, or assuming roles has no business holding a long-lived key, and IAM best practices say to prefer roles over long-lived access keys for human users. The remaining list is your real inventory: service accounts and break-glass users, each of which needs a named owner and a rotation deadline.

How does this fit with Secrets Manager and your existing vault?

Vaults are storage, not lifecycle tracking

AWS Secrets Manager, HashiCorp Vault, and Infisical store and distribute secret values well. None of them answer the questions the CLI audit above surfaces: who owns this key, when does the API key expire, and which rotation deadline slipped. Vaults also stop at their own perimeter. Your Stripe keys, GitHub tokens, and IAM access keys live outside any vault, which is where secrets sprawl starts.

KeyGuardRail tracks credential metadata only: owner, expiry date, last rotation, next deadline. It never holds secret values, so it sits alongside your vault instead of replacing it. The goal stays the one from the Reddit thread: humans don't handle secrets.

Running the same criteria against KeyGuardRail honestly

By the criteria in this article, KeyGuardRail covers inventory, ownership, and expiry deadlines across AWS and SaaS providers, but it does not rotate keys for you; the rotation sequence above is still yours to run. Doppler, Vault, and Infisical cover storage and distribution. Clutch Security and Akeyless cover other parts of the credential problem. Pick the layer you're missing.

FAQ

How often should I rotate AWS IAM access keys?

A 30/60/90-day tiered schedule works well: 30 days for high-risk keys like payment or CI deploy credentials, 90 days for low-activity service keys. The tier matters less than having a named owner and deadline for every key, since unowned keys sit unrotated until they fail in production.

Can I rotate an access key without downtime?

Yes, because AWS allows two access keys per user. Create the new key, deploy it to every consumer, verify it works, deactivate the old key, then delete it after a clean 24 to 48 hour window. Deactivation before deletion gives you a free rollback path if a missed workload still calls the old key.

Does AWS automatically rotate IAM access keys?

No. IAM access keys never rotate automatically, and no IAM policy can block keys past an age threshold. Detection is your only enforcement option: Config rules like access-keys-rotated flag overdue keys, and you run the create-verify-deactivate-delete sequence yourself on the schedule you set.

How do I see when an access key was last used?

Run aws iam get-access-key-last-used --access-key-id AKIA... and read LastUsedDate from the output, which also shows the service and region of the last call. Treat fresh timestamps as approximate, since the field updates on a delay, and remember it only covers direct AWS API authentication.

Should I replace IAM users with roles instead of rotating keys?

For human users, yes. Anyone authenticating through SSO, federation, or role assumption has no business holding a long-lived key, and AWS best practices say to prefer roles for people. Roles still leave service accounts and break-glass users needing keys, so rotation remains necessary for that remaining inventory.