I'm trying to create an AWS Lambda function, which processes a file uploaded to the first bucket, then saves it to the second bucket and then deletes the input file.
The problem is that when I'm trying to delete the file I'm getting
{
"message": "Access Denied",
"code": "AccessDenied",
"time": "2015-02-09T22:08:45.926Z",
"statusCode": 403,
"retryable": false,
"retryDelay": 30
}
The code snippet, which tries to delete the file is
s3.deleteObject({
Bucket: inputBucket,
Key: inputKey
}, function(a, b) {
if (a) {
console.error("Error on delete");
console.error(a);
} else {
console.log("Deleted successfully");
}
});
Short description. You can't delete an S3 bucket using the Amazon S3 console if the bucket contains 100,000 or more objects. You can't delete an S3 bucket using the AWS CLI if versioning is enabled. For more information, see Deleting a bucket.
To delete an S3 bucketSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, select the option next to the name of the bucket that you want to delete, and then choose Delete at the top of the page.
Navigate to the Amazon S3 bucket or folder that contains the objects that you want to delete. Select the check box to the left of the names of the objects that you want to delete. Choose Actions and choose Delete from the list of options that appears. Alternatively, choose Delete from the options in the upper right.
The possible reason why lambda wasn't able to delete the file ( S3 object ) could be due to the Lambda's Execution Role.
Steps to solve this
{
"Statement": [
{
"Sid": "Stmt1423535846414",
"Action": [
"s3:DeleteObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::*"
}
]
}
Go to IAM -> Roles -> <assigned-role-name> -> Permissions -> <policy-name>
Make sure your policy has the following:
{
"Statement": [
{
"Action": [
"s3:*"
],
"Resource": [
"arn:aws:s3:::<my-bucket>",
"arn:aws:s3:::<my-bucket>/*"
],
"Effect": "Allow"
}
]
}
Note: arn:aws:s3:::<my-bucket>
is for accessing my-bucket
whereas
arn:aws:s3:::<my-bucket>/*
is for accessing all objects under my-bucket
. They are similar but not the same. They need to be both present to ensure lambda has full S3 access
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With