Sub topic 17
Working toward least privilege
This chapter assumes you can read IAM policy JSON, know the difference between identity-based and resource-based policies, and have used the AWS CLI. Least privilege means granting only the permissions a person or workload needs, and nothing more.
Start broad, then narrow
You rarely know the exact permissions a workload needs on day one. A practical approach is to start with a managed policy or a wider custom policy in a development account. Then use real activity to cut it down. This is a loop, not a one-time task.
Find what is unused
IAM records when each service was last used by a user, group, or role. You request a report and then fetch it with the job ID that the first call returns.
aws iam generate-service-last-accessed-details \
--arn arn:aws:iam::111122223333:role/AppRole
aws iam get-service-last-accessed-details \
--job-id <JobId from the previous response>Any service with no recent access date is a candidate for removal. Service-level data is coarse. For some services, IAM can also report last-accessed data at the action level.
Generate a policy from activity
IAM Access Analyzer can read CloudTrail logs for a user or role over a chosen period. It then drafts a policy that contains only the actions it saw. Treat the draft as a starting point:
- Actions that run rarely, such as a yearly job, may be missing from the window.
- Generated policies may use
*for some resources. Tighten theResourceelement by hand. - Add
Conditionelements, for exampleaws:MultiFactorAuthPresentoraws:SourceIp, where they fit.
Access Analyzer can also produce findings for unused access, such as unused roles, unused access keys, and unused permissions. It also reports external access, meaning resources shared outside your account or organization. Review both kinds of finding regularly.
Test before you apply
The IAM policy simulator evaluates a policy against actions and resources you choose, without making real calls. Use it to confirm that the actions you need are allowed and that sensitive ones are denied. It follows the same evaluation logic as live requests. This includes explicit deny, permissions boundaries, and service control policies, if you include them in the simulation.
Keep it maintainable
- Prefer roles with temporary credentials over long-lived access keys.
- Scope
Resourceto specific ARNs. Remember that bucket-level and object-level S3 actions need different ARNs. - Put shared permissions in groups or customer managed policies, so one fix applies everywhere.
- Use permissions boundaries to cap what delegated administrators can grant.
- Remove unused users, roles, and keys on a schedule.
Exercise
A generated policy from 30 days of CloudTrail activity omits s3:PutObject, but the application writes a backup file once every 90 days. What is the risk, and what should you do before applying the policy?
Show answer
The backup job will fail with an access denied error, because the 30-day window never saw the action. Extend the analysis period or add the known action manually. Then test the final policy in the policy simulator before applying it.