Home > other >  Is it possible to list bucket using AWS Lambda based on tags?
Is it possible to list bucket using AWS Lambda based on tags?

Time:07-16

Have a requirement like need to delete buckets that are 15 days old and having a tag like Type: Test.

Is this achievable using an AWS Lambda function?

CodePudding user response:

Yes, you could use AWS lambda to trigger object deletion. Your lambda would need to be triggered on a regular schedule by an Amazon CloudWatch Events rule. Below you can find info about tagging S3 Objects and performing actions using the S3 API that in your case would be called by the lambda function.

Unless you have a specific reason to use a lambda function, the task you need to be accomplished is the perfect use case for an S3 lifecycle rule. This would be the cleanest way to achieve your goal as you could just setup a rule that removes content according to age and tag.

In case you’re set on Lambda, Additional resources to write it can be found below:

  • It’s possible to tag S3 objects as you requested. This can be achieved in multiple ways depending on what you’re trying to achieve as explained in this article.

Step 1 – Automating object tags to future objects - Object tagging works with many Amazon S3 API operations. For example, you can specify tags when you create objects, and the tagging action itself is free of charge when added as a part of the PutObject request. You specify tags using the x-amz-tagging request header. Alternatively, you could add an AWS Lambda trigger that adds the tags to the object when uploaded. Adding tags via Lambda would incur additional Lambda and S3 request fees.

Step 2: Applying object tags to existing objects - You can add object tags straight from the console on individual objects or use S3 Batch Operations to add or replace object tags to millions of objects. For example, using S3 Inventory reports for multiple prefixes, you can generate prefix-level manifests and then use S3 Batch Operations to add appropriate tags to each prefix. In the preceding example, the S3 Inventory report manifest for prefix 1 can be used as an input for S3 Batch Operations job to add the tag “SIA45,” which can then be used in the lifecycle configuration to transition to S3 Standard-IA storage class after 45 days since the object was created.

  • Also the retention period can be checked through the S3 REST APIS

CodePudding user response:

AWS Lambda simply runs the code that you provide. So, you could write code that:

  • Requests a list of objects in a bucket
  • Loops through the list and:
    • Checks that the object is older than 15 days and has a particular tag

However, it would be easier (and cheaper) to use an S3 Lifecycle rule instead of an AWS Lambda function.

  • Related