Boto3 and AWS Lambda - deleting snapshots older than
<p>I'm currently utilising AWS Lambda to create snapshots of my database and delete snapshots older than 6 days.  I'm using the Boto3 library to interface with the AWS API. I'm using a CloudWatch rule to trigger the deletion code every day. </p>

<p>Normally this is working fine, but I've come across an issue where at the start of the month (first 6 days) the delete script does not appear to delete any snapshots, even though snapshots older than 6 days exist. </p>

<p>The code is below: </p>

<pre><code>import json
import boto3
from datetime import datetime, timedelta, tzinfo

class Zone(tzinfo):
    def __init__(self,offset,isdst,name):
        self.offset = offset
        self.isdst = isdst
        self.name = name
    def utcoffset(self, dt):
        return timedelta(hours=self.offset) + self.dst(dt)
    def dst(self, dt):
        return timedelta(hours=1) if self.isdst else timedelta(0)
    def tzname(self,dt):
        return self.name

UTC = Zone(10,False,'UTC')

# Setting retention period of 6 days
retentionDate = datetime.now(UTC) - timedelta(days=6)

def lambda_handler(event, context):
    print("Connecting to RDS")
    rds = boto3.setup_default_session(region_name='ap-southeast-2')
    client = boto3.client('rds')
    snapshots = client.describe_db_snapshots(SnapshotType='manual')
    print('Deleting all DB Snapshots older than %s' % retentionDate)

    for i in snapshots['DBSnapshots']:
        if i['SnapshotCreateTime'] &lt; retentionDate:
            print ('Deleting snapshot %s' % i['DBSnapshotIdentifier'])
              client.delete_db_snapshot(DBSnapshotIdentifier=i['DBSnapshotIdentifier']
        )
</code></pre>

<p>Code looks perfectly fine and <a href="http://boto3.readthedocs.io/en/latest/reference/services/rds.html#RDS.Client.describe_db_snapshots" rel="nofollow">you are following the documentation</a></p>

<p>I would simply add</p>

<pre><code>    print(i['SnapshotCreateTime'], retentionDate)
</code></pre>

<p>in the for loop, the logs will tell you quickly what's going on in the beginning of every month.</p>

<p>Btw, are you using RDS from AWS? RDS supports automatic snapshot creation and you can also define a retention period. There is no need to create custom lambda scripts.</p>

python aws-lambda boto3