The AWS CLI is the fastest way to script and automate an AWS account, and it is also where many credential leaks start. Access keys pasted into a laptop config, a shared script or a public repository keep working until someone notices. A careful CLI setup takes ten minutes and avoids most of that risk.
This guide covers installing CLI version 2, signing in with IAM Identity Center, using profiles for several accounts, and handling long-term access keys safely when you have no alternative.
Install AWS CLI v2
Use version 2, which AWS publishes as a bundled installer for Windows, macOS and Linux and which supports Identity Center sign-in natively. Follow the official installation guide, then run aws –version to confirm. If you cannot install software, AWS CloudShell in the console gives you a browser terminal with the CLI already signed in as your console identity.

Credential options compared
| Method | Credential lifetime | Use it for |
|---|---|---|
| IAM Identity Center (aws configure sso) | Temporary, refreshed by sign-in | People on laptops and workstations |
| IAM role on EC2, Lambda or containers | Temporary, automatic | Code running on AWS |
| OIDC federation into a role | Temporary per job | CI/CD such as GitHub Actions |
| CloudShell | Console session | Quick tasks without local setup |
| IAM user access keys | Long-term until deleted | Tools that support nothing else |
| Root access keys | Long-term, unlimited power | Never |
Sign in with IAM Identity Center
- Make sure Identity Center is enabled and you have a user with a permission set; the root user vs Identity Center guide explains the setup.
- Run aws configure sso and enter a session name, the start URL of your access portal and its Region.
- Approve the sign-in in the browser window that opens.
- Pick the account and role, a default Region and output format, and give the profile a clear name such as prod-admin.
- Later, run aws sso login –profile prod-admin when the session expires.
The CLI SSO configuration guide covers the options. Nothing permanent is written to disk: the CLI caches short-lived tokens that expire on their own.
Profiles for several accounts
Each profile in the config file holds one account, role and Region. Pass –profile on a command, or set the AWS_PROFILE environment variable for a terminal session. Name profiles after the account and access level, not after people, and keep production profiles read-only by default with a separate admin profile for changes. If you manage several accounts, the AWS Organizations guide shows how one Identity Center portal covers all of them.
Before any change, run aws sts get-caller-identity. It returns the account ID and role, and it is the simplest guard against deleting something in the wrong account.
How the CLI finds credentials
The CLI checks sources in order and uses the first one it finds: command-line options, environment variables such as AWS_ACCESS_KEY_ID, the selected profile in the credentials and config files, container credentials, and finally the instance role on EC2. A forgotten environment variable can silently override the profile you think you are using, so check get-caller-identity whenever results look wrong.
When you need access keys
Some third-party tools, older backup software or on-premises servers only accept an access key ID and secret. In that case:
- Create a dedicated IAM user for that tool, never a person’s user and never root.
- Attach only the permissions the tool needs, scoped to specific buckets or resources.
- Store the key in a secrets manager or the tool’s encrypted configuration, not in code.
- Add the credentials file and .env files to .gitignore and use a secret scanner on repositories.
- Watch the key’s last-used date in IAM, and delete keys nobody uses.
The IAM roles and policies guide shows how to write narrow policies.
Rotating keys
An IAM user can hold two access keys at once, which makes rotation safe: create a second key, update every tool to use it, confirm in IAM that the old key’s last-used time has stopped moving, then deactivate the old key, wait, and delete it. Set a reminder, or an AWS Config rule, to flag keys older than your rotation period.
If a key leaks
- Deactivate the key in IAM immediately.
- Look up the key’s activity in CloudTrail; the CloudTrail vs CloudWatch guide explains where to look.
- Check every Region for unknown instances, users, roles and Lambda functions.
- Delete the key and any resources the attacker created, and review the bill.
- Answer any message from AWS about exposed credentials, since unanswered abuse notices can lead to restrictions. The suspended account guide explains that process.
After an account handover, delete every access key you did not create before you configure the CLI. The handover checklist lists the full set of checks, and the verification notes on each AWS account help you know what to expect.
CLI security checklist
- AWS CLI v2 installed and up to date.
- People sign in with aws configure sso, not static keys.
- Named profiles per account and role; production defaults to read-only.
- No root access keys exist.
- Remaining IAM user keys are scoped, stored securely and rotated.
- Credentials files excluded from version control.
AWS CLI and access key questions
Should I use access keys with the AWS CLI?
Only when there is no better option. For people, AWS recommends signing in through IAM Identity Center with aws configure sso, which issues temporary credentials. On AWS compute, use IAM roles. Long-term IAM user keys are a last resort.
Where does the AWS CLI store credentials?
Settings live in the config file and long-term keys in the credentials file, both in the .aws folder in your home directory. SSO sign-ins store short-lived tokens in a cache folder there instead of permanent keys.
How many access keys can an IAM user have?
Each IAM user can have up to two access keys at a time. That allows rotation: create the new key, update every tool, confirm the old key is unused, then deactivate and delete it.
What should I do if an access key is leaked?
Deactivate the key immediately in IAM, check CloudTrail for activity by that key, delete it, and review the account for unknown users, roles, instances and billing changes. Rotate any other secrets that might have been exposed.
How do I check which account the CLI is using?
Run aws sts get-caller-identity. It shows the account ID and the user or role ARN for the current profile, which helps avoid running commands against the wrong account.
Editorial guide by Mark Biden · Reviewed 24 November 2026. Official sources are linked beside the relevant sections; AWS features, quotas and prices change, so confirm details in the AWS console before relying on them.

Leave a Reply