Copy all objects from S3 bucket before it is emptied or deleted

Viewed 121

Is there a way to trigger a lambda before a bucket is actually deleted (for example, with a stack that it is a part of) or emptied to copy its objects? Maybe something else could be used instead of lambdas?

1 Answers

Deletion of a CloudFormation (CNF) stack with non-empty bucket will fail as non-empty buckets can't be deleted, unless you set its DeletionPolicy to retain. The retain would delete the stack, but leave out the bucket in your account. Without retain, you have to first delete all objects in a bucket before bucket can be deleted.

In either way, you have to delete the objects yourself through a custom lambda function. There is no out-of-the box mechanism in CFN nor S3 to delete objects when bucket is deleted. But since this is something that you have to develop yourself, you can do whatever you want with these objects before you actually delete them, e.g. copy to glacier.

There are few ways in which this can be achieve. But probably the most common way is through a custom resource, similar to the one given in AWS blog:

The resource given in this blog actually responds to Delete event in CFN and deletes the objects in the bucket:

b_operator.Bucket(str(the_bucket)).objects.all().delete()

So you would have to modify this custom resource to copy objects before the deletion operation is performed.

Related