Ich richte eine Lambda-Funktion ein, um tägliche Snapshots von RDS-Instanzen basierend auf this script zu erstellen. Ich laufe mit dem Python3-Interpreter.AWS Lambda: RDS-Snapshot - KeyError
import boto3
import datetime
def lambda_handler(event, context):
print("Connecting to RDS")
client = boto3.client('rds')
# Instance to backup
dbInstances = ['testdb']
for dbInstance in dbInstances:
print("RDS snapshot backups started at %s...\n" % datetime.datetime.now())
client.create_db_snapshot(
DBInstanceIdentifier=dbInstance,
DBSnapshotIdentifier=dbInstance+'{}'.format(datetime.datetime.now().strftime("%y-%m-%d-%H")),
Tags=[
{
'Key': 'Name',
'Value': 'dbInstace'
},
]
)
for snapshot in client.describe_db_snapshots(DBInstanceIdentifier=dbInstance, MaxRecords=50)['DBSnapshots']:
createTs = snapshot['SnapshotCreateTime'].replace(tzinfo=None)
if createTs < datetime.datetime.now() - datetime.timedelta(days=30):
print("Deleting snapshot id:", snapshot['DBSnapshotIdentifier'])
client.delete_db_snapshot(
DBSnapshotIdentifier=snapshot['DBSnapshotIdentifier']
)
Das Skript funktioniert zum Erstellen eines Snapshots; Ich erhalte diesen Fehler jedoch jedes Mal, wenn er ausgeführt wird. Daher glaube ich nicht, dass Snapshots ordnungsgemäß gelöscht werden.
'SnapshotCreateTime': KeyError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 29, in lambda_handler
createTs = snapshot['SnapshotCreateTime'].replace(tzinfo=None)
KeyError: 'SnapshotCreateTime'
Traceback (most recent call last):
File "/var/runtime/awslambda/bootstrap.py", line 226, in handle_event_request
result = request_handler(json_input, context)
File "/var/task/lambda_function.py", line 29, in lambda_handler
createTs = snapshot['SnapshotCreateTime'].replace(tzinfo=None)
KeyError: 'SnapshotCreateTime'
Das Problem scheint insbesondere mit dieser Linie zu sein:
createTs = snapshot['SnapshotCreateTime'].replace(tzinfo=None)
Warum ist das passiert?
, dass das Problem war. Wäre die beste Lösung, einen "Schlaf" da drin zu haben? Das scheint jedoch die Kosten zu erhöhen. – flyingcars34
Nach einer kurzen Überprüfung Ihres Codes versuchen Sie, Snapshots älter als 30 Tage zu löschen, damit Snapshots ohne SnapshotCreateTime ignoriert werden können, da sie deutlich weniger als 30 Tage alt sind (sie existieren noch nicht einmal). – jarmod