Skip to content

Instantly share code, notes, and snippets.

Forked from jdowning/
Last active May 10, 2021 11:26
What would you like to do?
Script to clean up Ubuntu EC2 instance before packaging as an AMI
# This script cleans up your EC2 instance before baking a new AMI.
# this works on Ubuntu (18+)
# Run the following command in a root shell:
# bash <(curl -s
function print_green {
echo -e "\e[32m${1}\e[0m"
print_green 'Clean Apt'
apt-get -y autoremove
apt-get clean
# clean journal
journalctl --rotate
journalctl --vacuum-time=1s
# clean up cloud-init cache
test -d /var/lib/cloud && /bin/rm -rf /var/lib/cloud/*
#print_green 'Remove SSH keys'
#[ -f /home/ubuntu/.ssh/authorized_keys ] && rm /home/ubuntu/.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'
[ -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