Lokun (Icelandic for 'shutdown') is a simple Google Cloud function to manage compute instances using a configured schedule.
- Go to your compute instance in Google Cloud Console and edit it
- Add a network tag
auto-shutdown
- Configure the start/end time by adding a
start-time
andstop-time
key/value to the custom metadata section. Times are in theHH:mm
format and UTC-based. - If you want to configure different machine types on a daily basis, add a
machineType-{day}
key/value to the custom metadata section. When starting a machine, it will resize it if a different machine type is configured for that day. When no specific machine type is configured for that day, it will keep the same machine type.- Use the 3-letter word for days:
mon
,tue
,wed
,thu
,fri
,sat
andsun
- Use the machine types specified here: https://cloud.google.com/compute/docs/machine-types
- Use the 3-letter word for days:
An example configuration may look like:
This configuration results in the following schedule:
- Monday: a
g1-small
machine - Tuesday till thursday: an
n1-standard-1
machine - Friday: a
g1-small
machine - Saturday and sunday: an
f1-micro
machine
- Go to the Google Cloud Console
- Create a new Cloud Function
- Give it a name, such as
checkGcpInstances
- Choose 128MB memory
- Choose your function trigger (HTTP for example)
- Choose 'Inline editor'
- Grab index.js and package.json and copy-paste these into the corresponding editors
- Use
checkGcpInstances
as function name to execute - Deploy your function and execute a test request
Depending on your cloud function configuration you need to trigger the function at some interval. Google Cloud Functions provides different trigger mechanisms such as HTTP triggers, Cloud Pub/Sub and Cloud Storage buckets. When using an HTTP trigger you could configure an HTTP uptime/ping tool to call the function with an interval.
By default the Google Cloud project ID will be detected from the platform. You can also specify the project ID in the request body using the following payload:
{ "project": "PROJECT_ID" }
Authentication settings will also be detected from the platform.