Is there any way to list orphan RDS Snapshots - amazon-web-services

I am planning for reducing overall cost of AWS RDS, I am looking for an AWS CLI command for listing RDS snapshots not associated with any RDS instance (aka orphan snapshot). Can you please help me on this? So far I got these commands:
This CLI gives me list of all RDS Snapshots:
aws rds describe-db-snapshots --query 'DBSnapshots[*].DBSnapshotIdentifier' --output table

Related

Stop multiple RDS instances using AWS CLI command

I have been trying to stop multiple instances of RDS using a single command line but it does not seem to work.
Currently I can only make it work with one instance at a time with a command like this:
aws rds stop-db-instance --db-instance-identifier test-instance1 --region ap-southeast-1 --profile dev
However I would like to stop multiple RDS and this does not seem to work:
aws rds stop-db-instance --db-instance-identifier test-instance1 test-instance2 testinstance3 --region ap-southeast-1 --profile dev
Any idea or suggestion on how I can make this work?
If it is not possible I will probably create a CRON job instead using Lambda.
Sadly you can't do this. But you can write a simple bash for loop:
ids=(test-instance1 test-instance2 test-instance3)
for id in ${ids[#]};
do
echo "Stopping: ${id}"
aws rds stop-db-instance --db-instance-identifier ${id} --region ap-southeast-1 --profile dev
done
If writing a shell script that calls aws rds stop-db-instance multiple times, once per RDS instance, is problematic for you somehow, then consider doing this via a scheduled Lambda (think of it like crontab).
See Schedule Amazon RDS stop and start using AWS Lambda, which:
presents a solution using AWS Lambda and Amazon EventBridge that allows you to schedule a Lambda function to stop and start the idle databases with specific tags to save on compute costs.

Why does AWS CLI rds describe-db-snapshots not include Aurora snapshots?

I can see 77 "System" snapshots in us-east-1 on the website / AWS Console. When I run the following:
aws rds describe-db-snapshots --region us-east-1 --include-shared --include-public --no-paginate --output text
... I get 35. I tried this in AWS CloudShell as well as locally with the access/secret from https://console.aws.amazon.com/iam/home?region=us-east-1#/security_credentials so this should be running with maximum (my) privileges.
I think it's excluding Aurora snapshots because the only engine value I see is postgres and not aurora-postgresql. I am going crazy trying to figure out why I can't see everything with the CLI ... any thoughts, pointers, RTFM's?
UPDATE: I added --filters "Name=engine,Values=aurora-postgresql" and sure enough the output is blank whereas --filters "Name=engine,Values=postgres" shows the 30+ entries for non-Aurora. So why are Aurora snapshots being excluded?
(Thanks to #JohnRotenstein for the answer in a comment to my question.)
There is a separate command called describe-db-cluster-snapshots that operates very similarly and outputs results for clusters, obviously, like Aurora. The only way to get the full list as seen in the Console is to combine this output with describe-db-snapshots.

List Snapshot ID's with time under specific VPC ID using PowerShell

I'm trying to List Snapshot ID's with time under specific VPC ID using PowerShell or AWS CLI
Snapshots are region specific, not VPC specific, therefore you can't directly list all snapshots for a specific VPC.
If you need that kind of filtering, then you can tag each snapshot with a VPC it "belongs" to (i.e VPCID:value-of-VPC-id). And then use this command.
aws ec2 describe-snapshots --owner-ids <your-account-id> --filters Name=tag:VPCID,Values=<value-of-VPC-id>
If you want to retrieve only VPC ID and time, then you can enhance the above with query parameter
aws ec2 describe-snapshots --owner-ids <your-account-id> --filters Name=tag:VPCID,Values=<value-of-VPC-id> --query "Snapshots[*].{ID:SnapshotId,Time:StartTime}"

How to find latest or most recent AWS RDS snapshot?

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.
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
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.
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"
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"
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.
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'
aws rds describe-db-cluster-snapshots --snapshot-type=automated --query="max_by(DBClusterSnapshots,&SnapshotCreateTime)"
This works in 2022.08
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"

Finding EC2 status using EC2 API

Is there any way to find out status of AWS EC2 instances, which are running on various different regions, from one EC2 instance which is present in any one of region by using EC2 API tool ?
How this is possible ?
I got the answer :-
ec2-describe-instances instance-ID --region region
Example :-
ec2-describe-instances i-f82d5ca0 --region eu-west-1
Where instance ID is EC2 instance ID which is located in region eu-west-1
Thats all .
Or in the new unified AWS CLI, this is slightly different:
aws ec2 describe-instances --instance-id i-f82d5ca0
You can also change the --output into json, text, or a table