Sub topic 13
Generating least-privilege policies from CloudTrail
Unused access findings tell you what to remove. Policy generation works the other way. It reads what a role did and drafts a policy that allows only those actions. This chapter assumes you know IAM roles, identity-based policies, and basic CloudTrail concepts (trails, management events, data events).
What policy generation does
Access Analyzer can look at the CloudTrail activity of one IAM user or role over a chosen period. It then produces an identity-based policy containing the actions that principal called. You use it when a role was created with broad permissions such as AdministratorAccess or s3:*, and you now want a policy that matches reality.
- The period can cover up to 90 days of CloudTrail events.
- Access Analyzer reads the events from the trail's S3 bucket. It needs a service role that is allowed to read that bucket.
- The result is a draft. You review it before attaching it to anything.
Starting a generation job
Generation runs as an asynchronous job. You supply the principal, the trail, the date range, and the role Access Analyzer assumes to read the logs.
aws accessanalyzer start-policy-generation \
--policy-generation-details '{"principalArn":"arn:aws:iam::111122223333:role/app-backend"}' \
--cloud-trail-details '{
"trails": [{
"cloudTrailArn": "arn:aws:cloudtrail:us-east-1:111122223333:trail/management-trail",
"regions": ["us-east-1"],
"allRegions": false
}],
"accessRole": "arn:aws:iam::111122223333:role/AccessAnalyzerMonitorServiceRole",
"startTime": "2026-08-30T00:00:00Z",
"endTime": "2026-11-26T00:00:00Z"
}'The command returns a jobId. Check progress and fetch the result with it:
aws accessanalyzer list-policy-generations \
--principal-arn arn:aws:iam::111122223333:role/app-backend
aws accessanalyzer get-generated-policy \
--job-id JOB_ID \
--include-resource-placeholders \
--query 'generatedPolicyResult.generatedPolicies[0].policy' \
--output textThe job status moves from IN_PROGRESS to SUCCEEDED, or to FAILED or CANCELED. The get-generated-policy call returns the policy only after success.
Reading the draft
Two details shape the draft policy.
Action level versus service level. For many services, the draft lists the exact actions seen in the logs, such as s3:GetObject. For services without action-level generation, the draft may only show the service. Check each statement.
Resources. With --include-resource-placeholders, unknown resources appear as placeholders such as arn:aws:s3:::${BucketName}/*. Replace them with real ARNs. Without them, you may get "Resource": "*", which is broader than you want.
Limits to know
- Only logged actions appear. S3 object-level calls such as
GetObjectare data events. If your trail records only management events, those calls are missing from the draft, and the role would lose access if you applied it as is. - A quiet period hides rare jobs. A quarterly report job that did not run inside the window will not appear.
- Conditions are not inferred. The draft contains no
Conditionblocks, so add any you need. - A role that never ran the action cannot show it. Compare the draft with the unused access findings for the same role before you decide.
A safe rollout
- Confirm the trail logs the event types the workload uses, including data events if needed.
- Generate a policy over the longest period that covers your rarest job.
- Replace placeholders and add conditions.
- Run
validate-policyon the result. - Use
check-no-new-accessto confirm the new policy grants nothing beyond the old one. - Attach, monitor for denials, then detach the broad policy.
Exercise
A generated policy for a role that processes S3 uploads contains no s3:GetObject statement, yet the application reads objects every day. What is the most likely cause, and what do you change?
Show answer
The trail records only management events, so S3 object-level calls were never logged. Enable data events for the relevant bucket, wait for new activity to accumulate, and generate the policy again. Until then, do not use the draft.