aws lambda function getting access denied when getObject from s3

后端 未结 12 1517
离开以前
离开以前 2020-12-08 06:44

I am getting an acccess denied error from S3 AWS service on my Lambda function.

This is the code:

// dependencies
var async = require(\'async\');
var         


        
相关标签:
12条回答
  • 2020-12-08 07:13

    If all the other policy ducks are in a row, S3 will still return an Access Denied message if the object doesn't exist AND the requester doesn't have ListBucket permission on the bucket.

    From https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html:

    ...If the object you request does not exist, the error Amazon S3 returns depends on whether you also have the s3:ListBucket permission.

    If you have the s3:ListBucket permission on the bucket, Amazon S3 will return an HTTP status code 404 ("no such key") error. if you don’t have the s3:ListBucket permission, Amazon S3 will return an HTTP status code 403 ("access denied") error.

    0 讨论(0)
  • 2020-12-08 07:15

    If you have encryption set on your S3 bucket (such as AWS KMS), you may need to make sure the IAM role applied to your Lambda function is added to the list of IAM > Encryption keys > region > key > Key Users for the corresponding key that you used to encrypt your S3 bucket at rest.

    In my screenshot, for example, I added the CyclopsApplicationLambdaRole role that I have applied to my Lambda function as a Key User in IAM for the same AWS KMS key that I used to encrypt my S3 bucket. Don't forget to select the correct region for your key when you open up the Encryption keys UI.

    Find the execution role you've applied to your Lambda function:

    Find the key you used to add encryption to your S3 bucket:

    In IAM > Encryption keys, choose your region and click on the key name:

    Add the role as a Key User in IAM Encryption keys for the key specified in S3:

    0 讨论(0)
  • 2020-12-08 07:18

    I was trying to read a file from s3 and create a new file by changing content of file read (Lambda + Node). Reading file from S3 did not had any problem. As soon I tried writing to S3 bucket I get 'Access Denied' error.

    I tried every thing listed above but couldn't get rid of 'Access Denied'. Finally I was able to get it working by giving 'List Object' permission to everyone on my bucket.

    Obviously this not the best approach but nothing else worked.

    0 讨论(0)
  • 2020-12-08 07:21

    If you are specifying the Resource don't forget to add the sub folder specification as well. Like this:

    "Resource": [
      "arn:aws:s3:::BUCKET-NAME",
      "arn:aws:s3:::BUCKET-NAME/*"
    ]
    
    0 讨论(0)
  • 2020-12-08 07:21

    I too ran into this issue, I fixed this by providing s3:GetObject* in the ACL as it is attempting to obtain a version of that object.

    0 讨论(0)
  • 2020-12-08 07:21

    I was struggling with this issue for hours. I was using AmazonS3EncryptionClient and nothing I did helped. Then I noticed that the client is actually deprecated, so I thought I'd try switching to the builder model they have:

    var builder = AmazonS3EncryptionClientBuilder.standard()
      .withEncryptionMaterials(new StaticEncryptionMaterialsProvider(encryptionMaterials))
    if (accessKey.nonEmpty && secretKey.nonEmpty) builder = builder.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey.get, secretKey.get)))
    builder.build()
    

    And... that solved it. Looks like Lambda has trouble injecting the credentials in the old model, but works well in the new one.

    0 讨论(0)
提交回复
热议问题