Skip to content

AWS CLI AccessDenied for S3 Buckets Despite Successful Access via AWS Console

0

I am working with multiple AWS accounts, where I have IAM access to both a source account (6963451289992) and a target account (196354236187).

Task Objective:
Most of my infrastructure like EC2 machines etc., are in target account. I have a task wherein I upload files from EC2 machines to S3 (source account). Since EC2 machines have configuration and necessary roles upload files from machines (EC2) to S3 (source account) happens without any issues.

Now, my task is that I have to download the files from S3 (source account) to another EC2 machine (target account).

To simplify this we have written SSM Document but it requires input of the file name which I uploaded initially (EC2 (target) to S3 (source)).

I have view, download and upload access in S3 in both accounts (source and target) and can do it via Console but when I try to find the list of buckets or files or anything in S3 of source account I'm getting an error.

Here’s my setup:

  1. IAM User: I log in using an IAM user in account 6963451289992.

  2. Role Switching: I have a profile (high-access) in my CLI configuration to assume a role in the target account (196354236187).

  3. AWS CLI Configuration:

    [default]
    region = us-east-2
    output = text
    role_arn = arn:aws:iam::6963451289992:user/user.name
    mfa_serial = arn:aws:iam::6963451289992:mfa/User.Name
    source_profile = default
    
    [profile high-access]
    role_arn = arn:aws:iam::196354236187:role/high-access
    mfa_serial = arn:aws:iam::6963451289992:mfa/User.Name
    source_profile = default
    region = us-east-2
    

When I run aws s3 ls or attempt to access a specific bucket via the CLI of source account, I encounter errors:

1. List All Buckets:

An error occurred (AccessDenied) when calling the ListBuckets operation: User: arn:aws:iam::6963451289992:user/user.name is not authorized to perform: s3:ListAllMyBuckets with an explicit deny in an identity-based policy.

2. Access Specific Bucket:

aws s3 ls s3://bucket-i-am-trying 

Result:

An error occurred (AccessDenied) when calling the ListObjectsV2 operation: User: arn:aws:iam::6963451289992:user/user.name is not authorized to perform: s3:ListBucket on resource: arn:aws:s3:::bucket-i-am-trying with an explicit deny in an identity-based policy.

Important Notes:

  • I can see and access the S3 buckets through the AWS Console without issues.
  • The CLI configuration uses MFA, and I verified that the correct role and credentials are being assumed.

What I did to resolve the issue ?

Here are the steps I have already tried, along with my expectations:

  1. Validated Console vs. CLI Behavior:

    • I confirmed that I can access the S3 buckets in the AWS Console without errors.
    • Expectation: The same access should work via the CLI.
  2. Verified CLI Profile:

    • Used aws sts get-caller-identity to confirm the correct role/profile is being used.
    • Cleared cached credentials in ~/.aws/cli/cache and ensured MFA configuration was correct.
    • Expectation: Running aws s3 ls should list the buckets.
  3. Tested Access to Specific Buckets:

    • Tried listing objects in specific buckets directly using aws s3 ls s3://bucket-name.
    • Expectation: Access should succeed, as it does in the Console.
  4. Debugged CLI Output:

    • Ran commands with --debug to confirm the correct API calls and role assumptions.
    • Expectation: The debug output would reveal a misconfiguration, but none was found.
  5. Policy Review:

    • My IAM user has the following permissions:
      • IAMReadOnlyAccess
      • IAMUserChangePassword
    • The user is subject to a permissions boundary (user-with-boundaries) that includes s3:* on "Resource": "*".
    • Expectation: No explicit deny should exist to block S3 access, but errors indicate otherwise.
  • Why can I access S3 buckets in the AWS Console but not via the CLI?

  • What additional steps or configurations should I check to resolve this mismatch, especially since the task involves both accounts (source account and target account) and I want to automate the process without constantly switching roles?

