Sub topic 10
Applying least privilege from the start
This chapter assumes you have finished the earlier chapters. The root user has MFA and no access keys, and you have a separate administrative identity for daily work. You should also be comfortable reading JSON.
What least privilege means
Least privilege means an identity gets only the permissions it needs for its task, and nothing more. An identity is a user or role that can sign in or make API calls. If one of those identities is compromised or makes a mistake, the damage is limited to what it was allowed to do.
Your daily admin identity is already more powerful than most tasks require. Treat it as the identity you use to create narrower ones.
Start narrow, then widen
New identities in IAM (AWS Identity and Access Management) have no permissions until you grant them. This is called an implicit deny. Use that default and add access only when a real task fails.
Write a scoped policy
A policy is a JSON document that lists allowed actions on named resources. This policy lets an identity list one bucket and read its objects. It grants no write or delete access and covers no other bucket.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListBucket",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::example-tutorial-bucket"
},
{
"Sid": "ReadObjects",
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-tutorial-bucket/*"
}
]
}Save it as policy.json. Then create the policy and attach it to a group. Replace the account ID with your own.
aws iam create-policy \
--policy-name tutorial-bucket-read \
--policy-document file://policy.json
aws iam attach-group-policy \
--group-name readers \
--policy-arn arn:aws:iam::111122223333:policy/tutorial-bucket-readAttaching policies to groups or roles, not to individual users, keeps permissions easy to review. You change one place instead of many.
Common mistakes
- Wildcards everywhere.
"Action": "*"with"Resource": "*"is full administrator access. Use it only for the admin identity, never as a shortcut for a script or an application. - Granting
AdministratorAccessto fix an error. An access-denied message names the missing action. Add that action instead. - Reusing one identity for many jobs. Give each application or person its own identity so you can narrow or remove access without side effects.
- Never reviewing. Permissions tend to grow and rarely shrink. Check them from time to time.
Confirm which identity you are using
Before you test a restricted identity, check who your CLI credentials belong to. This prevents you from testing with the wrong, more powerful identity.
aws sts get-caller-identityThe Arn field in the output shows the user or role. It should not end in :root.
Exercise
A script fails with an access-denied error on s3:PutObject. A teammate suggests attaching AdministratorAccess to the script's identity so it works. What should you do instead, and why?
Show answer
Add only s3:PutObject, and only for the specific bucket or prefix the script writes to (for example arn:aws:s3:::example-tutorial-bucket/uploads/*). Then run the script again and repeat only if it is denied again. AdministratorAccess would fix the error, but it would also let a bug or a leaked credential in that script change or delete anything in the account.