aws lambda function getting access denied when getObject from s3

后端 未结 12 1516
离开以前
离开以前 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 06:59

    I tried to execute a basic blueprint Python lambda function [example code] and I had the same issue. My execition role was lambda_basic_execution

    I went to S3 > (my bucket name here) > permissions .

    Because I'm beginner, I used the Policy Generator provided by Amazon rather than writing JSON myself: http://awspolicygen.s3.amazonaws.com/policygen.html my JSON looks like this:

    {
        "Id": "Policy153536723xxxx",
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "Stmt153536722xxxx",
                "Action": [
                    "s3:GetObject"
                ],
                "Effect": "Allow",
                "Resource": "arn:aws:s3:::tokabucket/*",
                "Principal": {
                    "AWS": [
                        "arn:aws:iam::82557712xxxx:role/lambda_basic_execution"
                    ]
                }
            }
        ]
    

    And then the code executed nicely:

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

    I ran into this issue and after hours of IAM policy madness, the solution was to:

    1. Go to S3 console
    2. Click bucket you are interested in.
    3. Click 'Properties'
    4. Unfold 'Permissions'
    5. Click 'Add more permissions'
    6. Choose 'Any Authenticated AWS User' from dropdown. Select 'Upload/Delete' and 'List' (or whatever you need for your lambda).
    7. Click 'Save'

    Done. Your carefully written IAM role policies don't matter, neither do specific bucket policies (I've written those too to make it work). Or they just don't work on my account, who knows.

    [EDIT]

    After a lot of tinkering the above approach is not the best. Try this:

    1. Keep your role policy as in the helloV post.
    2. Go to S3. Select your bucket. Click Permissions. Click Bucket Policy.
    3. Try something like this:
    {
        "Version": "2012-10-17",
        "Id": "Lambda access bucket policy",
        "Statement": [
            {
                "Sid": "All on objects in bucket lambda",
                "Effect": "Allow",
                "Principal": {
                    "AWS": "arn:aws:iam::AWSACCOUNTID:root"
                },
                "Action": "s3:*",
                "Resource": "arn:aws:s3:::BUCKET-NAME/*"
            },
            {
                "Sid": "All on bucket by lambda",
                "Effect": "Allow",
                "Principal": {
                    "AWS": "arn:aws:iam::AWSACCOUNTID:root"
                },
                "Action": "s3:*",
                "Resource": "arn:aws:s3:::BUCKET-NAME"
            }
        ]
    }
    

    Worked for me and does not require for you to share with all authenticated AWS users (which most of the time is not ideal).

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

    Your Lambda does not have privileges (S3:GetObject).

    Go to IAM dashboard, check the role associated with your Lambda execution. If you use AWS wizard, it automatically creates a role called oneClick_lambda_s3_exec_role. Click on Show Policy. It should show something similar to the attached image. Make sure S3:GetObject is listed.

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

    I was getting the same error "AccessDenied: Access Denied" while cropping s3 images using lambda function. I updated the s3 bucket policy and IAM role inline policy as per the document link given below.

    But still, I was getting the same error. Then I realised, I was trying to give "public-read" access in a private bucket. After removed ACL: 'public-read' from S3.putObject problem get resolved.

    https://aws.amazon.com/premiumsupport/knowledge-center/access-denied-lambda-s3-bucket/

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

    I solved my problem following all the instruction from the AWS - How do I allow my Lambda execution role to access my Amazon S3 bucket?:

    1. Create an AWS Identity and Access Management (IAM) role for the Lambda function that grants access to the S3 bucket.

    2. Modify the IAM role's trust policy.

    3. Set the IAM role as the Lambda function's execution role.

    4. Verify that the bucket policy grants access to the Lambda function's execution role.

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

    Interestingly enough, AWS returns 403 (access denied) when the file does not exist. Be sure the target file is in the S3 bucket.

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