Is it possible to change a snapshot's identifier when copying it by using the AWS RDS API? I am using Boto3 to write a Lambda function to be used in a DR scenario.
The reason why I'm trying to change the identifier is because I want to add the current date to the snapshot identifier for two reasons:
- It's easier to identify when the snapshot is from
- Different identifiers are needed, so that I can create multiple snapshots. A workaround solution that comes from the top of my mind is to delete the latest snapshot before copying the newest version of it, but I'd like to know if it is possible to change snapshot identifiers like you are able to do it through the management console.
My code:
client = boto3.client("rds") #dr region
db_identifier = "test-database-dr"
# Get snapshot automated arn so we can create a manual copy
describe_snapshots = client.describe_db_snapshots(
SnapshotType= "shared",
IncludeShared=True,
)
db_arn = (describe_snapshots["DBSnapshots"][-1]["DBSnapshotArn"])
# Create a snapshot copy
copy_snapshot = client.copy_db_snapshot(
SourceDBSnapshotIdentifier=db_arn,
TargetDBSnapshotIdentifier=db_identifier,
KmsKeyId="xxx",
SourceRegion="xxx"
)
The error that I get is:
botocore.errorfactory.DBSnapshotAlreadyExistsFault: An error occurred (DBSnapshotAlreadyExists) when calling the CopyDBSnapshot operation: Cannot create the snapshot because a snapshot with the identifier test-database-dr already exists.
CodePudding user response:
You need to set the TargetDBSnapshotIdentifier
to something different.
client = boto3.client("rds") #dr region
db_identifier = "test-database-dr-SOMETHING-DIFFERENT"
# Get snapshot automated arn so we can create a manual copy
describe_snapshots = client.describe_db_snapshots(
SnapshotType= "shared",
IncludeShared=True,
)
db_arn = (describe_snapshots["DBSnapshots"][-1]["DBSnapshotArn"])
# Create a snapshot copy
copy_snapshot = client.copy_db_snapshot(
SourceDBSnapshotIdentifier=db_arn,
TargetDBSnapshotIdentifier=db_identifier,
KmsKeyId="xxx",
SourceRegion="xxx"
)