Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ryanmark
Forked from jdowning/ami-clean.sh
Last active August 8, 2019 06:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanmark/e8319855354ec157cdaa to your computer and use it in GitHub Desktop.
Save ryanmark/e8319855354ec157cdaa to your computer and use it in GitHub Desktop.
Script to clean up Ubuntu EC2 instance before packaging as an AMI
#!/bin/bash
# This script cleans up your EC2 instance before baking a new AMI.
# Run the following command in a root shell:
#
# bash <(curl -s https://gist.github.com/ryanmark/e8319855354ec157cdaa/raw/ami-clean.sh)
function print_green {
echo -e "\e[32m${1}\e[0m"
}
print_green 'Clean Apt'
apt-get -y autoremove
aptitude clean
aptitude autoclean
print_green 'Remove SSH keys'
rm -Rf /home/ubuntu/.ssh/*
rm -Rf /var/lib/cloud/data/*
rm -Rf /var/lib/cloud/seed/*
rm -f /var/lib/cloud/instance
rm -Rf /var/lib/cloud/instances/*
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!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment