Skip to content

Instantly share code, notes, and snippets.

@jacoor
Created April 30, 2019 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacoor/1088efc66c2bf168c08c290bf69f6969 to your computer and use it in GitHub Desktop.
Save jacoor/1088efc66c2bf168c08c290bf69f6969 to your computer and use it in GitHub Desktop.
AWS EC2 user data error handling
# error handling
# redirect logs so can be found in instance -> actions -> instance settings -> Get System log
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
# trap errors. Shutdown restarts machine in an hour and prevents further commands,
# so instances won't finish booting and won't report to ECS
function my_trap()
{
echo "Sorry, there was an error at line $1 $2" && shutdown -h +60
}
trap 'my_trap $${LINENO} $$? ' ERR
# /error handling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment