27

I can call aws rds describe-db-snapshots --db-instance-identifier {my_db_instance} and sort all automated snapshots to find the most recently created one but I was hoping someone has a better idea out there.

1
  • ..just..a thought, whenever I have to restore an RDS postgres from a snapshot, I just go to the aws dashboard and I find it quickly by date and time (if you're looking for an automated way for restoring a snapshot then disregard my comment).... Commented Oct 7, 2018 at 10:04

8 Answers 8

56

For me, this one works:

aws rds describe-db-snapshots \
  --query="max_by(DBSnapshots, &SnapshotCreateTime)"

The query parameter returns only the most recent one.

If only the Arn is needed, this one might help:

aws rds describe-db-snapshots \
  --query="max_by(DBSnapshots, &SnapshotCreateTime).DBSnapshotArn" \
  --output text

And all that for a specific database instance:

aws rds describe-db-snapshots \
  --db-instance-identifier={instance identifier} \
  --query="max_by(DBSnapshots, &SnapshotCreateTime).DBSnapshotArn" \
  --output text
3
  • 3
    Works like a charm. Learn something new about AWS query every day. I believe this should be the accepted answer. Thanks!
    – Brent
    Commented Jun 29, 2018 at 15:56
  • DBSnapshots seems to be no longer valid, you should use DBClusterSnapshotIdentifier
    – FL3SH
    Commented Dec 29, 2021 at 11:51
  • @hey, thank you for the query and description, could you please suggest where can I learn the other functions/types of such query? PS. I have looked in the AWS CLI rds documentation but not found somthing usefull.
    – Vab
    Commented Jan 26, 2023 at 10:52
12

I know this is old, but I was needing to know the same information and was able to construct the following which will then just give me the snapshot name. It doesn't totally answer your question about emphatically finding the latest snapshot but in this example might give you some better direction.

aws rds describe-db-snapshots --db-instance-identifier prd --snapshot-type automated --query "DBSnapshots[?SnapshotCreateTime>='2017-06-05'].DBSnapshotIdentifier"

To break it down with the options

--db-instance-identifier (put in your instance name your are looking for)

--snapshot-type (I put in automated to find the automated backups)

--query "DBSnapshots[?SnapshotCreateTime>='2017-06-05'].DBSnapshotIdentifier"

(This is what I used to refine my search as we do daily backups, I just look for the snapshot create time to be greater than today and by giving the .DBSnapshotIdentifier gives me back just the name.

Hopefully this will help somebody else out.

1
  • Very helpful. Thanks! Commented Nov 18, 2021 at 17:12
4

My way:

> aws rds describe-db-snapshots --db-instance-identifier ${yourDbIdentifier} --query="reverse(sort_by(DBSnapshots, &SnapshotCreateTime))[0]|DBSnapshotIdentifier"
> "rds:dbName-2018-06-20-00-07"
3

If someone is looking for cluster command:

aws rds describe-db-cluster-snapshots --db-cluster-identifier prod --snapshot-type automated --query "DBClusterSnapshots[?SnapshotCreateTime>='2017-06-05'].DBClusterSnapshotIdentifier"
1

As at 31th October 2014, it looks like you can use the --t flag to list only automated backups.

http://docs.aws.amazon.com/AmazonRDS/latest/CommandLineReference/CLIReference-cmd-DescribeDBSnapshots.html

From there, you should be able to parse the output to determine your latest snapshots.

rds-describe-db-snapshots --t automated

DBSNAPSHOT  rds:<NAME>-2016-08-09-17-12  

There is no any other more simple way around for this.

0

I am getting this error while restoring the db from snapshot with the id that I get with the command from the above methods:

An error occurred (InvalidParameterValue) when calling the RestoreDBInstanceFromDBSnapshot operation: Invalid snapshot identifier:  "rds:dev-mysql-rds1-2018-10-06-01-09"

So, I have modified the above query to make it work for me, here is a query that worked for me to get the latest snapshot that worked with restore-db-instance-from-db-snapshot

aws rds describe-db-snapshots --query "DBSnapshots[?DBInstanceIdentifier=='MASTER_INSTANCE_IDENTIFIER']" | jq -r 'max_by(.SnapshotCreateTime).DBSnapshotIdentifier'
0
aws rds describe-db-cluster-snapshots --snapshot-type=automated --query="max_by(DBClusterSnapshots,&SnapshotCreateTime)"

This works in 2022.08

0

If it is RDS cluster then you can use below command:

aws rds describe-db-cluster-snapshots --db-cluster-identifier <DBClusterIdentifier> --region <region> --query="max_by(DBClusterSnapshots, &SnapshotCreateTime)"

you can use below command to fetch specific snapshot ARN:

aws rds describe-db-cluster-snapshots --db-cluster-identifier <DBClusterIdentifier> --region <region> --query="max_by(DBClusterSnapshots, &SnapshotCreateTime).DBClusterSnapshotArn" 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.