This week I ran aws sts get-caller-identity on my own laptop and got this back:
{
"UserId": "111122223333",
"Account": "111122223333",
"Arn": "arn:aws:iam::111122223333:root"
}
That :root at the end means the CLI profile was signed in as the account's root user, the one identity in an AWS account that no policy can restrict. I'd set it up months earlier to "just get something working" and forgotten about it. The same machine also had long-lived access keys for an old lab account that I hadn't used in weeks.
I'm studying for the AWS Solutions Architect Associate exam, and writing each topic up is how I check that I actually understand it. This is the first of three posts on AWS Identity and Access Management (IAM). It covers the day-one checklist for any AWS account:
- Protect the root user.
- Stop using the root user for day to day tasks.
- Audit what is left.
How IAM decides: the one rule to remember
Every request to AWS is made by a principal, which is a user, a role, or the root user. AWS first checks who the principal is (authentication), then whether it's allowed to do what it's asking (authorization). Authorization follows one rule:
- Everything is denied by default.
- An explicit
Allowin a policy grants access. - An explicit
Denyalways wins over anyAllow.
The root user is the exception. It's allowed to do everything, and IAM policies in the account can't take that away. That's why it needs more protection than anything else in the account.
Why root exists, and why you shouldn't use it
The root user is the email address and password you used to create the account. You can't delete it, because a short list of tasks can only be done as root, including:
- Closing the account.
- Restoring access when the only IAM administrator has removed their own permissions.
- Turning on MFA delete for an S3 bucket.
- Fixing an S3 bucket policy that denies everyone, including administrators.
Everything else, including full administrator work, can be done by a non-root identity that you can restrict, monitor and revoke. So the plan is: lock root away for those rare tasks, and do everything else as a non-root identity.
Step 1: Protect the root user
Turn on MFA, ideally twice
Sign in to the console as root, open the account menu, choose Security credentials, and assign an MFA device. AWS now requires MFA on root users, but the type of device matters:
| Type | Phishing resistance | Notes |
|---|---|---|
| Passkey / security key (e.g. YubiKey) | Strongest — FIDO-based, unique to the real AWS sign-in page | Recommended by AWS wherever possible |
| Virtual authenticator app (TOTP) | Weaker — codes can be intercepted on a fake page | Acceptable interim measure |
| Hardware TOTP token | Weaker — same TOTP limitation | Alternative to virtual app |
Root can have more than one MFA device. Register two, such as a passkey and a hardware key kept somewhere else, so losing one doesn't lock you out of the account.
Make sure root has no access keys
Access keys let a program act as root, with no MFA prompt. Root should never have them. You can check without signing in as root, from any identity with IAM read access:
aws iam get-account-summary \
--query 'SummaryMap.{RootAccessKeys: AccountAccessKeysPresent, RootMFA: AccountMFAEnabled}'
You want RootAccessKeys to be 0 and RootMFA to be 1. If root has keys, sign in as root, delete them under Security credentials, and then find out what was using them.
{
"RootAccessKeys": 0,
"RootMFA": 1
}
Set alternate contacts
AWS sends security notices, such as "your access key was found in a public GitHub repository", to the account's contacts. If the only contact is the root email address of a mailbox nobody reads, those notices go nowhere. Add a security contact:
aws account put-alternate-contact \
--alternate-contact-type SECURITY \
--name "Security team" \
--title "Security" \
--email-address security@example.com \
--phone-number "+1-555-0100"
Repeat with BILLING and OPERATIONS.
You can also add in --account-id 'your-account-id' if you want to set this on specific Organizations. Otherwise it will apply to the account-id running the commands.
If you use AWS Organizations
With several accounts in AWS Organizations, you can turn on centralized root access in the IAM console. It lets the management account remove root credentials from member accounts entirely, and perform the few root-only tasks centrally when you need them. That leaves you one root user to protect instead of one per account.
Step 2: Create a non-root admin
You need an everyday identity with administrator access that isn't root. There are two ways to create one.
Recommended: IAM Identity Center
IAM Identity Center gives people short-lived credentials through a browser sign-in, instead of a password and keys stored in IAM. For a single account:
- Open IAM Identity Center in the console and choose Enable. It sets up AWS Organizations for you if you don't have it.
- Under Users, create a user for yourself with an email address you control.
- Under Permission sets, create a predefined permission set using
AdministratorAccess. - Under AWS accounts, select your account and assign your user with that permission set.
- Accept the invitation email, set a password, and register an MFA device for this user too.
Then sign the CLI in with it. I covered the full setup in Setting up the AWS CLI on WSL the right way; the short version is:
aws configure sso
aws sso login --profile admin
aws sts get-caller-identity --profile admin
This time the ARN shows an assumed role, not root:
"Arn": "arn:aws:sts::111122223333:assumed-role/AWSReservedSSO_AdministratorAccess_0123456789abcdef/you"
Then put root away
Sign out of root. Store its password in a password manager and its MFA devices somewhere safe, and don't sign in as root again unless you need one of the root-only tasks.
Step 3: Remove root from your machine
This is the step I'd missed. Recent versions of the AWS CLI include an aws login command that signs the CLI in with your console credentials. It's convenient, and it accepts root's credentials as readily as anyone else's. That's how I ended up with a root profile.
List your profiles and check who each one really is:
aws configure list-profiles
aws sts get-caller-identity --profile <name>
The CLI has no command to delete a profile. Open ~/.aws/config and delete the root profile's block, from its [profile name] line down to the next line in square brackets. Do the same in ~/.aws/credentials if it has a matching block (there, the header is just [name]). Cached sign-in tokens can stay on disk after the profile is gone, so clear those too:
rm -f ~/.aws/login/cache/* ~/.aws/sso/cache/*
That signs you out of every profile. Run aws sso login again for the ones you're keeping.
While you're in there, set your admin profile as the default for the shell, so a command without --profile never lands in an account you didn't mean:
echo 'export AWS_PROFILE=admin' >> ~/.bashrc
Step 4: Audit every credential with the credential report
The IAM credential report is a CSV file listing every IAM user in the account, plus root, with the state of their passwords, MFA and access keys. Generate it, then download and decode it:
aws iam generate-credential-report
aws iam get-credential-report --query Content --output text | base64 --decode > credential-report.csv
If the first command returns "State": "STARTED", wait a few seconds and run it again until it says COMPLETE. AWS reuses a report for up to four hours, so if you've just made a change and want to see it, wait or check the user directly.
The columns worth reading first:
| Column | What to look for |
|---|---|
user | Root appears as <root_account>. |
mfa_active | false on root or any user with a password. |
password_last_used | Users who haven't signed in for months. |
access_key_1_active | true on root is a problem. |
access_key_1_last_rotated | Keys older than about 90 days. |
access_key_1_last_used_date | N/A means the key has never been used, so it probably isn't needed. |
Each user can have two access keys, so the same columns repeat as access_key_2_*.
A script to flag problems
Opening the CSV in a spreadsheet works for a handful of users. This short Python script prints only the rows that need attention:
import csv, sys
from datetime import datetime, timedelta, timezone
now = datetime.now(timezone.utc)
max_age = timedelta(days=90)
for row in csv.DictReader(open(sys.argv[1])):
user = row["user"]
is_root = user == "<root_account>"
has_password = is_root or row["password_enabled"] == "true"
if has_password and row["mfa_active"] != "true":
print(f"{user}: can sign in without MFA")
for n in ("1", "2"):
if row[f"access_key_{n}_active"] != "true":
continue
if is_root:
print(f"{user}: root has an active access key")
rotated = datetime.fromisoformat(row[f"access_key_{n}_last_rotated"])
if now - rotated > max_age:
print(f"{user}: key {n} is {(now - rotated).days} days old")
python3 check_credentials.py credential-report.csv
alice: can sign in without MFA
bob: key 1 is 314 days old
carol: key 2 is 537 days old
<root_account>: can sign in without MFA
<root_account>: root has an active access key
No output means nothing to fix. Running it once a month takes a minute and catches the things you forgot you'd created, like my lab account's keys.
Credential report vs. IAM access advisor
The credential report tells you whether credentials exist and when they were last used. It doesn't say what they were used for. For that, open a user or role in the IAM console and check the Access Advisor tab. This is IAM access advisor, and it lists the AWS services the identity has actually called. If a user's policy allows twenty services and access advisor shows they've only used two, you can safely remove the rest.
Don't confuse it with IAM Access Analyzer, a separate service that finds resources shared outside your account and unused access across an organization.
On the exam
- Use root only for root-only tasks. Know a few of them, such as closing the account and changing root's email or password.
- Root should have MFA and no access keys. "Create access keys for the root user" is never the right answer.
- An explicit
Denyalways overrides anAllow. - In AWS Organizations, service control policies (SCPs) restrict member accounts, including their root users, but don't apply to the management account.
- Credential report: credentials across the whole account. IAM access advisor: which services one identity has used. IAM Access Analyzer: external and unused access.
Checklist
- Root has MFA, ideally two devices, and at least one is a passkey or hardware key.
aws iam get-account-summaryshows no root access keys.- Billing, operations and security contacts are set.
- You have a non-root admin, preferably through IAM Identity Center, with MFA.
- No CLI profile on your machine returns
:root, andAWS_PROFILEpoints at your admin. - You've run the credential report, and the script prints nothing.
Next
The next post covers IAM users, groups, policies and access keys, with a demo that gives a contractor access to deploy one website and nothing else.