简体   繁体   English

AWS Lambda:RDS快照-KeyError

[英]AWS Lambda: RDS Snapshot - KeyError

I am setting up a Lambda function to take daily snapshots of RDS instances based on this script . 我正在设置一个Lambda函数,以基于此脚本拍摄RDS实例的每日快照。 I am running with the python3 interpreter. 我正在使用python3解释器。

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']
                )

The script does work for creating a snapshot; 该脚本确实可以创建快照; however I also get this error every time it runs, so I do not think it will properly delete snapshots. 但是,每次运行时也会出现此错误,因此我认为它不会正确删除快照。

'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'

The problem seems to be with this line in particular: 问题似乎尤其是与以下行有关:

createTs = snapshot['SnapshotCreateTime'].replace(tzinfo=None)

Why is this happening? 为什么会这样呢?

I suspect that you are seeing KeyError because the snapshot is still in progress and SnapshotCreateTime is not populated in the returned dict yet. 我怀疑您正在看到KeyError,因为快照仍在进行中,并且尚未在返回的dict中填充SnapshotCreateTime。

In this case the PercentProgress will be less than 100. 在这种情况下,PercentProgress将小于100。

for snap in snapshots['DBSnapshots']:
    if ('SnapshotCreateTime' in snap):
        print snap['SnapshotCreateTime']
    else:
        print 'No create time available'

    if ('PercentProgress' in snap):
        print snap['PercentProgress']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM