Skip to content

Instantly share code, notes, and snippets.

@garlandkr
Last active March 20, 2023 08:19
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save garlandkr/e80674b49270b0199fa6 to your computer and use it in GitHub Desktop.
Save garlandkr/e80674b49270b0199fa6 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.github.com/garlandkr/e80674b49270b0199fa6/raw/ami-clean.sh
function print_green {
echo -e "\e[32m${1}\e[0m"
}
print_green 'Clean Yum'
yum clean all
print_green 'Remove SSH keys'
[ -f /home/ec2-user/.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/ec2-user/.bash_history ] && rm /home/ec2-user/.bash_history
print_green 'AMI cleanup complete!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment