Skip to content

Instantly share code, notes, and snippets.

@mikedixson
Forked from garlandkr/ami-clean.sh
Last active July 27, 2022 14:50
Show Gist options
  • Save mikedixson/ab4365d7ff43c5fcbdaef9e4a5a186ac to your computer and use it in GitHub Desktop.
Save mikedixson/ab4365d7ff43c5fcbdaef9e4a5a186ac to your computer and use it in GitHub Desktop.
Clean-up an Amazon EC2 instance before creating an AMI
#!/bin/bash
# Run this script with:
# bash <(curl -s https://gist.githubusercontent.com/mikedixson/ab4365d7ff43c5fcbdaef9e4a5a186ac/raw/df8d4e298a0f1d9b34b134797f9993896db36970/ubuntu-ami-clean.sh)
function print_green {
echo -e "\e[32m${1}\e[0m"
}
print_green 'Clean apt'
apt clean all
print_green 'Remove SSH keys'
[ -f /home/ubuntu/.ssh/authorized_keys ] && rm /home/ec2-user/.ssh/authorized_keys
print_green 'Cleanup log files'
find /var/log -type f | while read f; do echo -ne '' > $f; done
print_green 'Cleanup bash history'
unset HISTFILE
[ -f /root/.bash_history ] && rm /root/.bash_history
[ -f /home/ubuntu/.bash_history ] && rm /home/ubuntu/.bash_history
print_green 'AMI cleanup complete!'
@mikedixson
Copy link
Author

Updated to clean up an ubuntu based instance

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