Ravi Shankar
Ravi Shankar

Reputation: 11

AWS - Delete objects in S3 bucket older than 90 days using Ansible

I am trying to delete some files which are present in an S3 bucket using Ansible. I have the below code and i need to delete objects based on a condition, e.g. objects being older than 3 months.

    - name: delete object from bucket which is older than 3 months
      aws_s3:
        encrypt: yes
        aws_access_key: "{{ access_key }}"
        aws_secret_key: "{{ secret_key }}"
        bucket: "{{ bucket }}" 
        prefix: "{{ database_name }}/"
        marker: "{{ database_name }}/"
        mode: delobj
        s3_url: "{{ s3_url}}"
        validate_certs: no

Upvotes: 0

Views: 748

Answers (1)

Kevin C
Kevin C

Reputation: 5720

I believe the most clean solution would be to let AWS do this for you. As you might know, you can configure S3 lifecycle rules. You can let Ansible configure a bucket with the s3_lifecycle module as such:

- name: Configure a lifecycle rule on a bucket to expire (delete) items with a prefix of /logs/ after 3 months
  community.aws.s3_lifecycle:
    name: mybucket
    expiration_days: 90
    prefix: logs/
    status: enabled
    state: present

Upvotes: 0

Related Questions