Warm tip: This article is reproduced from stackoverflow.com, please click
amazon-rds amazon-web-services aws-glue boto3 python

How to schedule an RDS snapshot and restore in the same script

发布于 2020-04-08 09:29:53

so I'm scheduling an AWS python job (through AWS Glue Python shell) that is supposed to clone a MySQL RDS database (best way to take a snapshot and restore?) and perform sql queries on the database. I have the boto3 library on the Python Shell and an SQL Python Library I loaded. I have this code currently

import boto3
client = boto3.client('rds')
# Create a snapshot of the database
snapshot_response = client.create_db_snapshot(
    DBSnapshotIdentifier='snapshot-identifier',
    DBInstanceIdentifier='instance-db',
)

# Restore db from snapshot
restore_response = client.restore_db_instance_from_db_snapshot(
    DBInstanceIdentifier = 'restored-db',
    DBSnapshotIdentifier = 'snapshot-identifier',
)

# Code that will perform sql queries on the restored-db database.

However, the client.restore_db_instance_from_db_snapshot fails because it says the snapshot is being created. So I understand that this means these calls are asynchronous. But I am not sure how to get this snapshot restore to work (either by making them synchronous - not a good idea?) or by some other way. Thanks for the help in advance :).

Questioner
luke henry
Viewed
50
John Rotenstein 2020-02-01 12:41

You can use a waiter:

waiter = client.get_waiter('db_cluster_snapshot_available')

Polls RDS.Client.describe_db_cluster_snapshots() every 30 seconds until a successful state is reached. An error is returned after 60 failed checks.

See: class RDS.Waiter.DBClusterSnapshotAvailable