Proof of concept, dead man switch to auto shutoff AWS instance(s)
This is a rudimentary tool that provides a "deadman's switch" functionality to running AWS instances. Its designed as a failsafe to shutdown AWS instances and prevent excessive costs.
This tool comprises 2 parts:
- aws-kill-switch-poll.sh
- deadman.sh
NOTE: these are not terribly well named.
This script is designed to run on my home lab (which does not incur any cost, beyond electricity).
This script:
- Runs on a linux VM via a cron job
- Write the out of
date +%s
to a text file (unix epoch time, number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT)) - Commit and push file to this git repo
This script is designed to run on an AWS instance (VM).
- Runs via cron job
- Fetches contents of file that above script commits to
- Compares fetched value with current unix epoch time
- IF difference (in seconds) is greater than configured max, shutdown instance (
shutdown -h now
)
Prerequisites:
- Git
- Git config completed with user.email and user.name
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
- Git credential helper configured to 'store'
git config --system credential.helper store
- Github Personal Access Token
- Via Github / Settings / Developer Settings / Personal access tokens
- This is required in order to authenticate using an account with MFA/2FA enabled
- Via Github / Settings / Developer Settings / Personal access tokens
Instructions:
- Create directory
/root/git
- Git Clone this repo to this directory, which will create
/root/git/aws-kill-switch
git clone https://....
- NOTE: run
git push
at at least once to save the credential
- Copy
aws-kill-switch-poll.sh
- from
/root/git/aws-kill-switch/src
- to
/root/git
- NOTE: this is done to ensure the script isn't changed while running and to avoid accidentally breaking the script
- from
- Add a line to crontab
*/5 * * * * /root/git/aws-kill-switch-poll.sh >/dev/null 2>&1
- NOTE: this runs once every 5 minutes, but can be customzied to suite your needs
- At this point
graylog-aws.log
will be updated each time the cron job executes
- Copy
deadman.sh
to /root - Make executable via
chmod +x deadman.sh
- Add to crontab