Published on

Google Cloud Signals on Termination

CLI set-up

Set up a preemption alert message

Useful information found at this website, and also Google's Docs. However : I didn't experience the problems outlined in the blog post. Google's 'hook' method seems to work pretty well in both the preemption and CLI instance stop cases - let me know if you have problems, since then I'll dig into the direct ACPI route (which wasn't necessary with the implementation below).

Create a Slack API endpoint

Set up a Slack Webhook using the instructions provided :

  • Create app associated with your preferred Slack workspace
  • 'Add Features and Functionality'-'Incoming Webhooks' : Switch to 'On'
  • 'Add New Webhook to Workspace' : Click button
  • Fill in the channel to which the webhook should post
  • Copy the 'Webhook URL', which looks like :

Create a script to call the Slack endpoint

Create this script as a local file shutdown.bash, making sure to update the SLACK_URL within post_to_slack() :


function post_to_slack () {
  # format message as a code block ```${msg}```

  case "$2" in

  curl -X POST --data "payload={\"text\": \"${SLACK_ICON} ${SLACK_MESSAGE}\"}" ${SLACK_URL}

post_to_slack "Server '`hostname`' going down..." "WARNING"
exit 0

Alternative : Use Pushed which has an app with a free tier. However, reviews of the app indicate that it may not be super-reliable, which kind of defeats the purpose of having it as a critical messaging system.

Create a 'shutdown hook' for the GCP machine

Post that script to the VM using the gcloud add-metadata command from your local machine :

gcloud compute instances add-metadata $INSTANCE_NAME \
    --metadata-from-file shutdown-script=shutdown.bash

Test that it works

This has worked for me :

  • Running shutdown.bash on the local machine
  • Running shutdown.bash on the VM itself
  • Watching Slack during instance stop
  • Being rudely interrupted via Slack when the VM gets preempted...

... as intended.