Skip to content

Instantly share code, notes, and snippets.

@ambakshi
Created November 2, 2014 19:55
Show Gist options
  • Save ambakshi/16c263aa815fe4d3bc44 to your computer and use it in GitHub Desktop.
Save ambakshi/16c263aa815fe4d3bc44 to your computer and use it in GitHub Desktop.
Send an email when an instance dies during cloud-init.
#!/bin/bash
exec > >(tee /var/log/user-data.log|logger -it user-data -s 2>/dev/console) 2>&1
eval `ec2-tags -s -i`
/usr/sbin/sendmail -oi -t -f $EMAIL <<EOM
From: $EMAIL
To: $EMAIL
Subject: EC2 instance failure
This email message was generated on the following EC2 instance:
`ec2-tags -s -i | sort`
If the instance is still running, you can monitor the output of this
job using a command like:
\$ ssh ec2-user@$EC2_LOCAL_IPV4 tail -1000f /var/log/user-data.log
\$ ec2-describe-instances --region $AWS_DEFAULT_REGION $INSTANCE_ID
--
`cat /var/log/user-data.log 2>/dev/null`
--
`ps fax`
--
EOM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment