Sub topic 10
Applying least privilege from the start
This chapter assumes you have finished the earlier chapters. You have MFA on the root user, no root access keys, and a separate administrative identity for daily work. You should be able to read JSON and run AWS CLI commands. Here you stop treating that admin identity as the template for every other identity and start granting narrower permissions.
What least privilege means
Least privilege means an identity gets only the actions it needs, on only the resources it needs, and only for as long as it needs them. A person or program that can do less can also break less. If their credentials leak, the damage is limited to what those credentials could do.
A permission has three dimensions you can narrow:
- Action: the API operations allowed, such as
s3:GetObjectinstead ofs3:*. - Resource: the specific objects the action applies to, such as one bucket instead of
*. - Condition: extra requirements, such as a source IP range, a required tag, or MFA present in the session.
Starting this way costs little. Tightening permissions on a mature account means finding out what each identity actually uses, and that is much harder.
How IAM decides a request
IAM (Identity and Access Management) evaluates each request against a few rules. Every request is denied by default. An explicit Allow in an applicable policy permits it. An explicit Deny always overrides any Allow.
The diagram below is simplified. Real evaluation also considers resource policies, permission boundaries, session policies, and service control policies. Those last ones appear when you reach AWS Organizations.
This model explains a common surprise. A new IAM user with no policies can do nothing, and that is correct. Your job is to add the smallest set of allows that makes their work possible.
Your admin identity is not the pattern
The administrative identity you created has broad permissions, usually the AdministratorAccess managed policy. That is appropriate for the person who sets up and manages the account. It is the wrong starting point for everyone and everything else.
Give each kind of identity its own permissions:
- Developers and analysts: a job-specific policy, not administrator.
- Applications and automation: an IAM role with a policy for that one workload. Avoid long-lived access keys. Roles hand out temporary credentials.
- Auditors: read-only access.
Choosing a starting point
You do not have to write every policy from scratch. Most teams move through these levels.
| Level | Example | Trade-off |
|---|---|---|
| AWS managed, broad | `AdministratorAccess`, `PowerUserAccess` | Fast, but far more than most tasks need |
| AWS managed, scoped by role | `ReadOnlyAccess`, `job-function/DatabaseAdministrator` | Reasonable for early work, still account-wide |
| AWS managed, per service | `AmazonS3ReadOnlyAccess` | Limited to one service, but covers every resource in it |
| Customer managed | Your own policy naming specific resources | Tightest fit, but you maintain it |
Start at the narrowest level that still lets the person finish their task. Move to a customer managed policy once you know which resources are involved.
Writing a narrow policy
Suppose a teammate needs to read reports from one S3 bucket and do nothing else. Save this as reports-read.json. The bucket name is a placeholder, so replace it with your own.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListOneBucket",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::example-reports-bucket"
},
{
"Sid": "ReadObjects",
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-reports-bucket/*"
}
]
}The policy has two statements because the two actions apply to different resource types. s3:ListBucket acts on the bucket itself, so its ARN has no path. s3:GetObject acts on objects, so its ARN ends in /*. Putting both actions in one statement with one resource is a frequent mistake, and it leaves either listing or reading denied.
Creating and attaching the policy
Attach policies to groups or roles rather than to individual users. When someone changes jobs, you change their membership instead of editing their permissions. The commands below use the placeholder account ID 111122223333 and a user named alice.
aws iam create-policy \
--policy-name ReportsReadOnly \
--policy-document file://reports-read.json
aws iam create-group --group-name report-readers
aws iam attach-group-policy \
--group-name report-readers \
--policy-arn arn:aws:iam::111122223333:policy/ReportsReadOnly
aws iam add-user-to-group \
--group-name report-readers \
--user-name aliceIf you use IAM Identity Center for people instead of IAM users, the equivalent is a permission set assigned to a group. The idea is the same: a narrow permission set per job.
Testing before you trust it
The IAM policy simulator evaluates policies without making real calls. You can run it from the CLI. This checks whether alice may read and write one object:
aws iam simulate-principal-policy \
--policy-source-arn arn:aws:iam::111122223333:user/alice \
--action-names s3:GetObject s3:PutObject \
--resource-arns arn:aws:s3:::example-reports-bucket/q1.csv \
--query 'EvaluationResults[].{Action:EvalActionName,Decision:EvalDecision}'You should see allowed for s3:GetObject and implicitDeny for s3:PutObject. Test s3:ListBucket in a separate run with the bucket ARN as the resource, because it uses a different resource type.
The simulator checks policy logic, not the real account state. It can still say allowed when something outside the policies, such as a service control policy or a resource policy, blocks the call. Treat it as a fast first check, then confirm with a real call as the actual user.
Tightening over time
Sometimes you cannot know the right permissions up front. A workable method is to start from a scoped managed policy, let the work happen, and then trim. IAM tracks which services an identity has actually used. You can ask for that report:
aws iam generate-service-last-accessed-details \
--arn arn:aws:iam::111122223333:user/aliceThe command returns a JobId. Pass it to aws iam get-service-last-accessed-details --job-id <JobId> to see each service the identity is allowed to use and when it last used it. A service that shows no recent access is a candidate for removal. Review these reports on a schedule, for example quarterly, so permissions do not creep upward unnoticed.
Common errors
- Using
Resource: "*"by default. It works immediately, which is why it spreads. Replace it with ARNs as soon as you know them. - Mismatched ARN types.
ListBucketneeds the bucket ARN,GetObjectneeds the object ARN, as shown above. - Granting policy-editing rights casually. An identity that can attach policies can give itself administrator. Actions like
iam:AttachUserPolicy,iam:PutUserPolicy, andiam:CreateAccessKeyare privilege escalation paths. Treat them as administrator-level. - Policies on individual users. They are hard to audit and easy to forget. Use groups or roles.
- Long-lived access keys for applications. Use roles with temporary credentials where the workload runs on AWS.
- Debugging
AccessDeniedby widening permissions. Read the error first. It usually names the action and resource. Check for an explicit deny or a missing resource type before adding wildcards.
Limits of this chapter
Everything above works at the level of a single account. Two stronger controls come later. Permission boundaries cap the most an identity's policies can ever grant. Service control policies in AWS Organizations cap what whole accounts can do, even for their administrators. Neither replaces writing narrow policies, since they set ceilings and do not pick the right permissions for you.
Exercise
A teammate's policy has one statement allowing s3:ListBucket and s3:GetObject on arn:aws:s3:::example-reports-bucket/*. They can download known files but aws s3 ls s3://example-reports-bucket fails with AccessDenied. What is wrong, and how do you fix it without widening access to other buckets?
Show answer
s3:ListBucket applies to the bucket, whose ARN is arn:aws:s3:::example-reports-bucket with no /*. The policy only grants it on objects, so listing is denied by default. Split the policy into two statements: s3:ListBucket on the bucket ARN and s3:GetObject on the /* ARN. This keeps access limited to the one bucket.
Exercise
Name two reasons to attach a policy to a group or role instead of directly to an IAM user, and name one IAM action that should be treated as administrator-level even though it does not sound dangerous.
Show answer
Groups and roles make permissions easier to audit and let you change access by changing membership, without editing each user. They also avoid forgotten per-user exceptions. iam:AttachUserPolicy (also iam:PutUserPolicy or iam:CreateAccessKey) is administrator-level because an identity that holds it can grant itself or others broader permissions.