I asked my AWS admin to review but they are asking me to provide exact error details next time I reach out to him again because he is saying that I'm able to access S3 buckets via console.

3 Answers
1

Hello.

Try changing the "default" profile as below.
"role_arn" is an option that specifies the ARN of the switch role instead of the ARN of the IAM user.
Judging from the error message, it looks like the switch is for "arn:aws:iam::6963451289992:user/user.name".
If the switch role is successful, the error message will include the string "arn:aws:iam::196354236187:role/high-access".

[default]
region = us-east-2
output = text

[profile high-access]
role_arn = arn:aws:iam::196354236187:role/high-access
mfa_serial = arn:aws:iam::6963451289992:mfa/User.Name
source_profile = default
region = us-east-2

By the way, when performing a switch role using the "--profile" option, the following IAM policy must be set for the IAM user.
I think there is no problem since the switch role is possible from the management console, but please check to be sure.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "sts:AssumeRole"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

Also, check out things like CloudTrail.
You may be able to identify the problem from the history of operations performed by "arn:aws:iam::6963451289992:user/user.name".
https://docs.aws.amazon.com/awscloudtrail/latest/userguide/tutorial-event-history.html

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

  • Hi Thanks for your answer, in the initial question I mentioned the issue incorrectly. I have no trouble accessing S3 buckets of target account via AWS CLI using --profile high-access.

    But my problem lies in accessing buckets of source account.

    I've updated my question to reflect the same, Can you please check that once ?

1

Hi,

I strongly suggest that you follow the guidance of this KC article to set your cross-account bucket access properly: https://repost.aws/knowledge-center/cross-account-access-s3

In particular, be careful on the definition of the various and required IAM policies described in this article.

Best,

Didier

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

0

The issue you're experiencing appears to be related to the way permissions are being applied when using the AWS CLI versus the AWS Console. This discrepancy often occurs due to differences in how roles and policies are evaluated in these two contexts.

Here are some potential reasons and steps to resolve the issue:

  1. Role Assumption: When using the CLI, make sure you're actually assuming the role in the target account. The error messages suggest you're still operating as your IAM user in the source account. Try explicitly assuming the role before running S3 commands:

    aws sts assume-role --role-arn arn:aws:iam::196354236187:role/high-access --role-session-name MySession

    Then use the temporary credentials provided in the output.

  2. S3 Bucket Policy: Check the S3 bucket policy in the target account. It may need to explicitly allow access from the assumed role in the target account. Add a statement like:

    { "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::196354236187:role/high-access" }, "Action": ["s3:ListBucket", "s3:GetObject"], "Resource": [ "arn:aws:s3:::bucket-i-am-trying", "arn:aws:s3:::bucket-i-am-trying/*" ] }

  3. Cross-Account Access: Ensure that the role in the target account (high-access) has a trust relationship allowing it to be assumed by your IAM user or a role in the source account.

  4. Permissions Boundary: The permissions boundary (user-with-boundaries) you mentioned might be restricting CLI access. Review this boundary to ensure it's not inadvertently blocking S3 actions when using the CLI.

  5. Session Duration: If your assumed role session is timing out, you may need to increase the session duration or implement a solution to refresh the credentials automatically.

  6. Use AWS SSO: Consider setting up AWS SSO (Single Sign-On) for easier management of cross-account access, which can simplify the process of switching between accounts and roles.

To troubleshoot further:

  • Use 'aws sts get-caller-identity' after assuming the role to verify you're operating as the intended role.
  • Check CloudTrail logs in both accounts to see exactly what identity is making the S3 requests and any associated errors.
  • Review the IAM Access Analyzer findings for potential issues with your policies.

Remember, the console might be using a different authentication method or role, which could explain the discrepancy. Always ensure your CLI is configured to use the same permissions as your console access.
Sources
Access Denied with S3 SDK | AWS re:Post
Access Denied for boto3 list_buckets operation when assuming cross account role from sagemaker notebook | AWS re:Post

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.