Sub topic 17
Working toward least privilege
Least privilege means a principal gets only the permissions its tasks need, and nothing more. This chapter assumes you can read policy documents (Effect, Action, Resource, Condition). It also assumes you know how policy evaluation combines Allow, Deny, and implicit deny, and that you know what permissions boundaries and SCPs do. You also need basic AWS CLI usage.
Why least privilege is a process
You rarely know the exact permissions a workload needs on day one. Developers often start with AdministratorAccess or a wildcard such as "Action": "s3:*" to get unblocked and never come back. Each unused permission is extra risk. A leaked access key, a compromised instance, or a mistaken script can then do far more than the job ever required.
Treat least privilege as a loop. Start with a reasonable scope, observe what the principal actually uses, remove the rest, and repeat as the workload changes.
Narrow each element of a statement
A statement has four levers. Tighten each one.
Actions
List the specific API actions needed instead of service:*. Reading objects from S3 needs s3:GetObject, not s3:*. Wildcards in action names are sometimes reasonable, such as ec2:Describe* for read-only discovery. Check that a wildcard does not also match write or delete actions.
Resources
Name the ARNs the workload touches. Remember from the S3 example earlier in this tutorial that bucket-level actions and object-level actions use different ARNs. A policy with "Resource": "*" applies to every resource of that type in the account, and some actions only support *. Look up the supported resource types for each action in the service authorization reference.
Conditions
Conditions limit when a permission applies. Useful keys include aws:SourceIp, aws:SourceVpc, aws:PrincipalOrgID, aws:RequestedRegion, aws:MultiFactorAuthPresent, and aws:SecureTransport. Conditions also let you scope by tags, so one policy can serve many resources.
Principals
For resource-based policies and trust policies, avoid broad principals. A trust policy that names an account root (arn:aws:iam::111122223333:root) trusts the whole account, so any identity there with sts:AssumeRole permission can assume the role. Name a specific role ARN when you can.
Here is a policy for an application that only reads reports under one prefix and only over TLS:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListReportsPrefix",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::amzn-s3-demo-bucket",
"Condition": {
"StringLike": { "s3:prefix": ["reports/*"] }
}
},
{
"Sid": "ReadReports",
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::amzn-s3-demo-bucket/reports/*",
"Condition": {
"Bool": { "aws:SecureTransport": "true" }
}
}
]
}The first statement limits listing to keys that start with reports/. The second limits reads to the same prefix and rejects plain HTTP requests.
Start from evidence, not guesses
IAM records what principals do, and you can use that record to prune permissions. Three tools matter.
| Tool | What it tells you | Typical use |
|---|---|---|
| Last accessed information | Which services (and for some services, actions) a user or role used, and when | Remove whole services nobody uses |
| IAM Access Analyzer policy generation | A draft policy built from CloudTrail events for a principal | Replace a broad policy with one that matches observed activity |
| Policy simulator | Whether a given principal would be allowed or denied for specific actions and resources | Test a change before deploying it |
Check last accessed data
Last accessed information is the quickest way to find unused services. Generate a report for a role, then read it with the job ID:
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>Entries without a LastAuthenticated timestamp are services the role has not used in the tracking period. Tracking covers a limited window, so do not read a missing entry as proof that a rarely run job (a yearly report, for example) never needs the permission.
Generate a policy from CloudTrail
Access Analyzer reads CloudTrail management and data events for a principal over a period you choose and drafts a policy for the actions it saw. You need a trail that stores those events and a service role that Access Analyzer can use to read the trail. Put the trail details in a file:
{
"trails": [
{
"cloudTrailArn": "arn:aws:cloudtrail:us-east-1:111122223333:trail/management-events",
"allRegions": true
}
],
"accessRole": "arn:aws:iam::111122223333:role/AccessAnalyzerMonitorServiceRole",
"startTime": "2026-08-01T00:00:00Z",
"endTime": "2026-11-01T00:00:00Z"
}Then start the job and fetch the result:
aws accessanalyzer start-policy-generation \
--policy-generation-details principalArn=arn:aws:iam::111122223333:role/AppRole \
--cloud-trail-details file://trail.json
aws accessanalyzer get-generated-policy \
--job-id <JobId from the previous response>Read the generated policy before using it. Treat it as a draft:
- It only covers activity in the chosen period, so rare operations are missing.
- Some services log only certain actions, so the draft may omit actions that CloudTrail does not record or that you did not enable data events for.
- It may use
*for resources where CloudTrail did not record a specific ARN. Replace those with real ARNs. - It does not include conditions. Add them yourself.
Test before you deploy
The policy simulator evaluates a principal's attached policies, permissions boundary, and (in some cases) SCPs against the actions you list. From the CLI:
aws iam simulate-principal-policy \
--policy-source-arn arn:aws:iam::111122223333:role/AppRole \
--action-names s3:GetObject s3:DeleteObject \
--resource-arns arn:aws:s3:::amzn-s3-demo-bucket/reports/q3.csv \
--query "EvaluationResults[].[EvalActionName,EvalDecision]" \
--output tableYou want allowed for s3:GetObject and implicitDeny for s3:DeleteObject. The simulator does not model every condition context, so a passing simulation does not replace a real test.
Use guardrails and automation to keep the gains
A policy narrowed once will drift. Combine it with controls that hold the line:
- Set a permissions boundary on roles that developers create, so a developer cannot grant more than the boundary allows.
- Use SCPs to block actions that no workload in an account or organization unit should perform, such as disabling CloudTrail.
- Prefer roles with temporary credentials over long-lived access keys, and delete access keys that last accessed data shows as unused.
- Run IAM Access Analyzer's external access findings to catch resource policies and trust policies that open access to outside principals.
- Review managed policy attachments on a schedule, and when a project ends, remove its roles.
Common mistakes
- Replacing one wildcard with another. Changing
"Action": "*"to"Action": "s3:*"is progress, but still grants delete and policy-changing actions. - Over-tightening without a test path. A narrow policy that breaks production at 2 a.m. leads people to widen it again. Test in a non-production account first, and keep the old policy version available for rollback.
- Forgetting dependent actions. Some tasks call several APIs behind one console click or SDK call. For example, launching an instance with a role needs
iam:PassRoleon that role in addition toec2:RunInstances. When a task fails after narrowing, read the error and the CloudTrail event before guessing. - Treating a generated policy as final. It describes the past, not the future.
The next chapter covers how to read denial messages and trace which policy blocked a request.
Exercise
A role's generated policy from a three-month CloudTrail window contains no s3:PutObject statement, but the application writes a year-end archive once a year. What should you do before deploying the generated policy, and why?
Show answer
Add the s3:PutObject permission (scoped to the archive bucket and prefix) to the draft, or confirm the job's schedule and extend the analysis window. The generated policy only reflects activity in the chosen period, so a rare operation is missing, and deploying it unchanged would make the yearly job fail with an implicit deny. Test the final policy with the policy simulator before deploying.
Exercise
Why is "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*" not enough to allow s3:ListBucket, and what is the least privilege way to restrict listing to one prefix?
Show answer
s3:ListBucket is a bucket-level action, so its resource is the bucket ARN (arn:aws:s3:::amzn-s3-demo-bucket), not the object ARN with /*. To restrict listing to one prefix, keep the bucket ARN as the resource and add a StringLike condition on s3:prefix, for example reports/*.