Sub topic 12
Keeping access secure
This chapter assumes you can sign in to the Console, run basic AWS CLI commands, and know what a named profile is. It covers the habits that keep an AWS account safe when you work by hand: protecting the root user, using multi-factor authentication (MFA), granting least privilege, and preferring temporary credentials over long-lived access keys.
Why access practices matter
Every way of reaching AWS ends in the same place: a request signed by some identity. If someone else obtains that identity, they can do whatever it is allowed to do, from any location, at any hour. Security practices reduce two things: the chance that an identity is stolen, and the damage it can do if it is.
The Console, the CLI, and CloudShell all use the same IAM permission system. A policy that blocks an action in the Console blocks it in the CLI too. What differs is how each tool holds your credentials, and that is where most mistakes happen.
Protect the root user
The root user is the identity created with the account, signed in with the account's email address. It has full access to everything, and most permission policies cannot restrict it.
Follow these rules:
- Enable MFA on the root user immediately after creating the account.
- Do not create access keys for the root user. If any exist, delete them.
- Do not use root for daily work. Create an administrative identity in IAM Identity Center or IAM and use that instead.
- Sign in as root only for the few tasks that require it, such as changing the account's support plan or closing the account.
- Use a shared mailbox or distribution list for the root email address, so that account recovery does not depend on one person.
Multi-factor authentication
MFA requires a second proof of identity in addition to a password. Even if a password is phished or reused from a breached site, the attacker still lacks the second factor.
| MFA type | Example | Notes |
|---|---|---|
| Passkey or security key | FIDO2 hardware key, device passkey | Resistant to phishing because the key checks the site's domain. Preferred option. |
| Authenticator app | Time-based one-time password (TOTP) app | Widely available. A code can still be typed into a fake site. |
Register more than one MFA device where the service allows it, so losing a phone does not lock you out. Store hardware keys somewhere safe.
MFA protects sign-in. For the CLI, the protection depends on how you sign in. With IAM Identity Center, the browser sign-in step enforces MFA before the CLI receives any credentials. With a plain IAM user's access keys, the keys work without MFA unless a policy or a role assumption demands it.
Prefer temporary credentials
Credentials come in two kinds.
- Long-lived credentials are IAM user access keys. They start with
AKIA, never expire on their own, and stay valid until you rotate or delete them. - Temporary credentials are issued by the AWS Security Token Service (STS). They include an access key ID, a secret key, and a session token, and they expire, typically in one to twelve hours depending on the method.
Temporary credentials limit the harm of a leak, because a stolen set stops working when it expires. The ways you obtain them include:
- IAM Identity Center with
aws configure ssoandaws sso login. The CLI caches short-lived credentials and refreshes them when you sign in again. - Assuming an IAM role, which returns temporary credentials for that role.
- AWS CloudShell, which uses the credentials of the identity you signed in to the Console with. You never create or store a key.
When you must use IAM user access keys, for example with a tool that supports nothing else, follow these practices:
- Create keys only for that user and purpose, and keep at most the two keys that rotation allows.
- Rotate them regularly and delete keys that are unused.
- Never put them in source code, shell history, or screenshots.
You can check when a key was last used in the IAM console, or with the CLI:
aws iam list-access-keys --user-name alice
aws iam get-access-key-last-used --access-key-id AKIAIOSFODNN7EXAMPLEThe key ID above is the placeholder used in AWS documentation.
Where CLI credentials live
The CLI looks for credentials in a fixed order. The first source that provides them wins:
- Command-line options such as
--profile. - Environment variables, for example
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_SESSION_TOKEN, andAWS_PROFILE. - The shared files
~/.aws/credentialsand~/.aws/config, including SSO and role-based profiles. - Container or instance credentials, such as an IAM role attached to an EC2 instance.
This order explains a common surprise. If a stale AWS_ACCESS_KEY_ID is exported in your shell, it overrides the profile you thought you were using. Always confirm the active identity before you run anything that changes resources:
aws sts get-caller-identity --profile devThe response shows the account ID, the user or role ARN, and the user ID. Compare the account ID to the one you intend to work in.
The ~/.aws/credentials file stores secrets in plain text. Restrict it to your user, and keep it out of backups and version control:
chmod 600 ~/.aws/credentials ~/.aws/configAdd .aws/ and any .env files to .gitignore. Secret scanners on code hosts catch many leaked keys, but by the time they alert you, the key may already be in use.
Apply least privilege
Least privilege means granting only the permissions needed for a task, and no more. Start with none and add permissions as needed, instead of starting wide and trying to remove them.
Practical steps:
- Give people and workloads separate identities. Do not share one user.
- Use groups, or permission sets in IAM Identity Center, to assign permissions, not per-user policies.
- Prefer a read-only role for exploration. The
ReadOnlyrole in the sampledevprofile is an example. - Scope policies to specific actions and resources. An action such as
s3:GetObjecton one bucket is safer thans3:*on*. - Use separate accounts or roles for production and development, so a mistake in a test session cannot touch production.
- Review unused permissions. IAM shows when a service was last accessed by a user or role, which helps you trim policies.
Here is a policy that allows reading from a single bucket and nothing else:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:ListBucket"],
"Resource": [
"arn:aws:s3:::amzn-s3-demo-bucket",
"arn:aws:s3:::amzn-s3-demo-bucket/*"
]
}
]
}Notice that s3:ListBucket applies to the bucket ARN and s3:GetObject applies to the objects inside it, which is why both resource lines appear.
Require MFA for sensitive actions
A policy can deny actions when the session was not authenticated with MFA. This statement denies stopping EC2 instances unless MFA was used:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Action": "ec2:StopInstances",
"Resource": "*",
"Condition": {
"BoolIfExists": { "aws:MultiFactorAuthPresent": "false" }
}
}
]
}BoolIfExists matters here. Sessions that carry no MFA information at all, such as long-lived access keys, would otherwise skip the condition and be allowed.
Safe habits in the three tools
- In the Console, sign out on shared computers, and check the account ID and Region in the account menu before you change anything.
- In the CLI, use named profiles instead of exported keys, run
aws sts get-caller-identityfirst, and use--dryrunwhere a command supports it. - In CloudShell, remember that your home directory persists. Do not save secrets there. Anyone who gains access to your signed-in session can read those files.
Monitor and respond
Turn on AWS CloudTrail, which records API calls from all three tools, so you can answer who did what and when. If you suspect a leaked key, act in this order:
- Make the key inactive with
aws iam update-access-key --user-name alice --access-key-id AKIAIOSFODNN7EXAMPLE --status Inactive. - Review CloudTrail for activity from that key.
- Delete the key, and replace it with a temporary-credential method if you can.
- Check for resources you did not create, such as unfamiliar EC2 instances or IAM users.
Deactivating before deleting lets you confirm that nothing legitimate depended on the key, and you can reactivate it if something did.
Exercise
Your CLI commands succeed, but they are running against the wrong account. aws sts get-caller-identity shows an account ID you do not recognize, even though you ran aws sso login --profile dev. Name one likely cause and how you would confirm and fix it.
Show answer
Environment variables probably override the profile. A stale AWSACCESSKEYID, AWSSECRETACCESSKEY, or AWSSESSIONTOKEN exported in the shell takes precedence over the profile files. Confirm by running env | grep AWS_ and then unset the variables with unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN. Rerun aws sts get-caller-identity --profile dev and check that the account ID matches the one you expect.
Exercise
Why does the MFA deny policy use BoolIfExists instead of Bool for aws:MultiFactorAuthPresent?
Show answer
Some sessions, such as those from long-lived IAM user access keys, do not include the aws:MultiFactorAuthPresent key at all. With Bool, the condition would not match when the key is missing, so the Deny would not apply and the action would be allowed. BoolIfExists treats a missing key as matching, so the Deny applies to those sessions too.