Sub topic 18
Analyzing and troubleshooting access
This chapter assumes you know the policy evaluation rules (explicit deny, then allow, otherwise implicit deny). It also assumes you can read policy JSON and run the AWS CLI. Here you will apply those rules to real failures. You will read denial messages, check your identity, simulate requests, and use CloudTrail and IAM Access Analyzer to find the cause.
Start with the denial message
Most AWS services return an error that names the principal, the action, and the resource. Newer messages also say why the request was denied. The wording tells you which policy type to look at first.
| Message fragment | Meaning | Where to look |
|---|---|---|
| "because no identity-based policy allows the action" | Implicit deny. Nothing grants the action. | Policies attached to the user, group, or role |
| "with an explicit deny in an identity-based policy" | A Deny statement matched. | Deny statements on the identity |
| "with an explicit deny in a service control policy" | An organization SCP blocks it. | SCPs on the account and its OUs |
| "because no resource-based policy allows the action" | The resource owner has not granted access. | The bucket, key, or queue policy |
| "with an explicit deny in a permissions boundary" | The boundary blocks it. | The boundary on the user or role |
Not every service gives this detail. Some older messages only say AccessDenied or UnauthorizedOperation. In that case, use the tools below.
Confirm who you are
A common cause of "my permissions are wrong" is that the request is not running as the identity you think. Environment variables, a named profile, or a leftover assumed-role session can override your login. Check first.
aws sts get-caller-identityIf the ARN contains assumed-role/ReadOnlyAuditor/audit-session, the permissions come from that role. They do not come from the user who assumed it. Session credentials in AWS_SESSION_TOKEN take precedence over a profile, so unset them to go back to your original identity.
Walk the evaluation in order
Work through the layers in the same order AWS does. Stop at the first one that explains the failure.
Cross-account requests need extra care. The caller's account and the resource's account must both allow the request, unless the caller assumes a role in the resource's account. In that case only the role's own account matters.
Simulate the request
The IAM policy simulator evaluates policies without making the real call. It is available in the console and through the CLI. This command tests whether dev-user can read one object.
aws iam simulate-principal-policy \
--policy-source-arn arn:aws:iam::111122223333:user/dev-user \
--action-names s3:GetObject \
--resource-arns arn:aws:s3:::amzn-s3-demo-bucket/report.csv \
--query 'EvaluationResults[].[EvalActionName,EvalDecision]' \
--output tableThe decision is allowed, implicitDeny, or explicitDeny. Those are the same three outcomes you learned in the evaluation chapter. Keep these limits in mind:
- The simulator does not know the real request context. If a policy uses
aws:SourceIporaws:MultiFactorAuthPresent, pass values with--context-entries, or the condition may evaluate differently than in production. - It evaluates identity-based policies and permissions boundaries. SCPs and session policies are only considered when you supply them in the simulation.
- It does not include resource-based policies unless you pass one with
--resource-policy.
A simulation that says allowed while the real call fails usually means an SCP, a session policy, or a resource policy that the simulation did not see.
Read the trail in CloudTrail
CloudTrail records IAM and most service API calls. A denied call is logged with an errorCode such as AccessDenied or Client.UnauthorizedOperation. The record also shows the caller, the source IP, the time, and the exact action name. That makes it the best way to find out what was really attempted.
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=EventName,AttributeValue=StopInstances \
--max-results 5lookup-events covers the last 90 days of management events in one Region. For data events, such as S3 object reads, you need a trail or CloudTrail Lake that has them enabled. Compare the userIdentity block in the record with what you expected. For an assumed role, userIdentity.arn shows the role session, and sessionContext shows who assumed it.
Decode encoded EC2 messages
Some EC2 failures return a long encoded string beginning with "Encoded authorization failure message". Decode it with STS.
aws sts decode-authorization-message \
--encoded-message <encoded message from the error> \
--query DecodedMessage \
--output textThe caller needs the sts:DecodeAuthorizationMessage permission. The decoded JSON lists the matched statements and the context values of the request.
Use IAM Access Analyzer
IAM Access Analyzer covers four jobs.
- External access findings. An analyzer with an account or organization zone of trust reports resources shared outside that zone. It checks S3 buckets, IAM role trust policies, KMS keys, and similar resources. A finding for a role means its trust policy lets an outside principal assume it. A cross-account bucket policy like the partner example earlier would appear as a finding.
- Unused access findings. An unused access analyzer reports roles and users with unused permissions, unused roles, and unused access keys or passwords. It helps you remove grants that nobody exercises.
- Policy validation. It checks a policy against the grammar and best practices. It reports errors, security warnings such as
Resource: "*"withiam:PassRole, and suggestions. - Policy generation. It reads CloudTrail activity for a role over a chosen period and drafts a policy that covers only the actions used.
A generated policy is a starting point. Rarely used actions, such as a yearly backup job, may be missing from the period you analyzed. Review the draft before attaching it.
Check last accessed information
IAM tracks when each service was last used by an identity. This data shows which grants can be removed.
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 command>The first command returns a JobId. The second returns the services with a LastAuthenticated time. For many services, action-level data is available too. A service with no recent access is a good candidate for removal.
Common causes of unexpected denials
- Wrong resource ARN.
s3:ListBucketneeds the bucket ARN, ands3:GetObjectneedsbucket/*. Swapping them is the most common S3 policy bug. - A missing
IfExistsoperator. ADenywithBoolonaws:MultiFactorAuthPresentdoes not match requests that lack the key.BoolIfExistsdoes. - Session policies. An
AssumeRolecall with a--policyargument narrows the session to the intersection of the role's permissions and that policy. - Propagation delay. IAM is eventually consistent. A policy change can take a few seconds to apply everywhere, so do not retry in a tight loop.
- Encryption keys. Reading an object encrypted with a customer managed KMS key needs
kms:Decrypton the key, in addition to the S3 permission.
Exercise
A user can call aws s3 ls s3://amzn-s3-demo-bucket but gets AccessDenied on aws s3 cp s3://amzn-s3-demo-bucket/report.csv .. simulate-principal-policy for s3:GetObject on that object returns allowed. Name two places you would look next, and say how you would confirm the actual cause.
Show answer
The simulator only saw the identity-based policies, so look at layers it may not have evaluated: an SCP or session policy that denies s3:GetObject, a bucket policy with a Deny, or a KMS key policy if the object is encrypted with a customer managed key. Run aws sts get-caller-identity to confirm the real identity and any assumed-role session. Then find the denied GetObject event in CloudTrail and read its error message and userIdentity. The message usually states which policy type produced the denial.