Skip to content

Instantly share code, notes, and snippets.

@muhqu
Created May 20, 2014 09:03
Show Gist options
  • Save muhqu/1c1170d6a051bdf9f824 to your computer and use it in GitHub Desktop.
Save muhqu/1c1170d6a051bdf9f824 to your computer and use it in GitHub Desktop.
ShellScript to output EBS Snapshots that are no longer referenced by AMIs
#!/bin/bash
aws ec2 describe-snapshots --owner-ids $AWS_ACCOUNT_ID > snapshots.json
aws ec2 describe-images --owners $AWS_ACCOUNT_ID > images.json
cat snapshots.json \
| jq '.Snapshots[] | select(.Description//""|startswith("Created by CreateImage"))' \
> snapshot-to-ami.json
cat images.json \
| jq '.Images[]|select(.RootDeviceType == "ebs")| {ImageId, SnapshotId:.BlockDeviceMappings[0].Ebs.SnapshotId}' \
> ami-to-snapshot.json
cat snapshot-to-ami.json ami-to-snapshot.json \
| jq -s 'group_by(.SnapshotId) | .[] | {SnapshotId:.[0].SnapshotId, ImageId: (.[].ImageId//null)} ' \
> snapshot-ami-merge.json
cat snapshot-ami-merge.json \
| jq -r 'select(.ImageId == null) | .SnapshotId'
@daniilyar-confyrm
Copy link

Thanks, man! It's working well, does not touch any snapshots, used in existing AMI(s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment