SecurityAWS
AWS IAM least privilege: from wildcards to exact actions
IAM records when each role last used a service, and CloudTrail records its API calls. Start from those two records and cut a wildcard policy down to what the role uses.

Start from recorded activity
The IAM best practices page allows broad permissions while you explore what a workload requires, and tells you to reduce them as the use case matures. Two records show what to keep. IAM provides last accessed information for users, roles, groups and policies. CloudTrail logs the API calls, and IAM Access Analyzer can generate a policy from them.
Last accessed information
For each service that a role's policies allow, IAM reports the date and time of the last attempt to access it. Service activity is tracked for a minimum of 400 days. The period is shorter in a Region where tracking started less than 400 days ago. For the services AWS lists, it also reports the last attempt for each management action. For data plane events, IAM reports no action last accessed information at all.
aws iam generate-service-last-accessed-details \
--arn arn:aws:iam::111122223333:role/my-app-role \
--granularity ACTION_LEVEL
aws iam get-service-last-accessed-details --job-id <job-id>In the report, LastAuthenticated is null when no IAM entity attempted to access the service within the tracking period. Remove those services first. The data includes all attempts to access an AWS API, including the unsuccessful ones, so an entry does not show that a call succeeded.
Policy generation from CloudTrail
IAM Access Analyzer reviews the CloudTrail events of one user or role over a period of up to 90 days and generates a policy from them. You specify the trail and a service role that gives Access Analyzer access to the trail and to last accessed information.
aws accessanalyzer start-policy-generation \
--policy-generation-details principalArn=arn:aws:iam::111122223333:role/my-app-role \
--cloud-trail-details file://trail.json
aws accessanalyzer get-generated-policy --job-id <job-id>trail.json holds the trail ARN, the service role in accessRole and a startTime. In the console, the feature is on the role's Permissions tab. AWS lists these limits:
- For data events such as Amazon S3 data events, it reports no activity at the level of individual actions.
iam:PassRoleis not tracked by CloudTrail and is not included.- For some services it identifies only the service and prompts you to add the actions.
- It reviews all CloudTrail events, including denied actions, so read every action before you keep it.
The generated template leaves resource ARNs for you to fill in. Use * only for actions that do not support resource-level permissions.
One policy before and after
Before, the policy allows every S3 action on every resource:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": "*"
}
]
}After, it follows the AWS example for read and write access to the objects in one bucket. The AWS example grants s3:*Object. This version names two object actions in its place:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListObjectsInBucket",
"Effect": "Allow",
"Action": ["s3:ListBucket"],
"Resource": ["arn:aws:s3:::my-app-data"]
},
{
"Sid": "ReadAndWriteObjects",
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": ["arn:aws:s3:::my-app-data/*"]
}
]
}Conditions that narrow a grant
A condition sets when a policy statement is in effect. Four global condition keys:
aws:SecureTransportchecks whether the request was sent using TLS.aws:RequestedRegionchecks the Region the request called against the Region you name.aws:ResourceTag/tag-keycompares a tag key and value in the policy with the tag attached to the resource.aws:PrincipalOrgIDcompares the organization of the requesting principal with the ID you specify. AWS notes that it simplifies thePrincipalelement of a resource-based policy.
Permissions boundaries
A permissions boundary is a managed policy. It caps what an identity-based policy can grant to a user or a role. It grants no permissions on its own. The effective permissions are the intersection of the boundary and the identity-based policies.
aws iam put-role-permissions-boundary \
--role-name my-app-role \
--permissions-boundary arn:aws:iam::111122223333:policy/my-boundaryYou can use boundaries to delegate permissions management. In the AWS example, a delegated user may create IAM users only when he sets a named policy as their permissions boundary. His policy tests the iam:PermissionsBoundary condition key.
Validation and rollout
aws accessanalyzer validate-policy \
--policy-document file://policy.json \
--policy-type IDENTITY_POLICYIAM Access Analyzer provides more than 100 policy checks. The command returns findings of four types: ERROR, SECURITY_WARNING, WARNING and SUGGESTION. An error reports a part of the policy that is not functional. A security warning reports access that AWS considers overly permissive. Fix both before you attach the policy. In the console, Access Analyzer gives the same recommendations as you author or edit a policy.
Then work in this order:
- Remove the services that last accessed information shows as unused.
- Generate a policy. It contains only what the role used in the date range you chose, and that range is 90 days at most.
- Fill in the resource ARNs and add conditions.
- Validate the policy and test it before production, as AWS advises.
- After you attach it, remove the broad policy. CloudTrail is the authoritative record of whether each call succeeded or was denied.
Sources
- Security best practices in IAM
- Refine permissions in AWS using last accessed information
- IAM Access Analyzer policy generation
- Grant read and write access to Amazon S3 bucket objects
- AWS global condition context keys
- Permissions boundaries for IAM entities
- AWS CLI Command Reference: start-policy-generation
- AWS CLI Command Reference: validate-policy
Want a second pair of eyes on yours? We do this work for a living.
More guides

AWS STS and AssumeRole: how short-lived credentials work
Authentication · 4 min read
Trust policy, permissions policy, session duration, the external ID and the one-hour limit on role chaining.

Service control policies: guardrails for every AWS account
Security · 4 min read
What SCPs restrict, what they skip, three example policies and a rollout on one OU first.

CloudTrail, Azure Activity Log and Cloud Audit Logs: turn them on first
Security · 4 min read
What each cloud logs by default, how long it keeps the record and where to send a copy.