【问题标题】:AWS RDS snapshot by python Lamdapython Lambda的AWS RDS快照
【发布时间】:2017-11-27 23:43:10
【问题描述】:

我正在尝试通过 AWS Lamda (python 3.6) 的功能创建/删除我的 AWS RDS 的快照,但我不知道我在哪里做错了,这个功能还删除了 7 天前的快照,所以任何人都可以建议我这样做

import boto3
import datetime

def lambda_handler(event, context):
    print("Connecting to RDS")
    client = boto3.client('rds')
    dbInstances = ['magento-live']

    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': 'NI',
                    'Value': 'NIRDS'

                },
            ]
        )

        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=7):
                print("Deleting snapshot id:", snapshot['DBSnapshotIdentifier'])
                client.delete_db_snapshot(
                    DBSnapshotIdentifier=snapshot['DBSnapshotIdentifier']
                )

但我总是遇到错误

    {
  "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)"
    ]
  ]
}

【问题讨论】:

  • 根据错误信息看起来你需要使用CreateDBClusterSnapshot
  • 是的,我能理解,所以我应该在打印旁边使用 client.create_db_cluster_snapshot 而不是 client.create_db_snapshot
  • 我有极光

标签: python amazon-web-services snapshot


【解决方案1】:

阿隆是对的。您使用了不同的方法(适用于 aurora 集群而不是数据库实例)。

这是一个对我有用的 lambda 函数:

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_cluster_snapshot(
    DBClusterIdentifier='enter-your-cluster-name-her', 
    DBClusterSnapshotIdentifier='enter-your-cluster-name-here-%s' % datetime.datetime.now().strftime("%y-%m-%d-%H"),
    Tags=[
        {
            'Key': 'ENV',
            'Value': 'dev'
        },
    ]
)

【讨论】:

    猜你喜欢
    • 2017-06-25
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多