Skip to content

Instantly share code, notes, and snippets.

@dex4er
Created June 15, 2025 08:50
Show Gist options
  • Save dex4er/6881a02473004c2c0331b0603b171b97 to your computer and use it in GitHub Desktop.
Save dex4er/6881a02473004c2c0331b0603b171b97 to your computer and use it in GitHub Desktop.
aws utils
#!/bin/bash
# Check if the required argument (prefix) is passed
if [ -z "$1" ]; then
echo "Usage: $0 <prefix>"
exit 1
fi
PREFIX=$1
# Get all AMI IDs where the name starts with the given prefix
AMI_IDS=$(aws ec2 describe-images --owners self --query "Images[?starts_with(Name, \`${PREFIX}\`)].ImageId" --output text)
# Loop through each AMI ID
for AMI_ID in $AMI_IDS; do
echo "Deregistering AMI: $AMI_ID"
# Deregister the AMI
aws ec2 deregister-image --image-id $AMI_ID
# Get associated Snapshot IDs
SNAPSHOT_IDS=$(aws ec2 describe-snapshots --filters Name=description,Values="*$AMI_ID*" --query "Snapshots[].SnapshotId" --output text)
# Loop through each Snapshot ID
for SNAPSHOT_ID in $SNAPSHOT_IDS; do
echo "Deleting Snapshot: $SNAPSHOT_ID"
# Delete the Snapshot
aws ec2 delete-snapshot --snapshot-id $SNAPSHOT_ID
done
done
# Additionally, delete snapshots where the name starts with the prefix
echo "Deleting all snapshots with name starting with: $PREFIX"
# Get all Snapshot IDs where the name starts with the given prefix
SNAPSHOT_IDS=$(aws ec2 describe-snapshots --owner-ids self --filters Name=tag:Name,Values="${PREFIX}*" --query "Snapshots[].SnapshotId" --output text)
# Loop through each Snapshot ID
for SNAPSHOT_ID in $SNAPSHOT_IDS; do
echo "Deleting Snapshot: $SNAPSHOT_ID"
# Delete the Snapshot
aws ec2 delete-snapshot --snapshot-id $SNAPSHOT_ID
done
echo "Completed deletion of AMIs and associated snapshots with prefix: $PREFIX"
#!/bin/bash
if [[ -z $2 ]]; then
echo Usage: $0 oldid newid source-profile destination-profile
exit 2
fi
if [[ -n $3 ]]; then
source_arg="--profile $3"
fi
if [[ -n $4 ]]; then
target_arg="--profile $4"
elif [[ -n $3 ]]; then
target_arg="--profile $3"
fi
aws secretsmanager create-secret $target_arg --name $2 --secret-string "$(aws secretsmanager get-secret-value $source_arg --secret-id $1 --query SecretString --output text)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment