I have a lambda function deployed as a container image. My lambda handler in the docker image is accessing my s3 bucket for downloading one or more files. When I invoke my lambda function locally using the boto3 lambda client, sometimes it works fine but sometimes it returns the error {'errorMessage' : 'An error occurred (Access Denied) when calling the ListObjects operation: Access Denied', 'errorType':'ClientError'}
. The line of code it shows the error on is
s3 = boto3.resource('s3')
bucket = s3.Bucket(BUCKET_NAME)
for obj in bucket.objects.filter(Prefix=file_path) ---> this line
My bucket policy looks like this:
{
"Version": "2012-10-17",
"Id": "Policy1657874069007",
"Statement": [
{
"Sid": "Stmt1657874045596",
"Effect": "Allow",
"Principal": {
"AWS": [
"arn:aws:iam::<account_id>:role/service-role/<lambda_role_arn>"
]
},
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::<bucket_name>",
"arn:aws:s3:::<bucket_name>/*"
]
}
]
}
I have also given the s3FullAccess role to my lambda function. I just don't understand what's happening, what's annoying is that how sometimes it works fine but sometimes it just starts giving this error. Can someone point out what I am doing wrong? I am new to AWS. Also, if I run the lambda container image locally , this error doesn't occur at all.