Sub topic 3
Passwords, MFA, and access keys
This chapter assumes you know what an IAM user is, can read JSON, and have the AWS CLI installed. An IAM user can hold three kinds of credentials. Each one serves a different kind of access, and each carries different risks.
The three credential types
| Credential | Used for | Lifetime |
|---|---|---|
| Console password | Signing in to the AWS Management Console | Until changed or expired |
| MFA device | A second factor on top of a password or an API call | Until deactivated |
| Access key | Programmatic calls from the CLI, SDKs, and scripts | Until deactivated or deleted |
A password and an access key are separate credentials. A user can have either, both, or neither. Creating a user does not create any of them automatically.
Passwords
A console password lets a person sign in through the account sign-in page. You can set an account-wide password policy that controls minimum length, required character types, expiration, and reuse prevention. The policy applies to IAM users only. It does not apply to the root user.
aws iam update-account-password-policy \
--minimum-password-length 14 \
--require-symbols \
--require-numbers \
--max-password-age 90 \
--password-reuse-prevention 5Multi-factor authentication
Multi-factor authentication (MFA) asks for something you have in addition to something you know. After the password, the user must also supply a code or a hardware response. A stolen password alone is then not enough to sign in.
IAM supports passkeys and security keys, virtual authenticator apps, and hardware TOTP tokens. You can register more than one MFA device per user. Prefer phishing-resistant options such as passkeys or FIDO security keys where you can.
MFA also protects API access. A policy can require it through a condition, as in the earlier policy that denies ec2:StopInstances without MFA. A long-lived access key cannot satisfy that condition on its own. The caller must first get temporary credentials with aws sts get-session-token, passing the device serial number and a current code.
aws sts get-session-token \
--serial-number arn:aws:iam::111122223333:mfa/dev-user \
--token-code 123456Access keys
An access key has two parts: an access key ID and a secret access key. The secret is shown only once, when you create the key. If it is lost, you must create a new key.
A user can have at most two access keys. The limit exists so you can rotate without downtime:
- Create a second key.
- Update every application to use it.
- Check when the old key was last used.
- Deactivate the old key, then delete it once nothing breaks.
aws iam create-access-key --user-name dev-user
aws iam get-access-key-last-used --access-key-id AKIAIOSFODNN7EXAMPLE
aws iam update-access-key \
--user-name dev-user \
--access-key-id AKIAIOSFODNN7EXAMPLE \
--status InactiveCommon errors
- Keys on the root user. Do not create access keys for the root user. Secure root with MFA and use it only for the few tasks that require it.
- Forgotten keys. Unused keys stay valid. Review the credential report regularly and remove keys that show no recent use.
- MFA conditions that miss key-based calls.
BoolIfExistsmatches requests that carry no MFA information.Boolalone does not.
Exercise
A script uses an access key for dev-user. A policy denies ec2:StopInstances unless MFA is present, and the script now fails with an access denied error. How can the script run the call and still satisfy the policy?
Show answer
The script must first call aws sts get-session-token with the user's MFA device serial number and a current code. It then uses the returned temporary credentials, which carry the MFA information the condition checks. The long-lived access key alone has no MFA context.
Exercise
Why does IAM allow only two access keys per user?
Show answer
Two keys let you rotate without downtime. You create the new key, move applications to it, and then deactivate and delete the old one.