Home » Posts tagged 'instance-ids'

Tag Archives: instance-ids

aws cli – snapshots listing and correlations

Today i was working on one curious issue, and I created few AWS CLI Snippets. I think the best use case of below snippets (in sequence) will be when we deregister an AMI, as this will result in detachment of snapshots attached to it and the snapshots can be deleted, but there can be cases when we want to preserve the snapshots, usually when we have huge amount of snapshots then listing and searching for snapshot-ids in AWS console may gets timed out, in those cases you can use below snippets to get the desired results. 

1. run below to get the list of volume-ids attached to an instance, in this we wanted to know VolumeId, InstanceId, DeviceName, and any relevant tag like Name tag, etc.

aws ec2 describe-instances --filters "Name=tag:Name,Values=<hostname>" --query "Reservations[].Instances[].{deviceID:BlockDeviceMappings[].Ebs.VolumeId,deviceName:BlockDeviceMappings[].DeviceName,InstanceId:InstanceId,Name:Tags[?Key=='Name']|[0].Value}"

2. copy one Volume-Id from above step, and add in below section, in this step we will be finding the date and time of the snapshots, we will be copying the date and time of the restore snapshot-ids to be restored, note if you are restoring an instance then it is advisable to restore all attached volumes, but just to bring the instance up you can restore root volume (/dev/sda1) also.

aws ec2 describe-snapshots --filters "Name=volume-id,Values=<volume-id-from-step-1>" --query "Snapshots[*].{DateTime:StartTime,Volume:VolumeId,SnapshotId:SnapshotId,Description:Description}" --output table

3. now we will use the date and time from above and will run to get the exact snapshot ids on that time for that volume-id.

aws ec2 describe-snapshots --filters "Name=volume-id,Values=<volume-id-from-step-1>" "Name=start-time,Values=2021-08-11T10:19*<this-is-date-from-step-2>" --query "Snapshots[].{DateTime:StartTime,Volume:VolumeId,SnapshotId:SnapshotId,Description:Description}" --output table


4. now run the step-3 for all volume-ids we got from step-1, from step-1 you will also see the association that is what volume-ids were attached as what block device like /dev/sda1, etc.