Python代码中关于Aurora快照复制的问题及解决方案

44次阅读
没有评论

问题描述

在Python代码中,我使用了以下代码来复制Aurora数据库快照到另一个区域:

current_date = str(dt.now().strftime('%Y-%m-%d'))
target_cluster_snapshot_arn = current_date + '_Development_Aurora'
response = TARGET_CLIENT.copy_db_cluster_snapshot(
    SourceDBClusterSnapshotIdentifier=source_cluster_snapshot_arn,
    TargetDBClusterSnapshotIdentifier=target_cluster_snapshot_arn,
    KmsKeyId='arn:aws:kms:us-west-2:xxxxxxx:key/axxxxxx-e326-4df2-8274-73f87ff02f37',
    CopyTags=True,
    Tags=[
        {
            'Key': 'Deletion_Date',
            'Value': (dt.now() + datetime.timedelta(days=30)).strftime('%Y-%m-%d')
        },
    ],
    SourceRegion=SOURCE_REGION
)

然而,当我运行这段代码时,我遇到了以下错误:

botocore.exceptions.ClientError: An error occurred (InvalidParameterValue) when calling the CopyDBClusterSnapshot operation: Invalid cluster snapshot identifier: 2018-05-22_Development_Aurora

但是,当我在TargetDBClusterSnapshotIdentifier中硬编码值时,就能正常运行:

response = TARGET_CLIENT.copy_db_cluster_snapshot(
    SourceDBClusterSnapshotIdentifier=source_cluster_snapshot_arn,
    TargetDBClusterSnapshotIdentifier='PrashastTest',
    KmsKeyId='arn:aws:kms:us-west-2:xxxxxxx:key/xxxxxxb3-e326-4df2-8274-73f87ff02f37',
    CopyTags=True,
    Tags=[
        {
            'Key': 'Deletion_Date',
            'Value': (dt.now() + datetime.timedelta(days=30)).strftime('%Y-%m-%d')
        },
    ],
    SourceRegion=SOURCE_REGION
)

请问这是为什么呢?

解决方案

出现这个问题是因为你在TargetDBClusterSnapshotIdentifier中使用了一个不合法的快照标识符。AWS要求快照标识符满足一定的规则,而你的标识符2018-05-22_Development_Aurora并不符合这些规则。

根据AWS API参考文档,以下是快照标识符的约束条件:

  • 必须包含1到63个字母、数字或连字符。
  • 第一个字符必须是字母。
  • 不能以连字符结尾,也不能包含两个连续的连字符。

因此,2018-05-22_Development_Aurora不符合第一个约束,因为它以数字开头,并且包含了连字符。而硬编码的PrashastTest则符合这些约束条件。

为了解决这个问题,你需要确保使用合法的快照标识符,按照约束条件命名标识符,以确保代码能够正常工作。

如果你想使用类似2018-05-22_Development_Aurora这样的标识符,你可以考虑使用其他方式来构建合法的标识符,比如去掉连字符并添加其他字符以确保第一个字符是字母。

通过遵循这些约束条件,你就能够成功复制Aurora数据库快照到另一个区域了。

正文完