Home > Software design >  AWS Lambda - Delete an object in a versioned S3 Bucket
AWS Lambda - Delete an object in a versioned S3 Bucket

Time:06-22

I'm working on setting up a Lambda function in JavaScript. I want this function to take some data when a dynamodb record is deleted, and use that to find and remove the S3 object it corresponds to (In a versioned bucket). Here's what I have so far:

import { Context, APIGatewayProxyResult, APIGatewayEvent } from 'aws-lambda';

const AWS = require('aws-sdk');
const s3 = new AWS.S3({
    region: 'eu-west-2'
});

export const handler = async (event: APIGatewayEvent, context: Context): Promise<APIGatewayProxyResult> => {
  event.Records.forEach(async record => {
    if (record.eventName == "REMOVE") {
        _processRecord(record.dynamodb.OldImage).promise();
    }
  });
};

async function _processRecord(oldImage) {
    const parameters = {
        Bucket: process.env.BUCKETNAME,
        Key: oldImage.propertyId.S
    };

    try {
        s3.headObject(parameters).promise();
        console.log('File located in S3');

        try {
            s3.deleteObject(parameters).promise();
            console.log('File deleted from S3');
        }
        catch (error) {
            console.log("ERROR in file Deleting : "   JSON.stringify(error));
        }
    } catch (error) {
        console.log("File not Found ERROR : "   error.code)
    }
}

Everything seems fine until I get to the S3 section. When I invoke the function I get a 202 response which all looks fine, but the files are not being deleted when I check in S3. I've tried adding in a version to the parameters but that doesn't seem to work either. Any help would be greatly appreciated.

CodePudding user response:

When you delete and object from an S3 bucket which has versioning enabled, the object is not permanently deleted. Instead the latest version is specially marked as deleted. Any get object issued to that object will return as if the object has been deleted.

To permanently delete an object in a versioned bucket you must delete all the versions of that object.

You can find more detail on the Deleting object versions docs.

CodePudding user response:

I've done some more digging and was able to figure out where things were going wrong. I think my main issue was actually using foreach in the async function. Replacing that with for (const record of Records) got things running smoothly.

  • Related