Skip to content

Instantly share code, notes, and snippets.

@mzupan
Last active June 8, 2021 05:19
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save mzupan/41d01bfb3b4c292fdac0 to your computer and use it in GitHub Desktop.
Save mzupan/41d01bfb3b4c292fdac0 to your computer and use it in GitHub Desktop.
AWS Lambda job to backup RDS instances
import boto3
import datetime
def lambda_handler(event, context):
print("Connecting to RDS")
client = boto3.client('rds')
print("RDS snapshot backups stated at %s...\n" % datetime.datetime.now())
client.create_db_snapshot(
DBInstanceIdentifier='web-platform-slave',
DBSnapshotIdentifier='web-platform-%s' % datetime.datetime.now().strftime("%y-%m-%d-%H"),
Tags=[
{
'Key': 'CostCenter',
'Value': 'web'
},
]
)
for snapshot in client.describe_db_snapshots(DBInstanceIdentifier='web-platform-slave', MaxRecords=50)['DBSnapshots']:
if create_ts < datetime.datetime.now() - datetime.timedelta(days=7):
print "Deleting snapshot id:", snapshot['DBSnapshotIdentifier']
client.delete_db_snapshot(
DBSnapshotIdentifier=snapshot['DBSnapshotIdentifier']
)
@mkubenka
Copy link

Example policy:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "arn:aws:logs:*:*:*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "rds:AddTagsToResource",
                "rds:DeleteDBSnapshot"
            ],
            "Resource": "arn:aws:rds:eu-central-1::snapshot:web-platform-*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "rds:ListTagsForResource",
                "rds:CreateDBSnapshot"
            ],
            "Resource": "arn:aws:rds:eu-central-1:*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "rds:DescribeDBSnapshots"
            ],
            "Resource": "*"
        }
    ]
}

@bcoelho-kcom
Copy link

I'm getting the following error:


{
"stackTrace": [
[
"/var/task/lambda_function.py",
23,
"lambda_handler",
"if create_ts < datetime.datetime.now() - datetime.timedelta(days=30):"
]
],
"errorType": "NameError",
"errorMessage": "global name 'create_ts' is not defined"

}

Any solution?

@tomcook
Copy link

tomcook commented Jun 30, 2016

use snapshot['SnapshotCreateTime'] instead of create_ts

@zsustar
Copy link

zsustar commented Oct 12, 2016

this won't work.

you will get following error:
"TypeError: can't compare offset-naive and offset-aware datetimes"

Add following line
create_ts = snapshot['SnapshotCreateTime'].replace(tzinfo=None)

so it will be look like

for snapshot in client.describe_db_snapshots(DBInstanceIdentifier='web-platform-slave', MaxRecords=50)['DBSnapshots']:
    create_ts = snapshot['SnapshotCreateTime'].replace(tzinfo=None)
    if create_ts < datetime.datetime.now() - datetime.timedelta(days=7):
        print "Deleting snapshot id:", snapshot['DBSnapshotIdentifier']
        client.delete_db_snapshot(
            DBSnapshotIdentifier=snapshot['DBSnapshotIdentifier']
        )

@prashantohol
Copy link

Hi, Used below code but it didn't work.

import boto3
import datetime

def lambda_handler(event, context):
print("Connecting to RDS")
client = boto3.client('rds')

print("RDS snapshot backups stated at %s...\n" % datetime.datetime.now())
client.create_db_snapshot(
    DBInstanceIdentifier='service-rds', 
    DBSnapshotIdentifier='rds-test-%s' % datetime.datetime.now().strftime("%y-%m-%d-%H"),
    Tags=[
        {
            'Key': 'Service',
            'Value': 'db'
        },
    ]
)

for snapshot in client.describe_db_snapshots(DBInstanceIdentifier='service-rds', MaxRecords=50)['DBSnapshots']:
create_ts = snapshot['SnapshotCreateTime'].replace(tzinfo=None)
if create_ts < datetime.datetime.now() - datetime.timedelta(days=7):
print "Deleting snapshot id:", snapshot['DBSnapshotIdentifier']
client.delete_db_snapshot(
DBSnapshotIdentifier=snapshot['DBSnapshotIdentifier']
)


Got below error

{
"errorMessage": "module initialization error"
}

@prashantohol
Copy link

prashantohol commented Jun 13, 2017

Hi Mike,

Good morning and hope you are doing good.

Got below error. Could you please help?

{
"errorMessage": "module initialization error"
}

import boto3
import datetime

def lambda_handler(event, context):
print("Connecting to RDS")
client = boto3.client('rds')

print("RDS snapshot backups stated at %s...\n" % datetime.datetime.now())
client.create_db_snapshot(
DBInstanceIdentifier='service-rds',
DBSnapshotIdentifier='rds-test-%s' % datetime.datetime.now().strftime("%y-%m-%d-%H"),
Tags=[
{
'Key': 'Service',
'Value': 'db'
},
]
)
for snapshot in client.describe_db_snapshots(DBInstanceIdentifier='service-rds', MaxRecords=50)['DBSnapshots']:
create_ts = snapshot['SnapshotCreateTime'].replace(tzinfo=None)
if create_ts < datetime.datetime.now() - datetime.timedelta(days=7):
print "Deleting snapshot id:", snapshot['DBSnapshotIdentifier']
client.delete_db_snapshot(
DBSnapshotIdentifier=snapshot['DBSnapshotIdentifier']
)

Thx
Prashant

@repudi8or
Copy link

repudi8or commented Jun 22, 2017

There is no snapshot['SnapshotCreateTime'] for snapshots in progress, so if you iterate over an in-progress snapshot this code will break with "errorType": "KeyError", "errorMessage": "'SnapshotCreateTime'"

I suggest skipping any without a CreateTime : -

    for snapshot in client.describe_db_snapshots(DBInstanceIdentifier='service-rds', MaxRecords=50)['DBSnapshots']:
        if 'SnapshotCreateTime' in snapshot:
            create_ts = snapshot['SnapshotCreateTime'].replace(tzinfo=None)
            if create_ts < datetime.datetime.now() - datetime.timedelta(days=7):
                print "Deleting snapshot id:", snapshot['DBSnapshotIdentifier']
                client.delete_db_snapshot(
                    DBSnapshotIdentifier=snapshot['DBSnapshotIdentifier'])

@songautam
Copy link

I am using the same code but getting below error -

{ "errorMessage": "An error occurred (InvalidParameterValue) when calling the CreateDBSnapshot operation: The specified instance is a member of a cluster and a snapshot cannot be created directly. Please use the CreateDBClusterSnapshot API instead.", "errorType": "ClientError", "stackTrace": [ [ "/var/task/lambda_function.py", 19, "lambda_handler", "'Value': 'NIRDS'" ], [ "/var/runtime/botocore/client.py", 312, "_api_call", "return self._make_api_call(operation_name, kwargs)" ], [ "/var/runtime/botocore/client.py", 605, "_make_api_call", "raise error_class(parsed_response, operation_name)" ] ] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment