$ run as a normal user# run as root (or with sudo)
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 AKIAIOSFODNN7EXAMPLEStep by step
- 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.
- 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.
- 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.
- Move every user of the key — CI secret, the app's config,
~/.aws/credentialson 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- Wait and check the old key (line 3) — after a day (or a full deploy cycle),
LastUsedDateof 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). - Deactivate first, delete later (lines 4 and 5) —
Inactivecan be undone in seconds if something breaks; delete after a few quiet days. - 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. - Keep watching — the AWS Config rule
access-keys-rotated(for examplemaxAccessKeyAge90) flags old keys automatically; send its result to Security Hub or a weekly report.
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.
Common problems
| Problem | Cause | Fix |
|---|---|---|
LimitExceeded on create | The user already has 2 keys. | Delete the unused key first (check last used). |
| App fails after deactivation | Some 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 InvalidClientTokenId | Copy error, or a few seconds of IAM delay. | Check the key ID; wait a moment; never paste secrets with spaces. |
| Lost the new secret | The secret is shown only once. | Delete that key and create a new one. |
| Old key leaked on GitHub | Public commit. | Deactivate at once, rotate, check CloudTrail for misuse, remove it from history. |
| Root user has access keys | Old setup. | Delete root keys; use MFA on root; work with roles. |
Go further
- Command pages: aws, jq.
- Next how-tos: Log in to AWS with SSO from the terminal, Deploy to Azure from GitHub Actions with OIDC, See who did what in AWS with CloudTrail, Remove a secret from Git history, Find secrets in a repository with Gitleaks.
- Learn it: Cloud CLIs at work — OIDC, Secrets services. Full guide: AWS CLI.
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.