Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 171Security · AWS

How to rotate AWS access keys without downtime

A long-lived IAM access key is a password that never expires: it sits in CI variables, laptops and old scripts, and one leak gives full access until someone notices. Best is no keys at all (SSO for people, roles and OIDC for machines). Where a key must stay, rotate it: create a second key, move the app to it, check the old key is no longer used, deactivate it, then delete it. This how-to does that safely and finds old keys in the whole account.

  • 5 Lessons
  • 3 Commands

$ run as a normal user# run as root (or with sudo)

171.1

The quick answer

aws iam list-access-keys --user-name ci-deploy --query 'AccessKeyMetadata[].[AccessKeyId,Status,CreateDate]' --output table
aws iam create-access-key --user-name ci-deploy --query 'AccessKey.[AccessKeyId,SecretAccessKey]' --output text
aws iam get-access-key-last-used --access-key-id AKIAIOSFODNN7EXAMPLE --query 'AccessKeyLastUsed.[LastUsedDate,ServiceName]' --output text
aws iam update-access-key --user-name ci-deploy --access-key-id AKIAIOSFODNN7EXAMPLE --status Inactive
aws iam delete-access-key --user-name ci-deploy --access-key-id AKIAIOSFODNN7EXAMPLE

Commands in this lesson

171.2

Step by step

  1. Find old keys in the whole account — the credential report has one line per user with key age and last use:
aws iam generate-credential-report
aws iam get-credential-report --query Content --output text | base64 -d | cut -d, -f1,9,10,11,14,15,16 | column -t -s,

Columns: user, key 1 active, key 1 last rotated, key 1 last used, then the same for key 2. Keys older than 90 days, or never used, are your list.

  1. Check the current key (quick answer, line 1). A user can have two keys — that is what makes rotation without downtime possible. If there are already two, delete the unused one first.
  2. Create the new key (line 2). The secret is shown only once: write it straight into the secret store (GitHub secret, Vault, AWS Secrets Manager), not into a file or chat.
  3. Move every user of the key — CI secret, the app's config, ~/.aws/credentials on servers. Restart or redeploy so the app really reads the new key. Test one call with the new key:
AWS_ACCESS_KEY_ID=AKIAI44QH8DHBEXAMPLE AWS_SECRET_ACCESS_KEY="$(cat /run/secrets/new-key)" aws sts get-caller-identity
  1. Wait and check the old key (line 3) — after a day (or a full deploy cycle), LastUsedDate of the old key must not move any more. If it still changes, some system still uses it: CloudTrail shows which (filter on the access key ID).
  2. Deactivate first, delete later (lines 4 and 5) — Inactive can be undone in seconds if something breaks; delete after a few quiet days.
  3. Get rid of keys for good — people: IAM Identity Center (aws sso login). EC2, Lambda, ECS: an instance or task role. GitHub Actions: OIDC to an IAM role. A key is then only needed for rare outside systems.
  4. Keep watching — the AWS Config rule access-keys-rotated (for example maxAccessKeyAge 90) flags old keys automatically; send its result to Security Hub or a weekly report.

171.3

Check that it worked

list-access-keys shows only the new key as Active; the app works; get-access-key-last-used of the new key shows today, and the old key is gone. The credential report shows no key older than your limit.

171.4

Common problems

ProblemCauseFix
LimitExceeded on createThe user already has 2 keys.Delete the unused key first (check last used).
App fails after deactivationSome system still used the old key.Set the old key back to Active, find the user in CloudTrail, move it, try again.
New key gives InvalidClientTokenIdCopy error, or a few seconds of IAM delay.Check the key ID; wait a moment; never paste secrets with spaces.
Lost the new secretThe secret is shown only once.Delete that key and create a new one.
Old key leaked on GitHubPublic commit.Deactivate at once, rotate, check CloudTrail for misuse, remove it from history.
Root user has access keysOld setup.Delete root keys; use MFA on root; work with roles.

171.5

Go further

1Why can you rotate a key without downtime?

An IAM user can have two keys at once: the app moves to the new key while the old one still works, and only then is the old key deactivated.

2Why deactivate the old key before deleting it?

Inactive can be switched back in seconds if a forgotten system breaks; a deleted key is gone for good.

Commands in this lesson

All commands in this chapter