'Access denied when trying to do AWS s3 ls using AWS cli

I launched an ec2 instance and created a role with a full S3 access policy for the instance. I installed awscli on it and configured my user's access key. My user has admin access and full S3 access policy too. I can see the buckets in the aws console but when I try to run aws s3 ls on the instance it returned An error occurred (AccessDenied) when calling the ListBuckets operation: Access Denied.

What else I need to do to add permission to the role or my user properly to be able to list and sync object between S3 and the instance?



Solution 1:[1]

I ran into this issue as well.

I ran aws sts get-caller-identity and noticed that the ARN did not match what I was expecting. It turns out if you have AWS configurations set in your bash_profile or bashrc, the awscli will default to using these instead.

I changed the enviornment variables in bash_profile and bashrc to the proper keys and everything started working.

Solution 2:[2]

There appears to be confusion about when to use IAM Users and IAM Roles.

When using an Amazon EC2 instance, the best method to grant permissions is:

  • Create an IAM Role and attach policies to grant the desired permissions
  • Associate the IAM Role with the Amazon EC2 instance. This can be done at launch time, or afterwards (Actions/Instance Settings/Attach IAM Role).
  • Any application running on the EC2 instance (including the AWS CLI) will now automatically receive credentials. Do not run aws configure.

If you are wanting to grant permissions to your own (non-EC2) computer, then:

  • Create an IAM User (or use your existing one) and attach policies to grant the desired permissions
  • On the computer, run aws configure and enter the Access Key and Secret Key associated with the IAM User. This will store the credentials in ~/.aws/credentials.
  • Any application running on this computer will then use credentials from the local credentials file

Solution 3:[3]

Turns out I forgot I had to do mfa to get access token to be able to operate in S3. Thank you for everyone response.

Solution 4:[4]

Create a IAM user with permission.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::bucketName/*"
        }
    ]
}

Save Access key ID & Secret access key.

sudo apt install awscli
aws configure
AWS Access Key ID [None]: AKIAxxxxxxxxxxxZI4
AWS Secret Access Key [None]: 8Bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx8
Default region name [None]: region (ex. us-east-2)
Default output format [None]: json

aws s3 ls s3://s3testingankit1/

Solution 5:[5]

This problem can occurs not only from the CLI but also when executing S3 API for example.

The reason for this error can come from wrong configuration of the access permissions to the bucket.

For example with the setup below you're giving a full privileges to perform actions on the bucket's internal objects, BUT not specifying any action on the bucket itself:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:*"
            ],
            "Resource": [
                "arn:aws:s3:::<name-of-bucket>/*"
            ]
        }
    ]
}

This will lead to the mentioned

... (AccessDenied) when calling the ListBuckets ...

error.

In order to fix this you should allow application to access the bucket (1st statement item) and to edit all objects inside the bucket (2nd statement item):

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::<name-of-bucket>"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::<name-of-bucket>/*"
            ]
        }
    ]
}

There are shorter configurations that might solve the problem, but the one specified above tries also to keep fine grained security permissions.

Solution 6:[6]

I ran into this yesterday running a script I ran successfully in September 2021.

TL;DR: add --profile your.profile.name to the end of the command

I have multiple profiles on the login I was using. I think something in the aws environment changed, or perhaps I had done something that was able to bypass this before. Back in September I set the profile with

aws configure set region us-west-2 --profile my.profile.name

But yesterday, after the failure, I saw that aws sts get-caller-identity was returning a different identity. After some documentation search I found the additional method for specifying the profile, and operations like:

aws s3 cp myfile s3://my-s3-bucket --profile my.profile.name

all worked

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 John Rotenstein
Solution 3 bliu
Solution 4 Ankit Kumar Rajpoot
Solution 5 RtmY
Solution 6 jpa57